pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 WooCommerce 的最后對特定產品類別的購物車項目

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

問題描述

限時送ChatGPT賬號..

如何將定義的產品類別中的項目排序到購物車訂單的末尾,例如,我希望屬于產品類別瓶子"的所有產品都位于購物車訂單的末尾.

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.

我發現此代碼按價格排序,但希望按照上述方式進行調整.

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 條件函數 用于檢查購物車項目是否屬于產品類別(但需要為相關產品(項目)設置定義的類別).

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)).

因此,以下代碼將對購物車中的商品與屬于指定類別的商品進行排序:

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 );
}

代碼位于活動子主題(或活動主題)的functions.php 文件中.經測試有效.

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

這篇關于在 WooCommerce 的最后對特定產品類別的購物車項目進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 电主轴-高速精密电主轴-高速电机厂家-瑞德沃斯品牌有限公司 | 电动手术床,医用护理床,led手术无影灯-曲阜明辉医疗设备有限公司 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸| 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 消泡剂_水处理消泡剂_切削液消泡剂_涂料消泡剂_有机硅消泡剂_广州中万新材料生产厂家 | POS机办理_个人pos机免费领取-银联pos机申请首页 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 无锡网站建设-做网站-建网站-网页设计制作-阿凡达建站公司 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 烽火安全网_加密软件、神盾软件官网 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 | CE认证_FCC认证_CCC认证_MFI认证_UN38.3认证-微测检测 CNAS实验室 | 代做标书-代写标书-专业标书文件编辑-「深圳卓越创兴公司」 | 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 选宝石船-陆地水上开采「精选」色选机械设备-青州冠诚重工机械有限公司 | 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 变压器配件,变压器吸湿器,武强县吉口变压器配件有限公司 | b2b网站大全,b2b网站排名,找b2b网站就上地球网 | 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 圈酒招商网【jiushuitv.com】_酒水招商_代理_加盟平台 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 馋嘴餐饮网_餐饮加盟店火爆好项目_餐饮连锁品牌加盟指南创业平台 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 换链神器官网-友情链接交换、购买交易于一体的站长平台 | 陕西视频监控,智能安防监控,安防系统-西安鑫安5A安防工程公司 | 黄石东方妇产医院_黄石妇科医院哪家好_黄石无痛人流医院 | 屏蔽泵厂家,化工屏蔽泵_维修-淄博泵业| 步进驱动器「一体化」步进电机品牌厂家-一体式步进驱动 |