2017-07-26 71 views
3

我已經建立了使用woocommerce_before_add_to_cart_button鉤一個隱藏的輸入項WooCommerce價格中重​​寫不工作

function add_gift_wrap_field() { 
    ?>`<input type="hidden" id="price_val" name="added_price" value="100.34">`<?php 
} 
add_action('woocommerce_before_add_to_cart_button', 'add_gift_wrap_field'); 

保存對產品領域:

function save_gift_wrap_fee($cart_item_data, $product_id) { 

    if(isset($_POST['added_price'])) { 

     $cart_item_data = array(); 

     $cart_item_data[ "gift_wrap_fee" ] = "YES"; 
     $cart_item_data[ "gift_wrap_price" ] = 100;  
    } 
    return $cart_item_data; 

} 
add_filter('woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2); 

現在,我可以附和了這裏面的$_POST['added_price']woocommerce_before_calculate_totals鉤。

我編寫的代碼如下所示:

function calculate_gift_wrap_fee($cart_object) { 
    if(!WC()->session->__isset("reload_checkout")) { 
     /* Gift wrap price */ 
     $additionalPrice = number_format($_POST['added_price'], 2); 

     $additionalPrice = floatval($additionalPrice); 
     //echo $additionalPrice; exit(); shows the value 
     foreach ($cart_object->cart_contents as $key => $value) { 
      if(isset($value["gift_wrap_fee"])) { 
        /* Woocommerce 3.0 + */ 
        $orgPrice = floatval($value['data']->get_price()); 
        $value['data']->set_price($orgPrice + $additionalPrice); //not adding the $additionalPrice here.  
      } 
     } 
    } 
} 
add_action('woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99); 

什麼我錯在這裏做什麼?

+1

我剛剛加入:) –

+1

謝謝:) ...爲什麼你不添加到購物車對象''added_price''在同一時間? – LoicTheAztec

+0

試過但實際上沒有工作 –

回答

2

這是基於您的代碼問題的完整清潔,測試和工作解決方案。

在這裏,我已經包括您的自定義字段中的相關車項目的車對象key/value,而不是你的calculate_gift_wrap_fee()功能獲得這個職位的價值。

這樣就不可能鬆開自定義字段值,新的價格計算是可靠的。

我已評論'gift_wrap_fee''gift_wrap_price',因爲它們現在不是真的需要(但是如果你喜歡,你可以取消註釋)。

下面是該代碼:

// The hidden product custom field 
add_action('woocommerce_before_add_to_cart_button', 'add_gift_wrap_field'); 
function add_gift_wrap_field() { 
    ?> 
     <input type="hidden" id="price_val" name="added_price" value="100.34"> 
    <?php 
} 

// Adding the custom field to as custom data for this cart item in the cart object 
add_action('woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2); 
function save_custom_fields_data_to_cart($cart_item_data, $product_id) { 
    $bool = false; 
    $data = array(); 
    if(! empty($_REQUEST['added_price'])) { 
     $cart_item_data['custom_data']['added_price'] = $_REQUEST['added_price']; 
     // Below this 2 values are not really needed (I think) 
     // (You can uncomment them if needed) 

     ## $cart_item_data['custom_data']['gift_wrap_fee'] = 'YES'; 
     ## $cart_item_data['custom_data']['gift_wrap_price'] = 100; 

     // below statement make sure every add to cart action as unique line item 
     $cart_item_data['custom_data']['unique_key'] = md5(microtime().rand()); 
     WC()->session->set('custom_data', $data); 
    } 
    return $cart_item_data; 
} 

// Changing the cart item price based on custom field calculation 
add_action('woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 10, 1); 
function calculate_gift_wrap_fee($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Iterating though cart items 
    foreach ($cart_object->get_cart() as $cart_item) { 
     // Continue if we get the custom data for the current cart item 
     if(! empty($cart_item['custom_data'])){ 
      // Get the custom field "added price" value 
      $added_price = number_format($cart_item['custom_data']['added_price'], 2); 
      // The WC_Product object 
      $wc_product = $cart_item['data']; 
      // Get the price (WooCommerce versions 2.5.x to 3+) 
      $product_price = method_exists($wc_product, 'get_price') ? floatval($wc_product->get_price()) : floatval($wc_product->price); 
      // New price calculation 
      $new_price = $product_price + $added_price; 
      // Set the calculeted price (WooCommerce versions 2.5.x to 3+) 
      method_exists($wc_product, 'set_price') ? $wc_product->set_price($new_price) : $wc_product->price = $new_price; 
     } 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

此代碼已經過測試,適用於從2.5.x到3+的WooCommerce版本。