简体   繁体   English

特定类别的WooCommerce产品小部件-为每个小部件设置类别数组

[英]WooCommerce products widget for a specific category - set category array per widget

I had the same question as Quadie as answered in the following thread . 我在以下线程中回答了与Quadie相同的问题。

The code as provided by LoicTheAztec works great... LoicTheAztec提供的代码非常有用...

add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
    // Set HERE your product category slugs 
    $categories = array( 'music', 'posters' );

    $query_args['tax_query'] = array( array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => $categories,
    ));

    return $query_args;
}, 10, 1 );

... but I would like to know if there is an option to extend this code to be able to set the categories per seperate widget. ...但是我想知道是否有扩展此代码的选项,以便能够为每个单独的小部件设置类别。 Eg I want a products widget for category 1 on a page x, while I want a products widget for category 2 somewhere else on page x. 例如,我想要页面x上类别1的产品小部件,而我希望页面x上其他位置的类别2的产品小部件。

I was thinking about using a shortcode & specifying the category as an array for this shortcode, but not sure on how to implement this. 我正在考虑使用简码 ,并将类别指定为此简码的数组,但不确定如何实现。

Any thoughts on this issue? 对这个问题有什么想法吗?


This is the shortcode I tried to use: 这是我尝试使用的简码:

[productsbycat cat1="broodjes"]

& it triggers the following code: &触发以下代码:

function productsbycat_func( $atts ) {
    $categories = shortcode_atts( array(
        'cat1' => 'something',
        'cat2' => 'something else',
    ), $atts );

    add_filter( 'woocommerce_products_widget_query_args', function ( $query_args ){

        $query_args['tax_query'] = array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => $categories,
        ));

        return $query_args;
    }, 10, 1 );
 }

add_shortcode( 'productsbycat', 'productsbycat_func' );

However, it does not generate anything yet. 但是,它尚未生成任何东西。

Is not possible to extend my code "to be able to set the categories per seperate widget". 无法扩展我的代码“以便能够为每个单独的小部件设置类别”。

For your shortcode only I think that you don't need any code customization for that. 仅对于您的短代码,我认为您不需要为此进行任何代码自定义。

I you look to the WooCommerce Official documentation related to WooCommerce Shortcodes , you will see on section "Product category" this short code: [product_category] , which main argument is category 我查看与WooCommerce简码相关的WooCommerce官方文档,您将在“产品类别”部分看到以下短代码: [product_category] ,其主要参数是category

You can add one product category slugs this way: 您可以通过以下方式添加一个产品类别标签

[product_category category="clothing"] // One product category

Or for many product categories slugs (coma separated) this way: 或对于许多产品类别段塞 (以逗号分隔)是这样的:

[product_category category="posters,music"]

The default arguments settings (that you can change) are: 可以更改的默认参数设置为:

$args = array(
    'per_page' => "12",
    'columns' => "4",
    'orderby' => 'title', // or by "menu_order"
    'order' => "asc", // or "desc"
    'category' => "" // Always only product category slugs
    'operator' => "IN" // Possible values are "IN", "NOT IN", "AND".
);

But it will not work as you would like in your product widgets as it will display the product grid loop for the defined categories 但是它无法像您在产品小部件中那样工作,因为它将显示已定义类别的产品网格循环


You should need to build your own widgets. 您应该需要构建自己的小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM