Woocommerce短代码显示特定产品类别的特价产品

问题描述 投票:0回答:2

我有不同的类别,但我希望在我的网站菜单中有一个项目,我将仅在特定类别中展示销售的产品,而不是所有正在销售的产品。

我使用此代码,但它显示所有待售的产品

[sale_products per_page="32"]

任何提示?

php wordpress woocommerce shortcode product
2个回答
0
投票

这是一个基于woocommerce版本3.2的自定义短代码,退出短代码,将动态获取当前产品类别(在单个产品页面中),并将为其显示“待售”产品。您也可以定义特定的产品类别。

您还可以设置(在功能代码或短代码本身内)这个参数:

  • category ==>特定产品类别
  • limit ==>要显示的默认产品数量
  • columns ==>默认列数。

代码:

// Only for Woocommerce version 3.2 and higher (3.2+)
add_shortcode("cat_sale_prod", "get_category_on_sale_products");
function get_category_on_sale_products( $atts ) {
    global $post;

    // Attributes
    $atts = shortcode_atts(
        array(
            'limit'    => '12',
            'columns'  => '4',
            'category'  => '',
        ),
        $atts, 'cat_sale_prod');

    $limit   = $atts['limit'];
    $columns = $atts['columns'];

    // Get product categories
    $terms = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'slugs' ));
    $term  = reset($terms);

    return do_shortcode( "[products limit='$limit' columns='$columns' on_sale='true' category='$term']" );
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

具有特定产品类别的使用示例:

[cat_sale_prod category="clothing"]

0
投票

Woocommerce附带了一些可用于向页面添加自定义内容的短代码。

[产品]短代码允许您通过邮政ID,SKU,类别,属性显示产品,支持分页,随机分类和产品标签,取代了对多个短代码的需求,例如:

[featured_products]
[sale_products]
[best_selling_products] 
[recent_products] 
[product_attribute]
[top_rated_products]

因此,您可以将[产品]短代码与某些属性结合使用,并在您的类别中显示待售产品,您可以使用这样的东西

[products  category ="slug-of-your-cat" on_sale="true" ]

欲了解更多信息,请访问:https://docs.woocommerce.com/document/woocommerce-shortcodes/

© www.soinside.com 2019 - 2024. All rights reserved.