Dưới đây là đoạn code hỗ trợ thêm sản phẩm cùng mức giá trong Woocommerce. Bạn hãy xem đoạn code bên dưới đã đính kèm mô tả, lưu ý: thêm vào file functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//SẢN PHẨM CÓ CÙNG MỨC GIÁ function get_same_price_hwp(){ //get gia mac dinh woo $get_meta = get_post_meta( get_the_ID(), '_price', true ); //get giá sản phẩm hiện tại $prmax = $get_meta + 50000; // giá max = giá sản phẩm hiện tại + 50k $loop = array( 'post_status' => 'publish', 'post_type' => 'product', 'posts_per_page' => 4, 'post__not_in' => array(get_the_ID()), 'meta_query' => array( array( 'key' => '_price', 'value' => array($get_meta, $prmax), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ) ) ); $hwpquery = new WP_Query($loop); if ( $hwpquery->have_posts() ) : echo '<h2>Sản phẩm cùng mức giá</h2>'; echo '<div class="row large-columns-4 medium-columns-3 small-columns-2 row-small">'; while ( $hwpquery->have_posts() ) : $hwpquery->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; wp_reset_postdata(); echo '</div>'; endif; } add_action('woocommerce_after_single_product_summary','get_same_price_hwp',1); |
Hiện tại mình set giá chênh lệch 50,000 K. Bạn có thể tùy chỉnh số tiền này. Và Hook thì mình đang dùng woocommerce_after_single_product_summary bạn cũng có thể thay đổi – Xem hook tại đây!