2016-02-01 68 views
0

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

例如:530 正常价格+附加价格:$ 180 + $ 10 总计:$ 190

  • 词数:1040 普通1.字数价格+附加价格:180 $ + 20 $ 总计:200 $
  • 此过程为创建动态输入表格客户计算金额的价格和总金额。

    `$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; 
    }` 
    
    +0

    请你分享一下你的代码,以便我们可以帮助你! –

    +0

    这是我尝试嵌入到woocommerce中的代码 – nishanthi

    回答

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