本文介绍了添加到购物车时,根据自定义字段值动态计算产品价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我找到了添加现有产品的代码:I found the code to add existing product:global $woocommerce;$woocommerce->cart->add_to_cart(16);但是我需要添加价格由2个输入决定的产品:But I need to add product, with price that determined by 2 inputs: 年龄 质量我通过年龄*质量公式来计算产品价格。I calculate the product's price by the formula age*quality.我知道可以添加具有变化的产品,但是有很多可能的变化。I know it's possible to add product with variations, but it's so many possible variations. 是否可以根据自定义字段计算值动态设置产品价格?谢谢推荐答案对于购物车商品(将产品添加到购物车时),可以设置自定义For the cart items (when product is added-to cart), is possible to set a custom dynamic calculated price based on product custom fields values. 1)首先,我们在单个产品页面中添加您的2个自定义字段(以下是正常的 文本输入 字段和 select 字段) > 年龄 和 质量 :1) First we add in the single product pages your 2 custom fields (Here a normal text input field and a select field) for "Age" and "Quality":// Add product custom fields inside the product add-to-cart formadd_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)当产品加入购物车时,我们保存了这些自定义帖子带有已添加购物车项目的数据值:2) When product is add-to-cart, we save these custom post data values with the added cart item:// Save product custom fields values after submission into the cart item dataadd_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)最后,从购物车中获取所有必要数据自行计算,并动态更新每个购物车项目的价格。3) Finally in this last step, you get all necessary data from cart to make your own calculation, and to update the price for each cart item dynamically. 我做了一个临时计算,只是为了证明代码按预期工作。在这里,在下面的代码中,您将必须自定义计算以满足您的需求(因为您在问题中没有提供任何详细信息)。 正如您将在下面看到的那样,这是一个简单的计算步骤,因为您可以获取每个购物车项目的自定义字段值以及产品的原始价格。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.以下是根据您的自定义字段值进行此动态计算的代码:Here is the code that will make this dynamic calculation based on your custom field values:// Replace the item price by your custom calculationadd_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文件中主题(或主题)或任何插件文件中。Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码已经过测试,可以正常工作。Code is tested and works perfectly. 在单个产品页面上显示定制价格: > 阅读此书可了解如何通过您的主题覆盖WoocCommerce模板为此,您将需要编辑模板 single-product / price.php 。For that you will need to edit the template single-product/price.php.然后,当客户将在您的产品自定义字段中设置一些值时,您需要在此单个产品页面上添加一些javascript / jQuery 来更新产品价格。 但这是另一个问题。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 -为购物车中的每个产品添加自定义价格 这篇关于添加到购物车时,根据自定义字段值动态计算产品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 14:08