且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Woocommerce订单和电子邮件通知中显示产品品牌和名称

更新时间:2023-11-30 08:29:46

我已经重新访问了您的问题代码,并添加了一些其他功能以在订购"页面和电子邮件通知上显示产品品牌:

I have revisited your question code and added some additional functions to display the product brand on Order pages and on email notifications:

// Utility: Get the product brand term names (from the product ID)
function wc_get_product_brand( $product_id ) {
   return implode(', ', wp_get_post_terms($product_id, 'pwb-brand', ['fields' => 'names']));
}

// Display product brand in Cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];          // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $cart_item['product_id'] ) ) {
        if ( is_cart() )
            return sprintf('<a href="%s">%s %s</a>', esc_url($permalink), $brand, $product->get_name());
        else
            return  $brand . ' ' . $product_name;
    }
    return $product_name;
}

// Display product brand in order pages and email notification
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product();        // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $item->get_product_id() ) ) {
        if ( is_wc_endpoint_url() )
            return sprintf('<a href="%s">%s %s</a>', esc_url($permalink), $brand, $product->get_name());
        else
            return  $brand . ' ' . $product_name;
    }
    return $product_name;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.