久久久久久久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)
主站蜘蛛池模板: 午夜视频一区二区 | 免费的色网站 | 一级免费毛片 | 欧美日产国产成人免费图片 | 91xx在线观看 | 户外露出一区二区三区 | 国产精品久久久久久久久久久免费看 | 国内久久 | 国产一区影院 | 免费精品 | 国产成人精品亚洲日本在线观看 | 久久九精品 | 特黄色一级毛片 | 91九色porny首页最多播放 | 国产精品乱码一二三区的特点 | 日一区二区三区 | 福利视频网址 | 欧美日韩中文在线 | 欧美视频在线播放 | 一级看片免费视频囗交动图 | 精品久久久久久久久久 | 成人午夜高清 | 国产日韩欧美在线观看 | 国精产品一品二品国精在线观看 | 久久精品一区 | 少妇午夜一级艳片欧美精品 | 精品久久影院 | 国产精品免费小视频 | 在线观看 亚洲 | 日韩第一页 | 久久精品国产久精国产 | 久草青青 | 国产精品高潮呻吟久久 | 欧美自拍第一页 | 国产一级片免费在线观看 | 精品国产一区二区三区久久久蜜月 | 九九九视频在线观看 | 黄色片大全在线观看 | 色毛片| 国产线视频精品免费观看视频 | 最新国产福利在线 |