在 WooCommerce 中随处显示 ACF 产品自定义字段 3+ [英] Display ACF product custom fields everywhere in WooCommerce 3+
问题描述
我正在使用 WooCommerce 和高级自定义字段 (ACF) 插件构建网站.我创建了两个名称为植物"的 ACF 自定义字段.和金额".
我试图在前端显示它们,但目前只有自定义字段植物"处处显示.我一直在网上环顾四周,但没有运气.
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );函数 add_custom_field() {全球$产品;//改了这个//也添加了这个(与 WC +3 兼容)$product_id = method_exists( $product, 'get_id' ) ?$product->get_id() : $product->id;echo "<div class='produto-informacoes-complementares'>>echo get_field( '植物', $product_id );回声</div>";echo "<div class='produto-informacoes-complementares'>>echo get_field('amount', $product_id);回声</div>";返回真;}add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );函数 save_my_custom_product_field( $cart_item_data, $product_id ) {$custom_field_value = get_field( '植物', $product_id, true );$custom_field_value = get_field( 'amount', $product_id, true );if( !empty( $custom_field_value ) ){$cart_item_data['植物'] = $custom_field_value;$cart_item_data['amount'] = $custom_field_value;//下面的语句确保每个添加到购物车的操作都是唯一的订单项$cart_item_data['unique_key'] = md5( microtime().rand() );}返回 $cart_item_data;}add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2);函数 render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {$custom_items = array();//Woo 2.4.2 更新if( !empty( $cart_data ) ) {$custom_items = $cart_data;}if( isset( $cart_item['植物'] )) {$custom_items[] = array( "name" => "גודל עציץ", "value" => $cart_item['plant'] );$custom_items[] = array( "name" => "כמות במגש", "value" => $cart_item['amount'] );}返回 $custom_items;}函数 add_order_item_meta_acf( $item_id, $values ) {wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ '植物' ] );wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'amount' ] );}add_action('woocommerce_add_order_item_meta', 'add_order_item_meta_acf', 10, 2);
您的代码有点过时并且有一些错误.使用以下内容在任何地方显示产品 ACF 自定义字段(产品页面、购物车、结帐、订单和电子邮件通知):
//显示在产品页面上add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_single_product_pages', 1 );函数 display_acf_single_product_pages() {全球$产品;$plant = get_field('植物', $product->get_id());$amount = get_field('amount', $product->get_id());如果 ( !empty($plant) && !empty($amount) ) {echo '';如果(!空($植物)){echo ''.__(尺寸",woocommerce").'</strong>:'.$植物.'</div>';}如果(!空($金额)){echo ''.__(金额",woocommerce").'</strong>:'.金额.'</div>';}回声'</div>';}}//显示在购物车和结账上add_filter('woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2);函数 display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {$plant = get_field('植物', $cart_item['product_id']);$amount = get_field('amount', $cart_item['product_id']);如果(!空($植物)){$custom_items[] = array( "name" => __("Size", "woocommerce"), "value" => $plant );}如果(!空($金额)){$custom_items[] = array( "name" => __("Amount", "woocommerce"), "value" => $amount );}返回 $custom_items;}//在订单和电子邮件通知上显示(另存为自定义订单项元数据)add_action('woocommerce_checkout_create_order_line_item', 'display_acf_on_orders_and_emails', 10, 4);函数 display_acf_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {$plant = get_field( '植物', $values['product_id'] );$amount = get_field('amount', $values['product_id']);如果(!空($植物)){$item->add_meta_data( __("Size", "woocommerce"), $plant );}如果(!空($金额)){$item->add_meta_data( __("Amount", "woocommerce"), $amount);}}代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.
I am building a site using WooCommerce and Advanced Custom Fields (ACF) plugin. I've created two ACF custom fields with names "plant" and "amount".
I am trying to display them both on front end but currently only the custom field "plant" is displaying everywhere. I've been looking around the net with no luck.
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );
function add_custom_field() {
global $product; // Changed this
// Added this too (compatibility with WC +3)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'plant', $product_id );
echo "</div>";
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'amount', $product_id );
echo "</div>";
return true;
}
add_filter( '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( 'plant', $product_id, true );
$custom_field_value = get_field( 'amount', $product_id, true );
if( !empty( $custom_field_value ) )
{
$cart_item_data['plant'] = $custom_field_value;
$cart_item_data['amount'] = $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['plant'] )) {
$custom_items[] = array( "name" => "גודל עציץ", "value" => $cart_item['plant'] );
$custom_items[] = array( "name" => "כמות במגש", "value" => $cart_item['amount'] );
}
return $custom_items;
}
function add_order_item_meta_acf( $item_id, $values ) {
wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'plant' ] );
wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'amount' ] );
}
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);
解决方案 Your code is a bit outdated and with some mistakes. Use the following to display product ACF custom fields everywhere (product page, cart, checkout, orders and email notifications):
// Display on product page
add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_single_product_pages', 1 );
function display_acf_single_product_pages() {
global $product;
$plant = get_field( 'plant', $product->get_id() );
$amount = get_field( 'amount', $product->get_id() );
if ( ! empty($plant) && ! empty($amount) ) {
echo '<div class="produto-informacoes-complementares">';
if ( ! empty($plant) ) {
echo '<div class="plant"><strong>' . __("Size", "woocommerce") . '</strong>: ' . $plant . '</div>';
}
if ( ! empty($amount) ) {
echo '<div class="amount"><strong>' . __("Amount", "woocommerce") . '</strong>: ' . $amount . '</div>';
}
echo '</div>';
}
}
// Display on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
$plant = get_field( 'plant', $cart_item['product_id'] );
$amount = get_field( 'amount', $cart_item['product_id'] );
if ( ! empty($plant) ) {
$custom_items[] = array( "name" => __("Size", "woocommerce"), "value" => $plant );
}
if ( ! empty($amount) ) {
$custom_items[] = array( "name" => __("Amount", "woocommerce"), "value" => $amount );
}
return $custom_items;
}
// Display on orders and email notifications (save as custom order item meta data)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_acf_on_orders_and_emails', 10, 4 );
function display_acf_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
$plant = get_field( 'plant', $values['product_id'] );
$amount = get_field( 'amount', $values['product_id'] );
if ( ! empty($plant) ) {
$item->add_meta_data( __("Size", "woocommerce"), $plant );
}
if ( ! empty($amount) ) {
$item->add_meta_data( __("Amount", "woocommerce"), $amount );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
这篇关于在 WooCommerce 中随处显示 ACF 产品自定义字段 3+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文