Woocommerce 按产品类别对购物车产品进行排序 [英] Woocommerce sort cart products by product category

查看:32
本文介绍了Woocommerce 按产品类别对购物车产品进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我想让我的 Woocommerce 购物车按产品类别的顺序显示产品.(我的产品被分配到一个品牌,我希望这些产品出现在其指定品牌下的购物车区域.)

I would like to make it so my Woocommerce cart shows products in order of product category. (My products are assigned to a brand, and i want the products to appear in the cart area under their assigned brands.)

我的尝试

目前我已经能够让它按字母顺序按键排序,但这是我对数组的了解.

At the moment i've been able to get it to sort alphabetical by key however this is as far as my knowledge with arrays goes.

示例代码

    add_action( 'woocommerce_cart_loaded_from_session', function() {

        global $woocommerce;
        $products_in_cart = array();
        foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
            $products_in_cart[ $key ] = $item['data']->get_title();
        }

        ksort( $products_in_cart );

        $cart_contents = array();
        foreach ( $products_in_cart as $cart_key => $product_title ) {
            $cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
        }
        $woocommerce->cart->cart_contents = $cart_contents;

    }, 100 );

附加说明

我知道我可以使用此代码来获取每个产品的术语 ID.但我不太确定如何最好地构建我的代码以获得我想要的结果.

I know I can use this code to get the term ID of each product. But i'm not quite sure how best to structure my code to get the outcome I'm after.

  $terms = wp_get_post_terms(get_the_ID(), 'product_cat' );

推荐答案

你已经得到了所有正确的部分.

You've got all the right pieces.

要在此上下文中获取帖子术语,您需要调整获取购物车项目 ID 的方式$terms = wp_get_post_terms($item['data']->id, 'product_cat');

To get the post terms in this context you need to tweak how you are getting the ID of the cart item $terms = wp_get_post_terms($item['data']->id, 'product_cat' );

获取帖子词的结果将给你一个看起来像这样的数组

The result of getting the post terms will give you an array that looks like this

Array(
[0] => stdClass Object(
    [term_id] => 877
    [name] => Product Category Name
    [slug] => Product Category Name
    [term_group] => 0
    [term_taxonomy_id] => 714
    [taxonomy] => product_cat
    [description] => 
    [parent] => 0
    [count] => 1
    [filter] => raw
    )
)

下面的代码将按数组中的第一个类别对购物车进行排序.这并不完整,您需要考虑未设置产品类别以及设置多个产品类别.

The code below will sort the cart by the first category in the array. This is not complete, you will need to account for no product category being set and also multiple product categories being set.

add_action( 'woocommerce_cart_loaded_from_session', function() {

    global $woocommerce;
    $products_in_cart = array();
    foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
        $terms = wp_get_post_terms($item['data']->id, 'product_cat' );
        $products_in_cart[ $key ] = $terms[0]->name;
    }

    ksort( $products_in_cart );

    $cart_contents = array();
    foreach ( $products_in_cart as $cart_key => $product_title ) {
        $cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
    }
    $woocommerce->cart->cart_contents = $cart_contents;

}, 100 );

这篇关于Woocommerce 按产品类别对购物车产品进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆