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

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

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

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

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

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

以下代碼將根據產品 sku 對產品進行最后排序

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 的商品不會從購物車中移除


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



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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