2014-12-26 1 views
1

woocommerce에서 사용자 지정 메타 보톡을 추가하려고합니다. 그것은 완벽하게 추가되었습니다. 내 궁극적 인 목표는 함수에서 해당 사용자 정의 필드를 호출하고 cart.php에 표시하는 것입니다. 그래서 코딩 :woocommerce에서 get_post_meta로 출력 오류가 발생했습니다.

사용자 정의 필드에 대한 : [I이 점

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">'; 

     woocommerce_wp_text_input( 
     array( 
     'id' => 'number_field', 
     'label' => __('<strong style="color:#239804">Your Free Products</strong>', 'woocommerce'), 
     'placeholder' => '', 
     'description' => __('Please enter a number', 'woocommerce'), 
     'type' => 'number', 
     'custom_attributes' => array(
     'step' => 'any', 
     'min' => '0' 
       ) 
     ) 
     ); 

     echo '</div>'; 

     }//woo_add_custom_general_fields 

     function woo_add_custom_general_fields_save($post_id){ 
     $woocommerce_number_field = $_POST['number_field']; 
     if(!empty($woocommerce_number_field)) 
     update_post_meta($post_id, 'number_field', esc_attr($woocommerce_number_field)); 

     }//woo_add_custom_general_fields_save($post_id) 

그것은 완벽하게 제품 관리자 페이지에 장착 한에 http://www.remicorson.com/mastering-woocommerce-products-custom-fields/]을 참조합니다. 나는 내 cart.php에서 cart.php

function free_products(){ 

global $woocommerce ,$product, $post; 

foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

$free_number = get_post_meta($post->ID, 'number_field', true); 

$free_product = $cart_item['quantity'] * $free_number; 

echo apply_filters('woocommerce_cart_item_quantity', $free_product, $cart_item_key);  
     } 

    } 

에 대한 카운터를 만드는 오전 어디 출력은 프런트 엔드에서 0이

<td class="product-quantity"> 
    <?php 

    echo free_products(); 
     ?> 
    </td> 

을 추가 할 때 지금은 다른 기능을 만드는 오전. 누구든지 내가 잘못하고있는 것을 도와 줄 수 있습니까? 미리 감사드립니다. 코드 아래

답변

2

시도 :

function free_products(){ 

    global $woocommerce ,$product, $post; 

    foreach (WC()->cart->get_cart() as $cart_item) { 
    $my_var = $cart_item['product_id']; 
    $free_number = get_post_meta($my_var, 'number_field', true); 


    $free_product = $cart_item['quantity'] * $free_number; 

    echo apply_filters('woocommerce_cart_item_quantity', $free_product);  
      } 

     } 

그것은 당신이나 나를 위해 일 not.Its을 위해 작동하는지 알려주세요.

+0

덕분에 @Rohil_PHPBeginner가 작동했습니다. 이제 실제로 product_id를 오인하고 있습니다. – amitabhaghosh197

+0

문제는 데이터를 가져 오기 위해 게시 ID가 가져 오지 못했습니다. 제품 ID와 게시 ID가 모두 동일하므로 제품 ID를 지정했습니다. –

+0

설명해 주셔서 감사합니다. – amitabhaghosh197

관련 문제