2017-02-04 25 views
1

WooCommerceでは、私はthis code from Remicorsonを使用していますが、動作していないため、どこに問題があるのか​​わかりません。ここでWooCommerceのカスタムフィールドの値をカートに追加する合計

は私のコードです:

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

function woo_add_cart_fee() 
{ 
    global $woocommerce; 
    $prod_fee = get_post_meta($item['product_id'], '_number_field', true); 

    // After that you need to add condition or do calculation in. 

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

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

add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee'); 

答えて

1

バックエンドの製品ページにカスタムフィールドを作成するためのあなたのコードおよびカスタムフィールドデータが動作していることを保存します。私はちょうどあなたがコードの中で、いくつかの説明を追加するためにそれを少しカスタマイズしている:

// Create and display Backend Product custom field 
add_action('woocommerce_product_options_general_product_data', 'ba_adding_custom_product_general_field'); 
function ba_adding_custom_product_general_field() 
{ 
    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>'; 
} 


// Save the submited data from Backend Product custom field 
add_action('woocommerce_process_product_meta', 'ba_saving_custom_product_general_field'); 
function ba_saving_custom_product_general_field($post_id) 
{ 

    // Get the submitted value 
    $woocommerce_number_field = $_POST['_number_field']; 

    // Create/Update the submitted value in postmeta table for this product 
    if (!empty($woocommerce_number_field)) 
     update_post_meta($post_id, '_number_field', esc_attr($woocommerce_number_field)); 
} 

そして、あなたは(カスタムフィールド「環境の手数料」と)あなたの管理製品ページでこれを取得する:

enter image description here

問題あなたは、その関数で同様の機能を埋め込むてきたように、あなたのwoo_add_cart_fee()から来る、ので、これはしません決定的に働きます。あなたがそのカスタムフィールドの計算に基づいて、カートフィー追加するには、正しい作業コードでしょう下記

:(!$料= 0)場合

// Adding a custom fee to cart based on a product custom field value calculation 
add_action('woocommerce_cart_calculate_fees', 'ba_custom_cart_fee'); 
function ba_custom_cart_fee($cart_object){ 

    // Initializing variables 
    $fee = 0; 

    // Iterating through each cart items 
    foreach($cart_object->get_cart() as $cart_item){ 

     $item_id = $cart_item['product_id']; // Item Id or product ID 
     $item_qty = $cart_item['quantity']; // Item Quantity 

     // Getting the corresponding product custom field value 
     $item_fee = get_post_meta($item_id, '_number_field', true); 

     // Adding the calculation to the fee 
     $fee += $item_qty * $item_fee; 
    } 

    if ($fees != 0) 
     $cart_object->add_fee(__('Handling fee', 'woocommerce'), $fee, true, 'standard'); 

} 
+1

をその$料Sなし... おかげで@ LoicTheAztecその作業は完全に.. –

関連する問題