Woocommerce product custom price is not accepted by woocommerce coupon

丶灬走出姿态 提交于 2019-12-11 07:37:34

问题


In our woocommerce shop , customer can enter the custom width and height of the product and product price calculated based on this details .

For example if the initial price for a product is 50 . And customer add width =2, height=3 , then the price for this product is going to 50*2*3=300

for this we are using following code

// Save custom field value in cart item as custom data
add_filter( 'woocommerce_add_cart_item', 'calculate_custom_cart_item_prices', 30, 3 );
function calculate_custom_cart_item_prices( $cart_item_data, $product_id, $variation_id ) {
    if ( isset($_POST['width']) && isset($_POST['height']) ) {
        // Get the correct Id to be used (compatible with product variations)
        $the_id = $variation_id > 0 ? $variation_id : $product_id;

        $product = wc_get_product( $the_id ); // Get the WC_Product object
        $product_price = (float) $product->get_price(); // Get the product price

        // Get the posted data
        $width  = (float) sanitize_text_field( $_POST['width'] );
        $height = (float) sanitize_text_field( $_POST['height'] );

        $new_price = $width * $height * $product_price; // Calculated price

        $cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
    }
    return $cart_item_data;
}

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

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! empty( $cart_item['calculated-price'] ) ){
            // Set the calculated item price (if there is one)
            $cart_item['data']->set_price( $cart_item['calculated-price'] );
        }
    }

And it is wormking , but the problem is:

when customer apply a 50% coupon code for this product then the discount is coming as 25 , because it calculating based on 50*(50/100)=25;

But actually product new price is 300, so the discount should be 300*(50/100)=150;


回答1:


Try updating your 'calculate_custom_cart_item_prices' function to something like this and see if that helps.

add_filter( 'woocommerce_add_cart_item', 'calculate_custom_cart_item_prices', 30, 2 );
function calculate_custom_cart_item_prices( $cart_item_data, $cart_item_key ) {
    if ( isset($_POST['width']) && isset($_POST['height']) ) {
        // Get the correct Id to be used (compatible with product variations)
        $the_id = $cart_item_data['variation_id'] > 0 ? $cart_item_data['variation_id'] : $cart_item_data['product_id'];

        $product = wc_get_product( $the_id ); // Get the WC_Product object
        $product_price = (float) $product->get_price(); // Get the product price

        // Get the posted data
        $width  = (float) sanitize_text_field( $_POST['width'] );
        $height = (float) sanitize_text_field( $_POST['height'] );

        $new_price = $width * $height * $product_price; // Calculated price

        $cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
    }
    return $cart_item_data;
}

My guess as to what is happening is a change in Woocommerce has changed the way the 'woocommerce_add_cart_item' filter works and so you need to update this function.



来源:https://stackoverflow.com/questions/49420877/woocommerce-product-custom-price-is-not-accepted-by-woocommerce-coupon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!