如果销售价格低于原价,Woocommerce 仅显示产品

时间:2021-01-12 09:18:52

标签: woocommerce shortcode

我遇到一个问题,我的商店显示的促销产品甚至没有促销价。我认为这与缓存有关。 现在,我想使用自定义短代码创建一个快速修复程序,该短代码仅显示变体销售价格低于原始变体价格的产品。

我得到了一些代码,但数组中的“过滤”不起作用。

<?php
function SaleProducts() {
global $product;
?>
<div class="woocommerce columns-4">
    <ul class="products columns-4">
        <?php
            $SalePrice = get_post_meta( get_the_ID(), '_price', true);
            $NormalPrice = get_post_meta( get_the_ID(), '_regular_price', true);
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post_type'         => 'product',
                'posts_per_page'    => 24,
                'post__in'          => wc_get_product_ids_on_sale(),
                'paged'             => $paged,
                'meta_query'     => array(
                        'relation' => 'AND',
                        array( // Simple products type
                            'key'           => '_price',
                            'value'         => '_regular_price',
                            'compare'       => '<=',
                            'type'          => 'numeric'
                        ),
                        array( // Variable products type
                            'key'           => '_regular_price',
                            'value'         => '_price',
                            'compare'       => '>=',
                            'type'          => 'numeric'
                        )
                    )
            );
        
            $loop = new WP_Query( $args );
            

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

                    wc_get_template_part( 'content', 'product' );
                
            endwhile;
        
            echo '<nav class="woocommerce-pagination">';
            echo previous_posts_link( '&laquo; Previous' );
            echo next_posts_link( 'Next &raquo;', $loop->max_num_pages );
            echo '</nav>';
        
            wp_reset_query();

        ?>
    </ul>
</div>
<?php
}
add_shortcode('onlysale', 'SaleProducts');
?>

希望有人能帮我。

0 个答案:

没有答案