【问题标题】:Sort WooCommerce cart items by vendor when using Dokan plugin使用 Dokan 插件时按供应商对 WooCommerce 购物车商品进行排序
【发布时间】:2021-02-08 19:57:48
【问题描述】:

我们正在研究一种解决方案,以按供应商商店名称对购物车中的产品进行分类和结帐。我们在这里找到了这个:Woocommerce sort cart products by product category,但我们必须为我们的案例扩展这个代码。每个产品都有一个供应商。所以我们不必检查产品是否没有供应商。

我不确定如何以正确的方式输出数组。我可以这样吗?

add_action( 'woocommerce_cart_loaded_from_session', function() {

    global $woocommerce;
    
    // Build product array
    $products_in_cart = array();

    // Loop through cart items
    foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
        
         // Get product object
        $product = $item->get_product();
        
        // Author id
        $author_id = $product->post->post_author;
        
        // Shopname
        $vendor = dokan()->vendor->get( $author_id );
        $shop_name = $vendor->get_shop_name();
        
        $products_in_cart[ $key ] = $shop_name[0]->name;
    
    }

    ksort( $products_in_cart );
    
    // Build cart array
    $cart_contents = array();
        
    foreach ( $products_in_cart as $cart_key => $Vendor_store ) {
        $cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
    }
    
    // Output sorted cart
    $woocommerce->cart->cart_contents = $cart_contents;

}, 100 );

【问题讨论】:

    标签: php wordpress woocommerce cart dokan


    【解决方案1】:

    更新:尝试以下简化和重新访问的代码:

    add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_items_by_vendor', 100 );
    function sort_cart_items_by_vendor() {
        $items_to_sort = $cart_contents = array(); // Initializing
    
        // Loop through cart items
        foreach ( WC()->cart->cart_contents as $item_key => $cart_item ) {
            $vendor_id  = get_post_field( 'post_author', $cart_item['product_id']);
            $store_info = dokan_get_store_info( $vendor_id );
            $items_to_sort[ $item_key ] = $store_info['store_name'];
        }
    
        ksort( $items_to_sort );
        
        // Loop through sorted items key
        foreach ( $items_to_sort as $cart_item_key => $store_name ) {
            $cart_contents[ $cart_item_key ] = WC()->cart->cart_contents[ $cart_item_key ];
        }
        
        // Set sorted items as cart contents
        WC()->cart->cart_contents = $cart_contents;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    相关:

    【讨论】:

    • 第 10 行是语法错误。一个 ;那里不见了。添加新产品后,它总是会清除购物车。但与此同时,我从您这里发现了这个问题:stackoverflow.com/questions/61992935/… 这实际上比仅按供应商名称排序更合适。在那里,我们将购物车分成几部分......让我们在那里谈谈。
    • @Nik7 有段时间我不再使用 Dokan,所以我不能再测试它了。
    • @Nik7 我更新了我的代码,缺少了;...试试看... 先在这里完成。它现在应该可以工作了。如果这个答案回答了你的问题,你可以请accept回答,如果你喜欢/想要你也可以请upvote回答,谢谢。
    • 谢谢。有用。但我注意到有时在添加产品后逻辑中断。但很难说会发生这种情况。供应商的一些产品运行良好,有些则不然。产品可能会忽略这种新的排序,或者为什么会发生这种情况?
    • @Nik7 代码有效。因此,在您的情况下,有些事情正在制造麻烦。它可以是您的主题、插件或您添加的其他自定义代码……(正在睡觉)
    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多