2017-04-17 64 views
2

在WooCommerce中,我有一個自定義產品字段「時間」我希望在結帳時輸出該字段的值,特別是在產品詳細信息中的產品名稱下,像這樣:Event time: (value from wcv_custom_product_field)在Woocommerce結賬頁上添加其他產品信息

我試過配售:

add_filter('woocommerce_get_item_data', 'wc_checkout_producttime', 10, 2); 

function wc_checkout_producttime($other_data, $cart_item) 
{ 
    $_product = $cart_item['data']; 

    $other_data[] = array('name' => 'wcv_custom_product_field', 'value' => $_product->get_wcv_custom_product_field()); 
return $other_data; 
} 

但我發現了在結賬ablank頁面。

我在做什麼錯了,我該如何解決這個問題?

謝謝。

回答

2

這裏是woocommerce_get_item_data過濾鉤子鉤住一個自定義函數,將在車和結算的項目展示您的產品自定義字段:

add_filter('woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2); 
function display_custom_product_field_data($cart_data, $cart_item) { 

    // Define HERE your product custom field meta key <== <== <== <== <== 
    $meta_key = 'wcv_custom_product_field'; 

    $product_id = $cart_item['product_id']; 

    $meta_value = get_post_meta($product_id, $meta_key, true); 

    if(!empty($cart_data)) 
     $custom_items = $cart_data; 

    if(!empty($meta_value)) { 
     $custom_items[] = array(
      'key'  => __('Event time', 'woocommerce'), 
      'value'  => $meta_value, 
      'display' => $meta_value, 
     ); 
    } 
    return $custom_items; 
} 

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

此代碼已經過測試並可正常工作。

相關問題