根据 Woocommerce 中的用户输入自定义购物车商品价格

问题描述 投票:0回答:1

在我们的 Woocommerce 商店中,我们对任何产品都有一些最低价格。每个产品内页都有两个字段,客户可以在其中输入产品的宽度、高度。然后他们可以将该产品添加到购物车,然后价格根据给定的宽度和高度进行更改。

例如,如果产品的最低价格是 50 。然后客户加上宽度=2,高度=3,那么这个产品的价格就是50*2*3=300

因此,为了安排这个,我们将此代码添加到

function.php

add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {

   $result=$_POST['width']*$_POST['height']*$cart_item_data['data']->price;

   $cart_item_data['new-price'] =$result;

   $cart_item_data['data']->set_price($result);

   return $cart_item_data;
}

所以这工作正常。

但问题是,一到结账页面,产品价格显示为50,但实际上是300。

所以为了克服这个问题,我使用以下代码

add_filter( 'woocommerce_cart_item_subtotal', 'update_with_custom_details', 10, 3 ); 
function update_with_custom_details( $subtotal, $cart_item, $cart_item_key ) {
     $subtotal =" £".$cart_item['line_total'];
     return $subtotal;
}

现在显示 300 英镑,但我知道这个代码是错误的。

代码错误,因为当客户为该产品申请50%优惠券代码时,折扣是25,因为它是根据

50*.5=25;
计算的,但实际上产品新价格是300,所以折扣应该是
300*5=150;

那么我如何更新购物车页面、结帐页面、迷你购物车等中的产品价格?

请帮忙。

另请参阅此 wocommerce 优惠券不接受 Woocommerce 产品定制价格

php wordpress woocommerce cart product-price
1个回答
4
投票

更新2:正确的做法需要分两步完成:

  • 我们计算“添加到购物车”事件挂钩的价格并将其保存为自定义购物车商品数据。
  • 我们将计算出的价格设置在
    woocommerce_before_calculate_totals
    钩子

代码:

// Save custom field value in cart item as custom data
add_filter( 'woocommerce_add_cart_item_data', '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;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        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'] );
        }
    }
}

代码位于活动子主题(或主题)的 function.php 文件中。已测试并有效。


出于测试目的,我使用了以下代码,在单个产品页面上输出 2 个自定义字段:

// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
    global $product;
    // The fake calculated price
    ?>
        <div>
            <label class="product-custom-text-label" for="servicetime"><?php _e( 'Custom text:', 'woocommerce'); ?><br>
                <input type="text" id="user-width" name="width" value=""><br>
                <input type="text" id="user-height" name="height" value="">
            </label>
        </div><br>
    <?php
}
© www.soinside.com 2019 - 2024. All rights reserved.