添加到购物车时,根据自定义字段值动态计算产品价格



我找到了添加现有产品的代码:

global $woocommerce;
$woocommerce->cart->add_to_cart(16);

但是我需要添加产品,价格由 2 个输入决定:

  1. 年龄
  2. 质量

用公式年龄*质量计算商品的价格。

我知道可以添加具有变体的产品,但是有很多可能的变化。

是否可以根据自定义字段计算值动态设置产品价格?

谢谢

对于购物车项目(将产品添加到购物车时),可以根据产品自定义字段值设置自定义动态计算价格

1)首先,我们在单个产品页面中添加您的2个自定义字段(这里是普通text input字段和select字段),用于"Age""Quality"

// Add product custom fields inside the product add-to-cart form
add_action('woocommerce_before_add_to_cart_button', 'custom_data_hidden_fields');
function custom_data_hidden_fields() {
echo '<div class="imput_fields custom-imput-fields">
<label class="age_prod">Age: <br><input type="text" id="age_prod" name="age_prod" value="" /></label>
<label class="quality_prod">Quality: <br>
<select name="quality_prod" id="quality_prod">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</label>
</div><br>';
}

2) 当产品添加到购物车时,我们将这些自定义帖子数据值与添加的购物车项目一起保存:

// Save product custom fields values after submission into the cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_hidden_fields', 10, 2 );
function save_custom_data_hidden_fields( $cart_item_data, $product_id ) {
$data = array();
if( isset( $_REQUEST['age_prod'] ) ) {
$cart_item_data['custom_data']['age'] = $_REQUEST['age_prod'];
}
if( isset( $_REQUEST['quality_prod'] ) ) {
$cart_item_data['custom_data']['quality'] = $_REQUEST['quality_prod'];
}
// below statement make sure every add to cart action as unique line item
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
return $cart_item_data;
}

3)最后,在最后一步中,您可以从购物车获取所有必要的数据以进行自己的计算,并动态更新每个购物车项目的价格。

我做了一个临时计算,只是为了表明代码按预期工作。

在下面的代码中,您必须根据自己的需求自定义计算(因为您在问题中没有提供有关此的任何详细信息)。
正如您将在下面看到的,这是进行自己计算的简单步骤,因为您可以获得每个购物车项目的自定义字段值以及产品的原始价格。

以下是将根据您的自定义字段值进行此动态计算的代码:

// Replace the item price by your custom calculation
add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10 );
function add_custom_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 ) {
// Product ID
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
// Product original price
$original_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price();
$original_price = $item_values['data']->price; 
## --- Get your custom cart item data --- ##
if( isset($cart_item['custom_data']['age']) )
$age     = $cart_item['custom_data']['age'];
if( isset($cart_item['custom_data']['quality']) )
$quality = $cart_item['custom_data']['quality'];
// CALCULATION FOR EACH ITEM:
## Make HERE your own calculation to feet your needs  <==  <==  <==  <==
$new_price = $original_price + ( ($age * 0.1) + $quality );
## Set the new item price in cart
if( version_compare( WC_VERSION, '3.0', '<' ) )
$cart_item['data']->price = $new_price;
else
$cart_item['data']->set_price($new_price);
}
}

代码进入函数.php活动子主题(或主题)的文件或任何插件文件中。

代码经过测试并完美运行。

在单个产品页面中显示自定义价格:

阅读本文以了解如何通过您的主题覆盖 WoocCommerce 模板

为此,您需要编辑模板single-product/price.php.

并且您需要向此单个产品页面添加一些javascript/jQuery以使产品价格更新,此时客户将在您的产品自定义字段中设置一些值。
但这是另一个问题。


相关答案: WooCommerce - 为购物车中的每个产品添加自定义价格

最新更新