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

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

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

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

PHP
富國(guó)滬深 2022-12-23 14:45:12
在 WooCommerce 中,我使用一個(gè)代碼來顯示牛排重量選擇表單,保存選擇數(shù)據(jù)并在購(gòu)物車、結(jié)帳頁(yè)面、編輯訂單時(shí)和電子郵件通知中顯示這些數(shù)據(jù)。我的代碼還結(jié)合了一個(gè)代碼,在將任何產(chǎn)品添加到購(gòu)物車時(shí)自動(dòng)添加包裝。添加包裝發(fā)生在 SKU 上。有一次,用戶@7uc1f3r 幫助進(jìn)行了自定義排序,使包裝始終位于購(gòu)物車中產(chǎn)品列表的最底部。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 );但不幸的是,排序代碼有點(diǎn)過時(shí),因?yàn)楝F(xiàn)在包裹是按SKU添加的,而不是按ID添加的。因此,排序不起作用??紤]到按 SKU 添加包裝,如何更改排序代碼?我很樂意為您提供幫助!
查看完整描述

1 回答

?
慕村225694

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

以下代碼將根據(jù)產(chǎn)品 sku 對(duì)產(chǎn)品進(jìn)行最后排序

function sort_cart_specific_product_at_bottom( $cart ) { 

    // Product sku to to display at tbe bottom of the product list

    $product_sku_last = array( 'lunchbox', 'pakket' );


    // 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 sku

        $product_sku = $cart_item['data']->get_sku();


        // Get product id

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


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

        if ( in_array( $product_sku, $product_sku_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 );

并且此代碼確保基于 sku 的商品不會(huì)從購(gòu)物車中移除


function prevent_cart_item_remove_link( $link, $cart_item_key ) {

    // Product sku that should not be removable

    $product_sku_last = array( 'lunchbox', 'pakket' );


    if( WC()->cart->find_product_in_cart( $cart_item_key ) ) {

        $cart_item = WC()->cart->cart_contents[ $cart_item_key ];


        // Get product sku

        $product_sku = $cart_item['data']->get_sku();


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

        if ( in_array( $product_sku, $product_sku_last ) ) {

            $link = '';

        }

    }


    return $link;

}

add_filter( 'woocommerce_cart_item_remove_link', 'prevent_cart_item_remove_link', 10, 2 );



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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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