2016-02-01 95 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);