【问题标题】:Change order of Cart in WooCommerce在 WooCommerce 中更改购物车的顺序
【发布时间】:2017-08-07 22:58:50
【问题描述】:

我希望在 WordPress 上的 WooCommerce 中重新排序购物车页面上的产品表。目前列出的产品从最旧 - 最新(从添加到购物车的顺序),并希望有相反的,希望有最新的添加在顶部和最旧的底部。

do_action( 'woocommerce_before_cart' ); ?>

<div class="cart_container">

<form class="cart-form" action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">

<?php do_action( 'woocommerce_before_cart_table' ); ?>

是否可以在调用cart_url时添加orderby

【问题讨论】:

    标签: php wordpress woocommerce cart orders


    【解决方案1】:

    要进行任何类型的购物车订购,您必须使用 woocommerce_cart_loaded_from_session钩子;并扭转 命令只需使用 PHP array_reverse 函数。

    代码如下:

    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) {
            $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);
    }
    

    代码进入您的活动子主题(或主题)的function.php 文件。或者也可以在任何插件 php 文件中。
    代码已经过测试并且可以工作。

    希望这会有所帮助!

    【讨论】:

    • 完美,正是我想要的,谢谢!知道如何为我的迷你购物车退回相同的订单吗?
    • @Kelly:我不太熟悉迷你购物车,但我会尽快找到解决方案。
    • @Kelly 将 mini-cart.php 模板复制到主题中并在每个循环中使用 array_reverse:foreach ( array_reverse(WC()-&gt;cart-&gt;get_cart()) as $cart_item_key =&gt; $cart_item ) {...
    【解决方案2】:

    公认的答案有一个主要缺陷:它创建了一个竞争条件和一个无限的 AJAX 刷新循环,并打开了多个选项卡 (see here)。

    我能够解决这个问题的方法是使用动作挂钩:

    1. 在循环浏览购物车内容之前,我们反转内容并保存新的反转顺序
    2. 购物车内容循环完后,我们重复第1步,恢复原来的顺序

    购物车项目在前端循环的三个区域(默认情况下),所以我使用的动作挂钩覆盖了每个区域。

    这是经过测试的代码:

    function reverse_cart_contents() {
      $cart_contents = WC()->cart->get_cart_contents();
    
      if($cart_contents) {
        $reversed_contents = array_reverse($cart_contents);
        WC()->cart->set_cart_contents($reversed_contents);
      }
    }
    add_action('woocommerce_before_mini_cart', 'reverse_cart_contents');
    add_action('woocommerce_after_mini_cart', 'reverse_cart_contents');
    add_action('woocommerce_before_cart', 'reverse_cart_contents');
    add_action('woocommerce_after_cart', 'reverse_cart_contents');
    add_action('woocommerce_review_order_before_cart_contents', 'reverse_cart_contents');
    add_action('woocommerce_review_order_after_cart_contents', 'reverse_cart_contents');
    

    【讨论】:

      【解决方案3】:

      您可以修改 woocommerce 插件的 cart/cart.php 模板文件。当循环从购物车页面上的“WC()->cart->get_cart()”开始时,您可以先将此数组放入一个单独的数组中,反转,然后使用此反转数组以相反的顺序显示购物车产品。

      建议使用此选项,因为您实际上并不与 woocommerce 对象交互,因此它涉及的处理较少。你只是把它们倒过来展示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 2021-04-19
        • 1970-01-01
        相关资源
        最近更新 更多