Ros*_*oss 4 php wordpress woocommerce advanced-custom-fields woocommerce-theming
我在 WooCommerce 产品上为帖子类型设置了高级自定义字段。因此每个产品都有 1 个独特的自定义字段。
我试图在购物车上的产品名称以及结账页面和订单表信息之后显示自定义字段。
但是,由于我的代码不显示任何输出,因此遇到了问题。
任何有关如何实现这一目标的建议将不胜感激。谢谢
// Display the ACF custom field 'location' after the product title on cart / checkout page.
function cart_item_custom_feild( $cart_item ) {
$address = get_field( 'location', $cart_item['product_id'] );
echo "<div>Address: $address.</div>";
}
add_action( 'woocommerce_after_cart_item_name', 'cart_item_custom_feild', 10, 1 );
Run Code Online (Sandbox Code Playgroud)
我也尝试过the_field
而不是get_field
如果您需要在购物车页面和结账页面上的订单审核表上运行它,您可以使用woocommerce_cart_item_name
过滤器挂钩,如下所示:
add_filter('woocommerce_cart_item_name', 'order_review_custom_field', 999, 3);
function order_review_custom_field($product_name, $cart_item, $cart_item_key)
{
$address = get_field('location', $cart_item['product_id']);
return ($address) ?
$product_name . '<div>Address: ' . $address . '</div>'
:
$product_name . '<div>Address: No address found!</div>';
}
Run Code Online (Sandbox Code Playgroud)
这是购物车页面上的结果:
在结账页面的订单审核表上:
我们可以使用woocommerce_order_item_meta_end
操作挂钩将自定义字段值附加到电子邮件模板上产品名称的末尾:
add_action("woocommerce_order_item_meta_end", "email_order_custom_field", 999, 4);
function email_order_custom_field($item_id, $item, $order, $plain_text)
{
$address = get_field('location', $item->get_product_id());
echo ($address) ?
'<div>Address: ' . $address . '</div>'
:
'<div>Address: No address found!</div>';
};
Run Code Online (Sandbox Code Playgroud)
这是电子邮件中的结果:
在感谢页面的订单详细信息表中:
这个答案已经在 woocommerce 上经过充分测试5.7.1
并且有效。