为了账号安全,请及时绑定邮箱和手机立即绑定

按产品 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 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信