gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:28:01 24 4
gpt4 key购买 nike

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

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) 最后,在这最后一步中,您从购物车中获取所有必要的数据以进行自己的计算,并动态更新每个购物车商品的价格。

I have make a temporary calculation, just to show that the code is working as expected.

Here In the code below, you will have to customize the calculation to feet your needs (as you don't give any details about that in your question).
As you will see below, it's an easy step to make your own calculation, as you get your custom fields values for each cart item and also the original price of the product.

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

// 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);
}
}

代码进入您活跃的子主题(或主题)的 function.php 文件或任何插件文件。

代码已经过测试并且可以完美运行。

DISPLAYING THE CUSTOM PRICE IN SINGLE PRODUCT PAGE:

Read this to understand how override the WoocCommerce templates via your theme

For that you will need to edit the template single-product/price.php.

And you will need to add some javascript/jQuery to this single product page to make the product price updated, when customer will set some values in your product custom fields.
But this is another question.


相关回答:WooCommerce - Adding a custom price to each product in cart

关于php - 添加到购物车时根据自定义字段值动态计算产品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com