猿问

按 SKU 对购物车 WooCommerce 中产品列表底部的产品进行排序

在 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 添加包装,如何更改排序代码?


我很乐意为您提供帮助!


富国沪深
浏览 92回答 1
1回答

慕村225694

以下代码将根据产品 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 );
随时随地看视频慕课网APP
我要回答