亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按產品 ID 對購物車 WooCommerce 中產品列表底部的產品進行排序

按產品 ID 對購物車 WooCommerce 中產品列表底部的產品進行排序

PHP
肥皂起泡泡 2022-11-12 13:22:50
在 WooCommerce 中,我使用代碼在將任何菜品添加到購物車時自動添加包裝。功能如下:菜品在購物車中,添加了1個飯盒菜品在購物車中,增加了2個飯盒菜在購物車里,增加了3個飯盒有 3 個飯盒,所以現在添加 1 個包裹function add_delivery_charge_to_cart( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )        return;    $lunchbox_id  = 5737; // "LunchBox" to be added to cart    $pakket_id = 5738; // "Pakket" to be added to cart    // Loop through cart items    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {        // Check if "LunchBox" product is already in cart        if( $cart_item['data']->get_id() == $lunchbox_id ) {            $lunchbox_key = $cart_item_key;            $lunchbox_qty = $cart_item['quantity'];        }        // Check if "Pakket" product is already in cart        if( $cart_item['data']->get_id() == $pakket_id ) {            $pakket_key = $cart_item_key;            $pakket_qty = $cart_item['quantity'];        }           }    // Get total items in cart, counts number of products and quantity per product    $total_items_in_cart = WC()->cart->get_cart_contents_count();    // If product "LunchBox" is in cart, we check the quantity to update it if needed    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {        // Lunchbox total = total_items_in_cart         $lunchbox_total = $total_items_in_cart;        // Isset lunchbox qty, lunchbox total - lunchbox qty        if ( isset($lunchbox_qty) ) {            $lunchbox_total = $lunchbox_total - $lunchbox_qty;        }        // Isset pakket qty, lunchbox total - pakket qty                if ( isset($pakket_qty) ) {            $lunchbox_total = $lunchbox_total - $pakket_qty;        } 有一個小問題。購物車中所有自動添加的包裝都與其他產品混合分類。如何使購物車中的包裝始終位于產品列表的底部?
查看完整描述

1 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

添加到數組中的產品 ID 將顯示在列表底部

function sort_cart_specific_product_at_bottom( $cart ) {    

    // Product id's to to display at tbe bottom of the product list

    $product_ids_last = array( 30, 815 );


    // Set empty arrays

    $products_in_cart = array();

    $products_last = array();

    $cart_contents = array();


    // Loop through cart items

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

        // Get product id

        $product_id = $cart_item['data']->get_id();


        // In_array — checks if a value exists in an array

        if ( in_array( $product_id, $product_ids_last) ) {

            // Add to products last array

            $products_last[ $cart_item_key ] = $product_id;

        } else {

            // Add to products in cart array

            $products_in_cart[ $cart_item_key ] = $product_id;

        }

    }


    // Merges the elements together so that the values of one are appended to the end of the previous one.

    $products_in_cart = array_merge( $products_in_cart, $products_last );


    // Assign sorted items to cart

    foreach ( $products_in_cart as $cart_item_key => $product_id ) {

        $cart_contents[ $cart_item_key ] = $cart->cart_contents[ $cart_item_key ];

    }


    // Cart contents

    $cart->cart_contents = $cart_contents;


}

add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_specific_product_at_bottom', 10, 1 );



查看完整回答
反對 回復 2022-11-12
  • 1 回答
  • 0 關注
  • 103 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號