我试图应用一个百分比折扣的基础上的产品与特定的产品属性的数量,在购物车.
更确切地说,我的目标是应用15%折扣的订单最少6产品与属性flaske
.
我已经成功地实现了变量属性集的可变产品,但我似乎不能针对单一/简单的产品。
到目前为止我的代码(借用自Condition for Quantity and Price for Woocommerce):
// Discount based on product quantity and attribute in cart
add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
function wc_cart_item_quantity_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// INITIALIZING VARIABLES
$min_item_amount = 6; // Min quantity
$discount = $items_count = $percent = $items_subtotal = 0;
$taxonomy = 'pa_variant'; // Taxonomy
$term_slugs = array('flaske'); // Term/terms
// Loop through cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Loop through variation
foreach( $cart_item['variation'] as $attribute => $term_slug ) {
// Only counting items that are above 6 and has attribute
if( $cart_item['data']->get_price() >= $min_item_amount && $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
$items_count += $cart_item['quantity'];
$items_subtotal += $cart_item['line_subtotal'];
}
}
}
// CONDITIONAL PERCENTAGE
if ($items_count >= 6 ) {
$percent = 15;
}
// DISCOUNT (TAXABLE)
if( $items_count > 0 ) {
// Calculation
$discount -= ($items_subtotal / 100) * $percent;
$cart->add_fee( __( "Mix & Match rabat - $percent%", "woocommerce" ), $discount, true);
}
}
字符串
我当前的代码对于变量产品(变体)非常有效,但它似乎不会影响单个产品,即使我给单个产品赋予与变量产品相同的属性。
我怀疑这与foreach
循环foreach( $cart_item['variation'] as $attribute => $term_slug )
有关
我如何才能使它在一般情况下工作,所以它也适用于具有相同属性flaske
的单一/简单产品?
任何帮助和建议将不胜感激。
其他有用的参考资料:
Woocommerce percentage discount per item based on quantity的
Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce的
1条答案
按热度按时间gxwragnw1#
在简单产品或可变产品中,您无法获得产品属性集,这与产品变体相同。
以下重新访问的代码也应该处理简单的产品(未测试):
字符串
应该能用