[英]Set order item permalink from the parent variable product in WooCommerce
On the thank you page and in order emails, the permalink for variable products always links directly to the product variation instead of the parent product itself, eg, https://mystore.com/some-product/?attribute_color=red .在感谢页面和订单电子邮件中,可变产品的永久链接始终直接链接到产品变体而不是父产品本身,例如https://mystore.com/some-product/?attribute_color=red 。 I need the permalink to reflect the product and not the variation, eg https://mystore.com/some-product/ .
我需要永久链接来反映产品而不是变化,例如https://mystore.com/some-product/ 。
I've tried the following:我尝试了以下方法:
$parent_id = $product->get_parent_id();
$slug = $product->get_permalink($parent_id);
The variable变量
$parent_id
returns correctly, but正确返回,但是
$slug
is always the variation permalink.始终是变化固定链接。 What am I missing?
我错过了什么? Alternatively, I've tried to retrieve the post name of the parent like so
或者,我尝试像这样检索父母的帖子名称
$parent_id = $product->get_parent_id();
$slug = $product->get_post_name($parent_id);
but this throws an error and the thank you page only partially renders.但这会引发错误,并且感谢页面仅部分呈现。
You don't need to override any template file, just use the following hooked function, to replace the product variation permalink, by the parent variable product permalink on all orders:您不需要覆盖任何模板文件,只需使用以下钩子 function,将产品变体永久链接替换为所有订单上的父变量产品永久链接:
add_filter( 'woocommerce_order_item_permalink', 'filter_order_item_permalink_callback', 10, 3 );
function filter_order_item_permalink_callback( $product_permalink, $item, $order ) {
// For product variations
if( $item->get_variation_id() > 0 ){
$product = $item->get_product();
$is_visible = $product && $product->is_visible();
// Get the instance of the parent variable product Object
$parent_product = wc_get_product( $item->get_product_id() );
// Return the parent product permalink (if product is visible)
return $is_visible ? $parent_product->get_permalink() : '';
}
return $product_permalink;
}
Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and work.
测试和工作。
For email notifications对于 email 通知
WooCommerce doesn't display, by default, the product permalink in the email notifications… WooCommerce 默认不显示 email 通知中的产品永久链接...
To display the product permalink on email notifications, use the following:要在 email 通知上显示产品永久链接,请使用以下命令:
add_filter( 'woocommerce_order_item_name', 'filter_order_item_name_callback', 10, 3 );
function filter_order_item_name_callback( $item_name, $item, $is_visible ) {
// On emails notifications only
if( ! is_wc_endpoint_url() > 0 ) {
$product = $item->get_product();
// For product variation type
if( $item->get_variation_id() > 0 ){
// Get the instance of the parent variable product Object
$parent_product = wc_get_product( $item->get_product_id() );
// The parent product permalink (if product is visible)
$product_permalink = $parent_product->get_permalink();
}
// For other item (product) type
else {
$product_permalink = $product->get_permalink();
}
return sprintf( '<a href="%s">%s</a>', $product_permalink, $item_name );
}
return $item_name;
}
Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and work.
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.