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

问题描述

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

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

标签: phpdatewoocommercecart

解决方案


购物车项目已按添加日期显示在购物车中。

然后添加到购物车的最后一个将成为列表中的第一个。

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

我想过使用带有添加/修改日期的自定义购物车项目数据,但在这种情况下,它会创建更多相同产品的行。

我用Options API解决了这个问题。

我考虑过在以下情况下创建/更新实时日期选项:

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

虽然该选项在以下情况下被删除:

  • woocommerce_cart_emptied购物车已清空

排序也将应用于迷你购物车。

以下函数将根据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。


推荐阅读