簡體   English   中英

從 WooCommerce 中的父變量 product 設置訂單項目永久鏈接

[英]Set order item permalink from the parent variable product in WooCommerce

在感謝頁面和訂單電子郵件中,可變產品的永久鏈接始終直接鏈接到產品變體而不是父產品本身,例如https://mystore.com/some-product/?attribute_color=red 我需要永久鏈接來反映產品而不是變化,例如https://mystore.com/some-product/

我嘗試了以下方法:

$parent_id = $product->get_parent_id();
$slug = $product->get_permalink($parent_id);

變量

$parent_id

正確返回,但是

$slug

始終是變化固定鏈接。 我錯過了什么? 或者,我嘗試像這樣檢索父母的帖子名稱

$parent_id = $product->get_parent_id();
$slug = $product->get_post_name($parent_id);

但這會引發錯誤,並且感謝頁面僅部分呈現。

您不需要覆蓋任何模板文件,只需使用以下鈎子 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;
}

代碼進入您的活動子主題(或活動主題)的functions.php 文件。 測試和工作。


對於 email 通知

WooCommerce 默認不顯示 email 通知中的產品永久鏈接...

要在 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;
}

代碼進入您的活動子主題(或活動主題)的functions.php 文件。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM