简体   繁体   English

从 WooCommerce 中的父变量 product 设置订单项目永久链接

[英]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.

相关问题 如果产品类别有子项从 WooCommerce 中的父项中删除永久链接 - If product category has children remove permalink from parent term in WooCommerce 从WooCommerce产品永久链接中删除破折号/连字符 - Remove Dash/Hyphen From WooCommerce Product Permalink 首先在Woocommerce中清空可变产品的订单商品元数据 - Empty order item metadata for variable product at first in Woocommerce 在 WooCommerce 自定义订单历史记录中添加永久链接到产品标题 - Add permalink to product title in WooCommerce custom order history 如何在图像中添加产品永久链接标记woocommerce购物车项目? - How can add product permalink in image a tag woocommerce cart item? 以编程方式添加WooCommerce订单之前,设置可变产品价格 - Set Variable Product Price before adding in WooCommerce order programatically 显示Woocommerce电子邮件主题中订单商品的产品属性值 - Display product attribute value from order item in Woocommerce email subject 从 Woocommerce 父变量 SKU 生成产品变体 SKU - Generate the product variations SKUs from Woocommerce parent variable SKU 获取WooCommerce订单中每个项目的产品类别 - Get product category for every item of a WooCommerce order 按产品类别获取 WooCommerce 产品永久链接 - Get WooCommerce product permalink by product category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM