基于Woocommerce中用户输入的自定义购物车项目价格 [英] Custom cart item price based on user input in Woocommerce
问题描述
在我们的Woocommerce商店中,我们为任何产品设置了最低价格。并且在每个产品内页中都有两个字段,客户可以在其中键入产品的宽度,高度。然后他们可以将此产品添加到购物车,然后根据给定的宽度和高度更改价格。
例如,某产品的最低价格为 50 。然后客户添加 width = 2,height = 3 ,则该产品的价格将为 50 * 2 * 3 = 300
因此要安排这些,我们将此代码添加到 function.php
add_filter('woocommerce_add_cart_item','add_custom_cart_item_data',10,2);
函数add_custom_cart_item_data($ cart_item_data,$ cart_item_key){
$ result = $ _ POST ['width'] * $ _ POST ['height'] * $ cart_item_data ['data']-> ;价钱;
$ cart_item_data [‘new-price’] = $结果;
$ cart_item_data [’data’]-> set_price($ result);
返回$ cart_item_data;
}
因此,此操作正常。
但是问题在于,一旦进入结帐页面,产品价格就会显示为50,但实际上是300。
p>所以要克服这个问题,我使用以下代码
add_filter('woocommerce_cart_item_subtotal ','update_with_custom_details',10,3);
函数update_with_custom_details($ subtotal,$ cart_item,$ cart_item_key){
$ subtotal =£。$ cart_item [’line_total’];
返回$小计;
}
现在显示的是£300,但我知道此代码是错误的。 / p>
代码错误,因为当客户为该产品应用50%优惠券代码时,折扣为25,因为它基于 50计算* .5 = 25;
但是实际上产品的新价格是300,所以折扣应该是 300 * 5 = 150;
那么我如何在购物车页面,结帐页面,迷你购物车等中更新产品价格?
请帮忙。
更新2:正确的实现方法需要分两步进行:
- 我们计算添加到购物车事件挂钩的价格,将其保存为自定义购物车项目数据。
- 我们在
woocommerce_before_calculate_totals
钩子
中设置该计算的价格。 ul>
代码:
//将购物车项中的自定义字段值另存为自定义数据
add_filter('woocommerce_add_cart_item_data','calculate_custom_cart_item_prices',30,3);
函数calculate_custom_cart_item_prices($ cart_item_data,$ product_id,$ variation_id){
if(isset($ _ POST ['width'])&& isset($ _ POST ['height'])){
//获取要使用的正确ID(与产品版本兼容)
$ the_id = $ variation_id> 0? $ variation_id:$ product_id;
$ product = wc_get_product($ the_id); //获取WC_Product对象
$ product_price =(float)$ product-> get_price(); //获取产品价格
//获取发布的数据
$ width =(float)sanitize_text_field($ _POST [’width’]);
$ height =(float)sanitize_text_field($ _POST [’height’]);
$ new_price = $宽度* $高度* $ product_price; //计算得出的价格
$ cart_item_data [‘calculated-price’] = $ new_price; //将此价格另存为自定义数据
}
return $ cart_item_data;
}
//在购物车价格
中设置自定义计算价格add_action(‘woocommerce_before_calculate_totals’,‘set_calculated_cart_item_price’,20,1);
函数set_calculated_cart_item_price($ cart){
if(is_admin()&&!defined('DOING_AJAX'))
return;
if(did_action(‘woocommerce_before_calculate_totals')> == 2)
return;
//遍历购物车项目
foreach($ cart-> get_cart()as $ cart_item){
if(!empty($ cart_item ['calculated-price' ]}} {
//设置计算出的商品价格(如果有)
$ cart_item ['data']-> set_price($ cart_item ['calculated-price']);
}
}
}
代码已输入您的活动子主题(或主题)的function.php文件。经过测试并可以正常工作。
出于测试目的,我使用了以下内容在单个产品页面上输出2个自定义字段的代码:
//隐藏的产品自定义字段
add_action('woocommerce_before_add_to_cart_button ','add_gift_wrap_field');
函数add_gift_wrap_field(){
全局$ product;
//伪造的计算价格
?>
< 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
}
in our Woocommerce store we have some minimum price for any products . And in every product inner page there is two filed where customer can type width , height of the product . And then they can add this product to cart, then price is changed based on given width and height.
For example if the minimum 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
So to arrange this we add this code to 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;
}
So this is working correctly .
But the problem is once it is coming to checkout page then the product price is showing as 50 , but it is 300 actually.
so to overcome this i use the following code
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;
}
now it is showing £300, but i know this code is wrong.
The code is wrong because when customer apply a 50% coupon code for this product then the discount is coming as 25 , Because it calculating based on 50*.5=25;
But actually product new price is 300, so the discount should be 300*5=150;
So how i can update the product price in cart page, checkout page ,mini cart etc ?
Please help .
Please see this also Woocommerce product custom price is not accepted by wocommerce coupon
Update 2: The correct way to do it need to be done in two steps:
- We calculate the price on add to cart event hook and save it as custom cart item data.
- We set that calculated price in
woocommerce_before_calculate_totals
hook
The code:
// 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'] );
}
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
For testing purpose, I have used the following code that output 2 custom fields on single product pages:
// 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
}
这篇关于基于Woocommerce中用户输入的自定义购物车项目价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!