简体   繁体   English

WooCommerce 特定优惠券折扣基于产品类别

[英]WooCommerce specific coupon discount based on product category

I've a problem with this code to apply different percentage of discount to product in different category我对此代码有问题,无法对不同类别的产品应用不同百分比的折扣

The function work but is execute n times where n depends on the number of products in cart function 工作但执行 n 次,其中 n 取决于购物车中的产品数量

add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 1, 5 );
function alter_shop_coupon_data( $discount, $discounting_amount, $cart_item, $single, $instance ){
    $coupons = WC()->cart->get_coupons();
    $cart_items = WC()->cart->get_cart();

    foreach ( $coupons as $code => $coupon ) {
        if ( $coupon->discount_type == 'percent' && $coupon->code == 'newsite' ) {
            foreach ($cart_items as $cart_item){
                $product_id = $cart_item['data']->id;                 
                
                if( has_term( 'cat1', 'product_cat', $product_id ) ){
                    $quantity = $cart_item['quantity'];
                    $price = $cart_item['data']->price;
                    $discount_20 = 0.2 * ($quantity * $price); //  20%

                }else{
                    $quantity = $cart_item['quantity'];
                    $price = $cart_item['data']->price;
                    $discount_10 = 0.1 * ($quantity * $price); //10%
                }
            }
        }
    }
    $discounting_amount = ($discount_20 + $discount_10);
    return $discounting_amount;
}

Since WooCommerce 3, there are many mistakes in your code… Also the $cart_item argument is included in the function, so you don't need to loop through cart items.由于 WooCommerce 3,您的代码中存在许多错误......此外, $cart_item参数包含在 function 中,因此您无需遍历购物车项目。

Also note that $coupon->is_type('percent') (coupon type) is not needed as you target a specific coupon in your code: $coupon->get_code() == 'newsite .另请注意,当您在代码中定位特定优惠券时,不需要$coupon->is_type('percent') (优惠券类型)$coupon->get_code() == 'newsite

Try the following:尝试以下操作:

add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 100, 5 );
function alter_shop_coupon_data( $discount, $discounting_amount, $cart_item, $single, $instance ){
    // Loop through applied WC_Coupon objects
    foreach ( WC()->cart->get_coupons() as $code => $coupon ) {
        if ( $coupon->is_type('percent') && $coupon->get_code() == 'newsite' ) {
            $discount = $cart_item['data']->get_price() * $cart_item['quantity'];
 
            if( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ){
                $discount *= 0.2; // 20%
            }else{
                $discount *= 0.1; // 10%
            }
        }
    }
    return $discount;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should better work.它应该更好地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当特定产品在 WooCommerce 购物车中时,才根据特定产品类别应用/确定折扣 - Apply/determine discount based on a particular product category only when a specific product is in WooCommerce cart 特定Woocommerce产品类别项目的强制性优惠券代码 - Mandatory Coupon code for specific Woocommerce product category items 根据 Woocommerce 中的产品类别提供 2 种不同百分比折扣的优惠券 - Coupon with 2 different percentage discounts based on product category in Woocommerce Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce 根据Woocommerce购物车小计自动为特定产品应用优惠券 - Auto apply a coupon for a specific product based on Woocommerce cart subtotal 基于Woocommerce中特定产品变化的购物车项目折扣 - Cart item discount based on specific product variation in Woocommerce 基于特定产品类别的 WooCommerce 结帐消息 - WooCommerce checkout message based on specific product category 在 WooCommerce 中为产品类别强制使用优惠券字段 - Make coupon field mandatory for a product category in WooCommerce Woocommerce中所有产品类型按正常价格计算的优惠券折扣 - Coupon discount calculated on regular price for all product types in Woocommerce 在基于父项的Woocommerce产品上显示特定的产品子类别标题 - Display specific product child category title on Woocommerce product based on parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM