Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

Filter product title for Woocommerce Order Items

I want wo add custom attribute for each product name in woocommerce order. Here is my code:

add_filter( 'woocommerce_order_get_items', 'filter_woocommerce_order_get_items', 10, 2 );
function filter_woocommerce_order_get_items($items, $instance){
    foreach ($items as $item_id => $item){
        $id = $item['product_id'];
        $brand = array_shift( wc_get_product_terms( $id, 'pa_brand', array( 'fields' => 'names' ) ) );
        if($brand) {
            $items[$item_id]['name'] = $item['name'] . ' [' . $brand . ']';
        }
    }
    return $items;
}

It looks normal for an old orders. But if it is a new order I've got such result:

enter image description here

Thanks for any advice!

question from:https://stackoverflow.com/questions/65939017/filter-product-title-for-woocommerce-order-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add Custom Attributes to Individual Products Sometimes it is important to add any custom attributes for an unique products as needed rather than going through the above-described procedure of setting pre-configured attributes. When you have different products with different attributes then you need to add custom attribute for individual products.

  • Go to WordPress dashboard
  • Select the specific products
  • Scroll down to the Product Data section and find attributes option
  • Click Attributes. The drop down menu for “Custom product attribute” is already selected. Click Add.
  • Name your attributes and add values
  • Save attributes For better understanding you can follow this tutorial

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...