【问题标题】:Create WooCommerce Page with products on sale使用销售产品创建 WooCommerce 页面
【发布时间】:2021-02-20 02:09:13
【问题描述】:

我正在尝试创建一个页面(默认 woocommerce 存档样式),它将仅显示正在销售的产品。在这里提一下,我只有可变产品而不是简单的产品。

我尝试制作我的自定义简码

global $woocommerce_loop;
    $atts = shortcode_atts( array(
        'per_page' => '-1',
        'columns'  => '4',
        'orderby'  => 'title',
        'order'    => 'asc'
    ), $atts );

    // Get products on sale
    $product_ids_on_sale = wc_get_product_ids_on_sale();

    $meta_query = WC()->query->get_meta_query();
    $args = array(
        'posts_per_page'    => $atts['per_page'],
        'orderby'           => $atts['orderby'],
        'order'             => $atts['order'],
        'no_found_rows'     => 1,
        'post_status'       => 'publish',
        'post_type'         => 'product',
        'meta_query'        => $meta_query,
        'post__in'          => array_merge( array( 0 ), $product_ids_on_sale )
    );
    ob_start();
    $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
    $columns = absint( $atts['columns'] );
    $woocommerce_loop['columns'] = $columns;
    if ( $products->have_posts() ) : 

        woocommerce_product_loop_start();

            while ( $products->have_posts() ) : $products->the_post(); 

                wc_get_template_part( 'content', 'product' ); 

            endwhile; // end of the loop. 

         woocommerce_product_loop_end(); 

     endif;
    wp_reset_postdata();
    return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';

但结果我只得到了 2 件产品。

有什么帮助或想法吗?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    您为什么要尝试创建新的短代码? Woocommerce 提供了自己的简码,以自己的档案风格显示在售的产品:

    [sale_products per_page="12"]

    你可以看到整个列表here

    【讨论】:

    • 嗯,我试过这个,但它只给我返回 2 个产品。但是在我的电子商店中,我有更多的产品在售。
    • @GeorgeNtoum 我不明白为什么会这样。可能是您的其他产品没有销售价格吗? wc_get_product_ids_on_sale() 代码在销售价格上查找非零值。您可能需要检查是否正确添加。
    • 是的,这对我来说也很奇怪。我制作了一个循环所有产品的 php 文件,并使用 $product->is_on_sale() 方法对每个产品进行检查,并将所有销售产品返回给我。所以我知道这些产品似乎很适合销售。 (我也有销售徽章)。我将发布一个我发现的临时解决方案。
    • 谢谢,这将为我节省大量时间来创建一些自定义类别 :) 我不认为要寻找现有的简码
    • @MitchellK,很高兴它对你有帮助! WooCommerce 确实有一些可以灵活使用的短代码。
    【解决方案2】:

    我通过创建自定义简码找到了一个临时解决方案。我不知道为什么我没有使用默认的 woocommerce 短代码获得所有销售产品。

    这对我有用:

    函数 variable_sale_products( $atts ) { 全球 $woocommerce, $product;

        $args = array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => -1
        );
    
        ob_start();
        $loop = new WP_Query( $args );
    
        if ( $loop->have_posts() ) {
            woocommerce_product_loop_start();
    
            while ( $loop->have_posts() ) : $loop->the_post();
                $id = get_the_ID();
                $_product = wc_get_product( $id );
    
                if($_product->is_on_sale()){    
                    wc_get_template_part( 'content', 'product' );
                }
            endwhile;
    
            woocommerce_product_loop_end(); 
        }
        wp_reset_postdata();
        return '<div class="woocommerce columns-4">' . ob_get_clean() . '</div>';
    }
    add_shortcode( 'variation_sale_product', 'variable_sale_products' );
    

    如果您有任何其他建议,我想听听您的意见

    【讨论】:

    • 在下面查看我的答案。
    【解决方案3】:

    尝试扩展 $args(此代码添加销售简单和可变产品):

    $args = array(
            'posts_per_page'   => -1,
            'post_type'  => 'product',
            'meta_key' => 'total_sales',
            'orderby' => 'meta_value_num',
            'meta_query'     => array(
                'relation' => 'OR',
                array( // Simple products type
                    'key'           => '_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                ),
                array( // Variable products type
                    'key'           => '_min_variation_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                )
            )
          );
    

    【讨论】:

      【解决方案4】:

      我试过 [sale_products per_page="12"] ,但它只显示一种产品。

      以下是我展示所有特价商品的步骤:

      1- 转到 WooCommerce -> 状态 -> 工具选项卡,然后单击旁边的按钮以生成查找表,并在再次检查之前给它一些时间来完成。如果这不起作用,请继续下一步。

      2- 将shortode 更新为[sale_products per_page="32" paginate="true" columns="4" orderby="random"]。如果这不起作用,请再次执行下一步。

      3- 这不合逻辑,但我所做的是我删除了出现在销售页面中的商品的“销售价格”。之后所有其他项目都出现了!

      希望这对任何人都有帮助

      【讨论】:

        猜你喜欢
        • 2021-11-06
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 2016-06-15
        相关资源
        最近更新 更多