基于用户在Woocommerce中输入的自定义购物车项目价格



在我们的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产品定制价格

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

代码进入函数.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
}

最新更新