首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce产品定制价格不接受woocommerce优惠券

Woocommerce产品定制价格不接受woocommerce优惠券
EN

Stack Overflow用户
提问于 2018-03-22 05:00:28
回答 1查看 241关注 0票数 1

在我们的woocommerce商店,客户可以输入根据这些细节计算的产品和产品价格的定制宽度和高度。

例如,如果产品的初始价格为50。客户加上宽度=2,height=3,则该产品的价格将转到50*2*3=300

为此,我们将使用以下代码

代码语言:javascript
复制
// 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'] );
        }
    }

这是一种病毒,但问题是:

当客户对此产品使用50%的优惠券代码时,折扣为25,因为它是根据50*(50/100)=25计算的; 但是实际上新产品的价格是300,所以的折扣应该是300*(50/100)=150;

EN

回答 1

Stack Overflow用户

发布于 2018-03-22 21:37:40

尝试将您的“calculate_custom_cart_item_prices”函数更新到类似的内容,看看这是否有帮助。

代码语言:javascript
复制
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‘过滤器的工作方式,因此您需要更新这个函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49420877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档