2016-08-30 64 views
5

我在產品頁面的常規設置選項卡的WooCommerce Admin中創建了一個自定義字段,以便插入一些製造日期。我想在購物車上顯示此自定義字段值,並在每個產品的名稱上方顯示結帳頁面。在購物車和結帳中顯示的管理產品頁面自定義字段

這裏是我的代碼:

// Insert a Custom Admin Field 
function woo_add_custom_general_fields() { 

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

    woocommerce_wp_text_input(array( 
     'id'    => 'days_manufacture', 
     'label'    => __('Days for Manufacture', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Insert here', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => 
     array(
      'step' => 'any', 
      'min' => '1' 
     ) 
    )); 
    echo '</div>'; 
} 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 


// Save field 
function woo_add_custom_general_fields_save($post_id){ 
    $woocommerce_number_field = $_POST['days_manufacture']; 
    if(!empty($woocommerce_number_field)) 
     update_post_meta($post_id, 'days_manufacture', esc_attr($woocommerce_number_field)); 
} 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 


function save_days_field($cart_item_data, $product_id) { 
    if(isset($_REQUEST['days_manufacture'])) { 
     $cart_item_data[ 'days_manufacture' ] = get_post_meta($post->ID, 'days_manufacture',true); 

/* below statement make sure every add to cart action as unique line item */ 

    $cart_item_data['unique_key'] = md5(microtime().rand()); 
    } 
    return $cart_item_data; 
} 
add_action('woocommerce_add_cart_item_data', 'save_days_field', 10, 2); 


function render_meta_on_cart_and_checkout($cart_data, $cart_item = null) { 
    $custom_items = array(); 
    /* Woo 2.4.2 updates */ 
    if(!empty($cart_data)) { 
     $custom_items = $cart_data; 
    } 
    if(isset($cart_item['days_manufacture'])) { 
     $custom_items[] = array("name" => 'Days:', "value" => $cart_item['days_manufacture']); 
    } 
    return $custom_items; 
} 
add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2); 

但這不是工作,我不能顯示在購物車和結帳頁面自定義字段值。

我該如何做到這一點?

感謝

+0

優秀的,它的工作完美。你是個天才。非常感謝你!還有一個問題,如果我現在需要在我的產品屏幕上顯示價值,那麼我現在不再使用.get_post_meta了。 –

回答

3

我已經測試你的代碼,並糾正了一些部分,使該產品自定義字段出現在購物車和結帳頁面。

下面是更正後的代碼:

// Insert a Custom Admin Field 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 

function woo_add_custom_general_fields() { 

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

    woocommerce_wp_text_input(array(
     'id'    => 'days_manufacture', 
     'label'    => __('Days for Manufacture', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Insert here', 'woocommerce'), 
     'type'    => 'number', 
     'custom_attributes' => array(
      'step' => 'any', 
      'min' => '1' 
     ), 
    )); 

    echo '</div>'; 
} 


// Save the field 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

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


// Store custom field 
add_action('woocommerce_add_cart_item_data', 'save_days_field', 10, 2); 

function save_days_field($cart_item_data, $product_id) { 
    $special_item = get_post_meta($product_id , 'days_manufacture',true); 
    if(!empty($special_item)) { 
     $cart_item_data[ 'days_manufacture' ] = $special_item; 

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


// Render meta on cart and checkout 
add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2); 

function rendering_meta_field_on_cart_and_checkout($cart_data, $cart_item) { 
    $custom_items = array(); 
    // Woo 2.4.2 updates 
    if(!empty($cart_data)) { 
     $custom_items = $cart_data; 
    } 
    if(isset($cart_item['days_manufacture'])) { 
     $custom_items[] = array("name" => __("Days", "woocommerce"), "value" => $cart_item['days_manufacture']); 
    } 
    return $custom_items; 
} 

當然,這正好在你的活躍兒童主題(或主題)的function.php文件或也以任何插件文件。

該代碼已經過測試並且可以正常工作。

參考:WooCommerce : Add custom Metabox to admin order page

相關問題