应用优惠券时,将 WooCommerce 购物车中的产品销售价格更改为正常价格,但排除某些类别

贾科莫

应用优惠券时(属于某种类型),我通过以下方式将产品折扣价更改为正常价格:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }

    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}

但是,如果我排除了一个类别,代码就会吓坏,因为它将购物车中的价格从销售更改为常规,并且不添加折扣。

如何解决此问题,以使排除的类别不会更改为正常价格?

7uc1f3r

要排除某些类别,您可以has_term()在遍历购物车项目时使用

所以你得到:

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    
    // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
    $excluded_categories = array( 63, 15, 'categorie-1' );

    // Initialize
    $coupon_flag = false;

    // Loop through applied coupons
    foreach ( $cart->get_applied_coupons() as $coupon_code ) {
        // Get an instance of the WC_Coupon Object
        $coupon = new WC_Coupon( $coupon_code );
        
        // Only for certain types, several can be added, separated by a comma
        if ( in_array( $coupon->get_discount_type(), array( 'percent', 'percent_product' ) ) ) {
            $coupon_flag = true;
            break;
        }
    }
    
    // True
    if ( $coupon_flag ) {
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product ID in
            $product_id = $cart_item['product_id'];

            // NOT contains the definite term
            if ( ! has_term( $excluded_categories, 'product_cat', $product_id ) ) {
                // Get regular price
                $regular_price = $cart_item['data']->get_regular_price();
                
                // Set new price
                $cart_item['data']->set_price( $regular_price );
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在WooCommerce中最便宜的购物车上应用100%的优惠券折扣

不要对WooCommerce中的缺货产品应用优惠券折扣

在应用优惠券代码后更改WooCommerce购物车价格

即使WooCommerce中的购物车为空,也可以通过URL中的GET方法应用优惠券折扣

以编程方式将优惠券应用于WooCommerce3中的订单

在WooCommerce 3中以编程方式设置产品销售价格

自动针对特定产品ID在Woocommerce购物车中应用或删除优惠券

将WooCommerce产品的销售价格复制到正常价格并重置销售价格

在Woocommerce中为类别应用优惠券时,自定义购物车项目价格

隐藏Woocommerce 3中特定产品类别的购物车优惠券字段

根据Woocommerce中特定购物车数量自动应用优惠券

在Woocommerce中添加费用之前检查已应用的优惠券

在Woocommerce中应用优惠券时显示罢工的购物车小计

在WooCommerce中显示正常价格之前的销售价格

仅当在Woocommerce中应用了优惠券时才允许购买特定产品

根据产品属性条款有条件地应用WooCommerce优惠券

如果在Woocommerce中未应用优惠券,则自动对用户角色应用折扣

在Woocommerce中应用特定优惠券时隐藏免费送货

使用优惠券简码形式检查优惠券是否在WooCommerce中应用

关于应用或删除优惠券的woocommerce重新加载购物车页面

将“ [删除]”链接文本更改为WooCommerce结帐中的优惠券图标

如何在woocommerce的购物车页面中同时显示总订单销售价格和常规价格(或总折扣价格)

在正常价格之前显示销售价格(WooCommerce)

在 WooCommerce 中应用特定优惠券代码时发送电子邮件通知

限制为一张应用的优惠券,删除 Woocommerce 中其他以前应用的优惠券

仅当在 Woocommerce 中应用优惠券时才允许购买特定产品

在 WooCommerce 中根据客户送货区域应用特定优惠券

WooCommerce 应用优惠券取决于购物车订单项数量

Woocommerce如何替换缺货产品的销售价格但保持正常价格?