在 WooCommerce 动态定价中排除特定产品 ID and/or 类别
Exclude specific product ID and/or categogry in WooCommerce Dynamic Pricing
以下代码用于将正在打折的产品从 Dynamic Pricing 折扣中排除。现在我想将此功能扩展到还包括特定产品和类别(不一定在同一功能中)。
在 if 语句中添加一个 "or product id = xx" 的 ID 是否足够?
目前的代码如下:
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
if ( $product->get_sale_price() ) {
$eligible = false;
}
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
return $eligible;
}
您可以尝试以下方法来处理要在代码中定义的特定产品 ID 和产品类别 (未测试):
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
// Here Define product ids to be excluded
$product_ids = array( 37, 53 );
// Here Define product categories to be excluded
$categories = array( 't-shirts', 'posters' );
remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
if ( $product->get_sale_price() ) {
$eligible = false;
}
// Handling specific product IDS
if ( in_array( $product->get_id(), $product_ids ) ) {
$eligible = false;
}
// Handling specific product categories
$product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$eligible = false;
}
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
return $eligible;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。应该可以。
以下代码用于将正在打折的产品从 Dynamic Pricing 折扣中排除。现在我想将此功能扩展到还包括特定产品和类别(不一定在同一功能中)。
在 if 语句中添加一个 "or product id = xx" 的 ID 是否足够?
目前的代码如下:
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
if ( $product->get_sale_price() ) {
$eligible = false;
}
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
return $eligible;
}
您可以尝试以下方法来处理要在代码中定义的特定产品 ID 和产品类别 (未测试):
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
function is_product_eligible( $eligible, $product, $discounter_name, $discounter_object ) {
// Here Define product ids to be excluded
$product_ids = array( 37, 53 );
// Here Define product categories to be excluded
$categories = array( 't-shirts', 'posters' );
remove_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
if ( $product->get_sale_price() ) {
$eligible = false;
}
// Handling specific product IDS
if ( in_array( $product->get_id(), $product_ids ) ) {
$eligible = false;
}
// Handling specific product categories
$product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$eligible = false;
}
add_filter( 'woocommerce_dynamic_pricing_process_product_discounts', 'is_product_eligible', 10, 4 );
return $eligible;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。应该可以。