第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

按產(chǎn)品 ID 對(duì)購物車 WooCommerce 中產(chǎn)品列表底部的產(chǎn)品進(jìn)行排序

按產(chǎn)品 ID 對(duì)購物車 WooCommerce 中產(chǎn)品列表底部的產(chǎn)品進(jìn)行排序

PHP
肥皂起泡泡 2022-11-12 13:22:50
在 WooCommerce 中,我使用代碼在將任何菜品添加到購物車時(shí)自動(dòng)添加包裝。功能如下:菜品在購物車中,添加了1個(gè)飯盒菜品在購物車中,增加了2個(gè)飯盒菜在購物車?yán)?,增加?個(gè)飯盒有 3 個(gè)飯盒,所以現(xiàn)在添加 1 個(gè)包裹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;        } 有一個(gè)小問題。購物車中所有自動(dòng)添加的包裝都與其他產(chǎn)品混合分類。如何使購物車中的包裝始終位于產(chǎn)品列表的底部?
查看完整描述

1 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

添加到數(shù)組中的產(chǎn)品 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 );



查看完整回答
反對(duì) 回復(fù) 2022-11-12
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)