以下代码可将正在促销的产品排除在“动态定价”折扣之外。现在我想将此功能扩展到也包括特定的产品和类别(不一定在同一功能中)。 在 if 语句中添加“或产品 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;
}
:
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 文件中。应该可以。