【问题标题】:Dynamically calculate product price based on custom fields values when added-to-cart添加到购物车时根据自定义字段值动态计算产品价格
【发布时间】:2017-01-28 23:05:15
【问题描述】:

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

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

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

  1. 年龄
  2. 质量

我通过年龄*质量公式计算产品的价格。

我知道可以添加具有变体的产品,但可能的变体太多了。

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

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

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

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

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

    代码已经过测试并且运行良好。

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

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

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

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


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

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多