作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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 );
我还尝试了 the_field
而不是 get_field
最佳答案
如果您需要在结帐页面的购物车页面和订单评论表上运行它,您可以使用woocommerce_cart_item_name
过滤器钩子(Hook),如下所示:
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>';
}
这是购物车页面上的结果:
在结账页面的订单审核表:
我们可以使用 woocommerce_order_item_meta_end
操作 Hook 将自定义字段值附加到电子邮件模板上产品名称的末尾:
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>';
};
这是电子邮件中的结果:
在感谢页面的订单详情表中:
这个答案已经在 woocommerce 5.7.1
上进行了全面测试并且有效。
关于php - 如何在 WooCommerce 中的购物车和订单评论中的产品名称后显示 ACF 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70580839/
我是一名优秀的程序员,十分优秀!