在Woocommerce中为类别应用优惠券时,自定义购物车项目价格 [英] Custom cart item price when applying coupon for a category in Woocommerce

查看:91
本文介绍了在Woocommerce中为类别应用优惠券时,自定义购物车项目价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在商店中有一些产品,并且正在为客户提供一些优惠券。

We have some products in shop , and we are giving some coupons to customer .

product -> ABC price 10 

coupon code is 'newcp' discount 20%;

因此,当人们将产品添加到购物车时,价格为10。

so when people add the product to cart price will be 10 .

然后他们应用优惠券,然后将原始产品价格显示为10,然后从中计算出20%,最后总和为8

Then they apply coupon then original product price shown as 10 and calculate 20% from that and at the end the total will be 8

我们需要根据特定条件进行更改

But now we need to change this as per specific condition

当人们应用产品优惠券 newbc

When people apply product coupon newbc

1)如果优惠券是 newcp ,则将 order_item_price 更改为 order_item_price +3 [仅当类别为水果时],并且此价格应显示在购物车页面,结帐页面,订购电子邮件中

1)if coupon is newcp , then change order_item_price as order_item_price +3[ only if category is Fruit] , and this price should be shown in cart page, checkout page , order email

2)从新价格计算折扣,从13

2)Calculate discount from the new price calculate discouunt from 13

3计算抵扣3)如果人们删除优惠券,价格将再次回到10

3)If people remove coupon then price will again return to 10

我提出了2个解决方案,但不起作用。

解决方案1 ​​

add_action('woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);

function add_custom_price($cart_obj)
    {
    if (is_admin() && !defined('DOING_AJAX')) return;
    foreach($cart_obj->get_cart() as $key => $value)
        {
        $product_id = $value['product_id'];
        $coupon_code = $value['coupon_code'];
        if ($coupon_code != '' && $coupon_code == "newcp")
            {
            global $woocommerce;
            if (WC()->cart->has_discount($coupon_code)) return;
              else
                {
                if (has_term('fruits', 'product_cat', $product_id))
                    {
                    $value['data']->set_price(CURRENT_CART_PRICE + 3);
                    }
                }
            }
        }
    }

解决方案2

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 ) {
           $coupon = $code;
        }
    }

    if ($coupon == "newcp"){
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {


            $price = $cart_item['data']->price+3;
            $cart_item['data']->set_price( $price );
        }
    }

}

请帮助。

推荐答案

以下是实现该目标的一种可能方法:

Here is a possible way to achieve that:

// Add custom calculated price conditionally as custom data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'custom_add_cart_item_data', 20, 2 );
function custom_add_cart_item_data( $cart_item_data, $product_id ){
    // Your settings below
    $product_categories = array('fruits'); 
    $addition = 3;

    $product = wc_get_product($product_id);
    $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product_id;


    if ( has_term( $product_categories, 'product_cat', $the_id ) )
        $cart_item['custom_price'] = $product->get_price() + $addition;

    return $cart_item;
}

// Set conditionally a custom item price
add_action('woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only for a DEFINED coupon code ( to be set below )
    $coupon_code = 'newcp';
    if ( ! $cart->has_discount( $coupon_code ) ) return;

    foreach( $cart->get_cart() as $cart_item ) {
        if ( isset($cart_item['custom_price']) ) {
            $cart_item['data']->set_price( (float) $cart_item['custom_price'] );
        }
    }
}

代码已输入活动子主题(或活动主题)的function.php文件。经过测试并可以正常工作。

Code goes in function.php file of the active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中为类别应用优惠券时,自定义购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆