Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

shortcode - Woocommerce show only products if sale price is lower then original price

I got a problem that my shop shows sale products that have not even a sale price. I think this has something to do with caching. For now i want to create a quick fix with a custom shortcode that only shows products that have a lower variation sale price then the original variation price.

I got a bit of the code, but the 'filtering' in the array is not working.

<?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');
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...