在WooCommerce 3+中到处显示ACF产品自定义字段



我正在使用WooCommerce和高级自定义字段(ACF(插件构建一个网站。我创建了两个ACF自定义字段;植物";以及";金额";。

我试图在前端同时显示它们,但目前只显示自定义字段";植物";正在到处显示。我一直在网上找来找去,但运气不好。

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);

您的代码有点过时,并且有一些错误。使用以下内容在各处显示产品ACF自定义字段(产品页面、购物车、结账、订单和电子邮件通知(:

// 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 );
}
}

代码位于活动子主题(或活动主题(的functions.php文件中。测试并工作。

最新更新