woocommerce优惠券不接受Woocommerce产品自定义价格 [英] Woocommerce product custom price is not accepted by woocommerce coupon

查看:114
本文介绍了woocommerce优惠券不接受Woocommerce产品自定义价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的woocommerce商店中,客户可以输入产品的自定义宽度和高度以及根据此详细信息计算出的产品价格.

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

例如,如果产品的初始价格为50 .然后客户添加 width = 2,height = 3 ,那么该产品的价格将为 50 * 2 * 3 = 300

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:

当客户为此产品应用50%的优惠券代码时, 折扣为25,因为它基于50 *(50/100)= 25进行计算;

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;

但是实际上产品的新价格是300,所以折扣应该是 300 *(50/100)= 150;

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

推荐答案

尝试将您的'calculate_custom_cart_item_prices'函数更新为类似的功能,看看是否有帮助.

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

我对正在发生的变化的猜测是Woocommerce发生了变化,从而改变了"woocommerce_add_cart_item"过滤器的工作方式,因此您需要更新此功能.

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.

这篇关于woocommerce优惠券不接受Woocommerce产品自定义价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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