如何按日期对购物车项目进行排序(最后修改的购物车项目将是第一个)?

How to sort cart items by date (the last modified cart item will be the first)?

我正在尝试按日期排序购物车内容,最后添加或修改的内容必须首先显示。我尝试颠倒购物车的顺序,但它没有按预期工作。这是我试过的代码:

//reorder cart
add_action('woocommerce_cart_loaded_from_session', 'wh_cartOrderItemsbyNewest');

function wh_cartOrderItemsbyNewest() {

    //if the cart is empty do nothing
    if (WC()->cart->get_cart_contents_count() == 0) {
        return;
    }

    //array to collect cart items
    $cart_sort = [];

    //add cart item inside the array
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { print_r(WC()->cart->cart_contents[$cart_item_key][0]);
        $cart_sort[$cart_item_key] = WC()->cart->cart_contents[$cart_item_key];
    }

    //replace the cart contents with in the reverse order
    WC()->cart->cart_contents = array_reverse($cart_sort);
}

Cart items are already shown in cart by date added.

那么最后加入购物车的将是列表中的第一个。

当您更改购物车中的产品数量时出现问题。在这种情况下,他将保持原来的位置。

我考虑过使用日期为 addition/modification 的自定义购物车商品数据,但在这种情况下,它会创建相同产品的更多行。

我用 Options API 解决了它。

我考虑过creating/updating实时日期选项,当时:

  • woocommerce_add_to_cart 产品已添加到购物车
  • woocommerce_cart_item_removed 产品已从购物车中移除
  • woocommerce_after_cart_item_quantity_update 购物车商品数量已更新 (增加或减少)

当删除选项时:

  • woocommerce_cart_emptied购物车已清空

The sort will also be applied to the mini cart.

以下函数将根据 last_update_cart_item 选项创建购物车商品的自定义排序

// sorts cart items by modification date
add_action( 'woocommerce_cart_loaded_from_session', 'sorts_cart_items_by_modification_date' );
function sorts_cart_items_by_modification_date() {

    // only if the cart is not empty
    if ( WC()->cart->get_cart_contents_count() == 0 ) {
        return;
    }

    // only if the "last_update_cart_item" option exists
    if ( ! $last_update = get_option( 'last_update_cart_item' ) ) {
        return;
    }

    $cart_sort = array();
    $cart_item_content = array();

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // gets the update date of the cart item
        $cart_sort[$cart_item_key] = $last_update[$cart_item_key];
        // gets the contents of the cart item
        $cart_item_content[$cart_item_key] = WC()->cart->cart_contents[$cart_item_key];
    }

    // sorts the cart items by descending date (the last modified will be the first)
    array_multisort( $cart_sort, SORT_DESC, WC()->cart->get_cart() );

    // creates the new sorting with the contents of the cart items
    foreach ( $cart_sort as $key => $value ) {
        $sorted[$key] = $cart_item_content[$key];
    }

    // update cart contents with custom sorting
    WC()->cart->cart_contents = $sorted;
    
}

虽然以下函数将更新 last_update_cart_item 选项 当添加、更改或从购物车中删除产品时 :

// update the "last_update_cart_item" option with the date the product was added to the cart
add_action( 'woocommerce_add_to_cart', 'after_woocommerce_add_to_cart', 10, 6 );
function after_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    $last_update = array();
    if ( get_option( 'last_update_cart_item' ) ) {
        $last_update = get_option( 'last_update_cart_item' );
    }
    // set the updated date of the cart item
    $last_update[$cart_item_key] = date("Y-m-d H:i:s");
    update_option( 'last_update_cart_item', $last_update );

    return $cart_item_data;
}

// when the cart item is removed, it also removes the cart item key from the "last_update_cart_item" option
add_action( 'woocommerce_cart_item_removed', 'after_woocommerce_cart_item_removed', 10, 2 );
function after_woocommerce_cart_item_removed( $cart_item_key, $cart ) {
    if ( $last_update = get_option( 'last_update_cart_item' ) ) {
        if ( array_key_exists( $cart_item_key, $last_update ) ) {
            // removes the cart item key from the "last_update_cart_item" option
            unset( $last_update[$cart_item_key] );
            update_option( 'last_update_cart_item', $last_update );
        }
    }
}

// when the quantity of the cart item updates, sets or updates the date
add_action( 'woocommerce_after_cart_item_quantity_update', 'update_cart_item_data_after_cart_item_quantity_update', 10, 4 );
function update_cart_item_data_after_cart_item_quantity_update( $cart_item_key, $quantity, $old_quantity, $cart ) {
    $last_update = array();
    if ( get_option( 'last_update_cart_item' ) ) {
        $last_update = get_option( 'last_update_cart_item' );
    }
    // updates the date of the cart item whose quantity has been changed
    $last_update[$cart_item_key] = date("Y-m-d H:i:s");
    update_option( 'last_update_cart_item', $last_update );

}

// when the cart is empty delete the "last_update_cart_item" option
add_action( 'woocommerce_cart_emptied', 'after_woocommerce_cart_emptied', 10, 1 );
function after_woocommerce_cart_emptied( $cart ) {
    // delete the "last_update_cart_item" option
    delete_option( 'last_update_cart_item' );
}

代码已经过测试并且可以工作。将它添加到您的活动主题 functions.php.