gpt4 book ai didi

php - 如何在 WooCommerce 中的购物车和订单评论中的产品名称后显示 ACF 字段?

转载 作者:行者123 更新时间:2023-12-02 18:23:16 24 4
gpt4 key购买 nike

我在 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

最佳答案

1- 在购物车页面和结帐页面的订单查看表上

如果您需要在结帐页面的购物车页面订单评论表上运行它,您可以使用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>';

}

这是购物车页面上的结果:

enter image description here

结账页面的订单审核表:

enter image description here


2- 在电子邮件和致谢页面的订单详情表中:

我们可以使用 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>';
};

这是电子邮件中的结果:

enter image description here

在感谢页面的订单详情表中:

enter image description here


这个答案已经在 woocommerce 5.7.1 上进行了全面测试并且有效。

关于php - 如何在 WooCommerce 中的购物车和订单评论中的产品名称后显示 ACF 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70580839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com