2015-10-18 113 views
0

我有一個關於在WooCommerce的購物車中計數的問題。 我想爲每個產品添加一個處理費用字段,並大幅計算購物車中的費用總額。根據我的研究,我在我的產品中創造了一個領域。 Demo-1如何在WooCommerce購物車中將自定義字段計爲額外費用?

我的下一步是在我的購物車中計算此字段。 我也在Google中搜索過這個問題,但我只能找到一些解決方案(Wordpress: Add extra fee in cart)來計算固定費用,而不是一個戲劇性的功能。
Demo-2

// Display Fields 
    add_action('woocommerce_product_options_general_product_data',  'woo_add_custom_general_fields'); 

    // Save Fields 
    add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

    function woo_add_custom_general_fields() { 

    global $woocommerce, $post; 

    echo '<div class="options_group">'; 

    // Custom fields will be created here... 

    woocommerce_wp_text_input( 
    array( 
     'id'    => '_number_field', 
     'label'    => __('Environmental fee', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Enter the custom value here.', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
    ) 
); 

    echo '</div>'; 

    } 


    function woo_add_custom_general_fields_save($post_id){ 


    // Number Field 
    $woocommerce_number_field = $_POST['_number_field']; 
    if(!empty($woocommerce_number_field)) 
     update_post_meta($post_id, '_number_field', esc_attr($woocommerce_number_field)); 


    } 


    add_action('woocommerce_cart_calculate_fees','endo_handling_fee'); 
    function endo_handling_fee() { 
     global $woocommerce; 

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

     $fee = 5.00; 
     $woocommerce->cart->add_fee('Handling', $fee, true, 'standard'); 
    } 

如何修改函數來計算每個產品的費用,由我創建的自定義字段中提供哪些價值,在小計列?

現在,我正在嘗試下面的代碼。 我相信關鍵是如何抓住產品的價值,並將價值作爲一個變量。

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees'); 
function add_custom_fees(WC_Cart $cart){ 
$fees = 0; 

foreach($cart->get_cart() as $item){ 
    $fees += $item[ 'quantity' ] * 0.08; 
} 

if($fees != 0){ 
    $cart->add_fee('Handling fee', $fees); 
} 
} 

回答

2

您需要的函數是get_post_meta來獲取自定義字段的值。

$prod_fee = get_post_meta($item['product_id'] , '_number_field', true); 

然後,您可以累積並將其顯示爲合併費用。

+0

嗨安菲利普, 感謝您的回覆, 我試過你的例子。 它不起作用,它似乎沒有從產品中獲得價值。 $ prod_fee = get_post_meta($ item ['product_id'],'_number_field',true); $ prod_fee = 5; //此示例正常工作.. – Allen

+0

您是如何實施解決方案的? @Mitul的答案是錯誤的,因爲$ item ['product_id']超出了foreach的範圍,將$ prod_fee放在foreach塊中。 – Anfelipe

+0

感謝您的回覆。它終於工作得很好。 – Allen

0

你需要從產品的元數據產品的費用爲@Anfelipe給出了代碼

$prod_fee = get_post_meta($item['product_id'] , '_number_field', true);

之後,你需要在添加條件或做計算。

function add_custom_fees(WC_Cart $cart){ 
    $fees = 0; 
    $prod_fee = get_post_meta($item['product_id'] , '_number_field', true); 
    foreach($cart->get_cart() as $item){ 
     $fees += $item[ 'quantity' ] * $prod_fee ; 
    } 
    if($fees != 0){ 
     $cart->add_fee('Handling fee', $fees); 
    } 
} 
+0

嗨Mitul, 感謝您的答覆。 我已將您的示例添加到我的項目中。 但是,在我的購物小計專欄中並不是這樣。 我也嘗試使用另一個字段,例如「_sale_price」,以便變量。 結果是一樣的。 你可能知道哪裏錯了嗎? 再次感謝。 – Allen

+0

嗨,Mitul,謝謝你的例子。它激勵我達到我想要的功能。 – Allen

相關問題