2016-06-28 44 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个钩子的错误,是使这个不工作...现在它应该工作。

相关问题