【问题标题】:Woocommerce - Cart Page - Change Sequence of Products in Cart (Travel Packages)Woocommerce - 购物车页面 - 更改购物车中产品的顺序(旅行套餐)
【发布时间】:2014-10-29 21:29:56
【问题描述】:

我正在尝试使用 woocommerce 销售自建旅行套餐。因此,访问者会选择一些他们喜欢的酒店、活动和交通工具,然后将它们添加到购物车中。

我面临的问题是物品(旅行产品)按添加顺序添加到购物车中。

但是,我需要这样的功能:管理员或访客都可以重新排列这些物品在购物车中出现的位置(顺序)。也许像 AJAX 拖放功能会很棒。

有办法吗?

附言。我不是指更改目录中出现的订单产品,使用插件很容易。

请给我一些指导。谢谢。

【问题讨论】:

    标签: woocommerce cart


    【解决方案1】:

    我曾经考虑从我的插件重新订购购物车中的物品,但最终认为这对我来说不值得。这是我要使用的:

    add_action( 'woocommerce_cart_loaded_from_session', 'so_26640996_reorder_cart_keys' );
    
    /**
     * Re-order the cart keys to keep container/bundled items from being separated in case of force-sells
     * @param  object   $cart - the WC_Cart class
     * @return void
     */
    function so_26640996_reorder_cart_keys( $cart ){
    
        if( $cart->cart_contents ) { 
    
            $new = array();
    
            $contents = $cart->cart_contents;
    
            // loop through the cart's contents
            foreach( $cart->cart_contents as $cart_key => $cart_item ){
    
                // add the item back into a new array of cart contents
                $new[$cart_key]=$cart_item;
    
                // if the cart item meets some condition re-arrange its position
                if( isset( $cart_item['mnm_contents'] ) && is_array( $cart_item['mnm_contents'] ) ){
                    foreach($cart_item['mnm_contents'] as $key){
                        if( isset( $contents[$key] ) ){
                            $new[$key] = $contents[$key];
                            unset($contents[$key]);
                        }
                    }
                }
    
            }
    
            $cart->cart_contents = $new;
    
        }
    
    }
    

    它不完全适合您的问题(而且也很晚),但也许它会帮助有同样问题的人。

    【讨论】:

    • 您好,感谢您的回复。您是否将此代码粘贴到购物车页面中?
    • 不,最好为它创建一个新插件。或者在最坏的情况下,在您的主题的functions.php 中尝试一下。请记住,这只是一个示例,可能不会完全按照您的要求进行。
    • 嗨@helgatheviking 你的解决方案解决了我的问题,但是你的一些代码我不明白,如果你能得到解释,不胜感激。在第一个循环中 $new 已经分配给 cart_item,但是第二个循环中的 $new 如何使购物车项目以正确的顺序排列?以及为什么需要unset,无论是否申请unset,我的序列都是正确的。
    • @YingRuiWong 因为我 5 年前的代码马虎?我认为你是对的......应该不需要unset() 任何东西,$contents 变量可能可以被忽略。尽管请注意,我可能会在我的一个插件中引用它,并且可能仅将排序应用于我的插件创建的产品类型。
    猜你喜欢
    • 2018-06-03
    • 2018-07-04
    • 1970-01-01
    • 2017-08-07
    • 2017-04-06
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    相关资源
    最近更新 更多