2016-06-28 124 views
1

我想顯示使用Advanced Custom Fields插件創建的自定義字段的值,同時在WooCommerce購物車和結帳頁面中顯示。WooCommerce ACF在購物車和結帳頁面上顯示自定義元數據頁面

我用下面的代碼在functions.php頁我的主題,只顯示該產品的單頁:

add_action('woocommerce_before_add_to_cart_button', 'add_custom_field', 0); 

function add_custom_field() { 
    global $post; 

    echo "<div class='produto-informacoes-complementares'>"; 
    echo get_field('info_complementar', $product_id, true); 
    echo "</div>"; 

    return true; 
} 

謝謝大家對先進給出的所有幫助,請原諒我的英語。

回答

3

我不能在你的網上商店進行測試,所以我不能完全肯定:

在單品頁(你的函數)顯示自定義字段值:

add_action('woocommerce_before_add_to_cart_button', 'add_custom_field', 0); 

function add_custom_field() { 
    global $product;    // Changed this 
    $product_id = $product->id; // And this 

    echo "<div class='produto-informacoes-complementares'>"; 
    echo get_field('info_complementar', $product_id, true); 
    echo "</div>"; 

    return true; 
} 

(更新)保存此自定義字段到購物車和會話:

add_action('woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2); 

function save_my_custom_product_field($cart_item_data, $product_id) { 

    $custom_field_value = get_field('info_complementar', $product_id, true); 

    if(!empty($custom_field_value)) 
    { 
     $cart_item_data['info_complementar'] = $custom_field_value; 

     // 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_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2); 

function render_meta_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['info_complementar'])) { 
     $custom_items[] = array("name" => "Info complementar", "value" => $cart_item['info_complementar']); 
    } 
    return $custom_items; 
} 

有沒有在最後2個鉤子的錯誤,是使這個不工作...現在它應該工作。

相關問題