Custom cart item price when applying coupon for a

2019-07-23 07:44发布

问题:

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

product -> ABC price 10 

coupon code is 'newcp' discount 20%;

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

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

When people apply product coupon newbc

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)Calculate discount from the new price calculate discouunt from 13

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

I made 2 solutions , but not working.

Solution 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);
                    }
                }
            }
        }
    }

Solution 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 );
        }
    }

}

Please help .

回答1:

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'] );
        }
    }
}

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