Woocommerce根据产品页面中的客户输入计算金额

时间:2016-02-01 09:43:35

标签: wordpress

在产品页面中获取客户输入的字数。要计算产品附加费用金额固定金额,每500字以上额外10美元。

例如:1。字数:530 正常价格+额外价格:180 $ + 10 $ 总计:190美元

  1. 字数:1040 正常价格+额外价格:180 $ + 20 $ 总计:200 $
  2. 此过程用于创建动态输入表单客户计算金额的价格和总金额。

    `$extra_amount = (int)'10';
    $amount = (int)'180'; // how to get amount from woocommerce data
    if(isset($_POST['wordnumber'])){ // how to get this paramater form woocommerce post values
    $test =$_POST['wordnumber'];
    $original_amount = '';
        if($test <= (int)'500'){
            $original_amount = $amount;
        }
        elseif($test > (int)'500'){
            $div_amount = $test/(int)'500';
            $round = floor($div_amount);
            //echo '<br/>';
            $total_extra = $round*$extra_amount;
            $original_amount = $amount+$total_extra;
        }
        echo $original_amount;
    }`
    

1 个答案:

答案 0 :(得分:0)

我安装了WC Fields Factory插件,然后我使用密钥从一个字段获得了价值。因为我写了一个函数而且我压倒了价格值。

function calculate_gift_wrap_fee( $cart_object ) {
/* Gift wrap price */
$additionalPrice = 10;
    foreach ( $cart_object->cart_contents as $key => $value ) {  
             $test = $value['custom field'];
               if( $test <= 100 ) {
                   $quantity = floatval( $value['quantity'] );
                    $orgPrice = floatval( $value['data']->price );
                    $value['data']->price = ( $orgPrice );
                }
                elseif( $test > 100 ) {
                    $div_amount = floatval($test/500);
                    $round = floor($div_amount);
                    $total_extra = floatval($round * $additionalPrice); 
                    $quantity = floatval( $value['quantity'] );
                    $orgPrice = floatval( $value['data']->price );
                    $pp = ($value['price']);
                    $total = floatval($pp + $total_extra);
                    $value['data']->price = ( $orgPrice + $total );
                }       
    }}
add_action( 'woocommerce_before_calculate_totals', calculate_gift_wrap_fee', 1, 1 );