久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 WooCommerce 的最后對(duì)特定產(chǎn)品類別的購(gòu)物車項(xiàng)目

Sort specific product category cart items at the end in WooCommerce(在 WooCommerce 的最后對(duì)特定產(chǎn)品類別的購(gòu)物車項(xiàng)目進(jìn)行排序)
本文介紹了在 WooCommerce 的最后對(duì)特定產(chǎn)品類別的購(gòu)物車項(xiàng)目進(jìn)行排序的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

如何將定義的產(chǎn)品類別中的項(xiàng)目排序到購(gòu)物車訂單的末尾,例如,我希望屬于產(chǎn)品類別瓶子"的所有產(chǎn)品都位于購(gòu)物車訂單的末尾.

How to sort items from a defined product category to be at the end of a cart order, for example I want all products that belong to a product category 'bottle' to be at the end of the cart order.

我發(fā)現(xiàn)此代碼按價(jià)格排序,但希望按照上述方式進(jìn)行調(diào)整.

I have found this code that sorts by price, but would like to adjust to as described above.

add_action( 'woocommerce_cart_loaded_from_session', 'prefix_cart_order_prod_cat' );

function prefix_cart_order_prod_cat() {

    $products_in_cart = array();
    // Assign each product's price to its cart item key (to be used again later)
    foreach ( WC()->cart->cart_contents as $key => $item ) {
        $product = wc_get_product( $item['product_id'] );
        $products_in_cart[ $key ] = $product->get_price();
    }

    // SORTING - use one or the other two following lines:
    //asort( $products_in_cart ); // sort low to high
   arsort( $products_in_cart ); // sort high to low

    // Put sorted items back in cart
    $cart_contents = array();
    foreach ( $products_in_cart as $cart_key => $price ) {
       $cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
    }

    WC()->cart->cart_contents = $cart_contents;

}

推薦答案

您可以使用 has_term() WordPress 條件函數(shù) 用于檢查購(gòu)物車項(xiàng)目是否屬于產(chǎn)品類別(但需要為相關(guān)產(chǎn)品(項(xiàng)目)設(shè)置定義的類別).

You can use has_term() conditional WordPress function to check if cart items belongs to a product category (but the defined category(ies) need to be set for the related products (items)).

因此,以下代碼將對(duì)購(gòu)物車中的商品與屬于指定類別的商品進(jìn)行排序:

So the following code will sort cart items with items belonging to specified category(ies) at the end:

add_action( 'woocommerce_cart_loaded_from_session', 'product_category_cart_items_sorted_end' );
function product_category_cart_items_sorted_end() {
    $category_terms    = __('T-shirts'); // Here set your category terms (can be names, slugs or Ids)
    $items_in_category = $other_items = array(); // Initizlizing

    // Assign each item in a different array depending if it belongs to defined category terms or not
    foreach ( WC()->cart->cart_contents as $key => $item ) {
        if( has_term( $category_terms, 'product_cat', $item['product_id'] ) ) {
            $items_in_category[ $key ] = $item;
        } else {
            $other_items[ $key ] = $item;
        }
    }

    // Set back merged items arrays with the items that belongs to a category at the end
    WC()->cart->cart_contents = array_merge( $other_items, $items_in_category );
}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中.經(jīng)測(cè)試有效.

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

這篇關(guān)于在 WooCommerce 的最后對(duì)特定產(chǎn)品類別的購(gòu)物車項(xiàng)目進(jìn)行排序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗(yàn)證問題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品综合久久 | 草草网 | 国产成人综合网 | 91欧美 | 精品国产乱码久久久久久1区2区 | 国外成人在线视频网站 | 亚洲三级在线 | 久久国 | 免费成人高清 | 国产一区日韩在线 | 国产精品99久久久精品免费观看 | 精品日韩电影 | 亚洲一区二区不卡在线观看 | 国产精品久久久久久久免费大片 | 国产欧美一区二区久久性色99 | 美女福利网站 | 青青草在线播放 | 伊人无码高清 | 免费麻豆视频 | 一区二区中文 | 精品亚洲一区二区三区 | 91精品国产91久久综合桃花 | 美女视频一区二区三区 | 久久久久久国产精品免费免费男同 | av免费网站在线 | 久久精品一区二区 | 久久国产高清视频 | 久久精品二区 | 成人一区二区电影 | 日韩欧美成人一区二区三区 | 亚洲一区二区三区四区五区中文 | 国产视频二区在线观看 | 精品视频一区二区在线观看 | 日本精品视频一区二区 | 成年男女免费视频网站 | 99久久精品免费看国产免费软件 | 日韩一区二区av | 亚洲欧美日韩精品久久亚洲区 | 久久成人一区 | 97精品超碰一区二区三区 | 精品产国自在拍 |