Last active
May 24, 2024 03:32
-
-
Save braddalton/c00c88cd8dd81c65543a7367f1ba91ba to your computer and use it in GitHub Desktop.
Products by Brand in WooCommerce. Full Tutorial https://wpsites.net/?p=116042
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_after_single_product_summary', 'display_on_sale_products_by_brand', 15 ); | |
function display_on_sale_products_by_brand() { | |
global $post; | |
// Get the brands of the current product | |
$brands = wp_get_post_terms( $post->ID, 'brand' ); | |
if ( ! empty( $brands ) && ! is_wp_error( $brands ) ) { | |
foreach ( $brands as $brand ) { | |
$brand_id = $brand->term_id; | |
// Query on-sale products by the same brand | |
$args = array( | |
'post_type' => 'product', | |
'posts_per_page' => 10, // Adjust the number of products to display | |
'meta_query' => array( | |
array( | |
'key' => '_sale_price', | |
'value' => 0, | |
'compare' => '>', | |
'type' => 'NUMERIC' | |
) | |
), | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'brand', | |
'field' => 'term_id', | |
'terms' => $brand_id | |
) | |
) | |
); | |
$query = new WP_Query( $args ); | |
if ( $query->have_posts() ) { | |
echo '<p>On-Sale Products by ' . $brand->name . ':</p>'; | |
echo '<ul class="products">'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
wc_get_template_part( 'content', 'product' ); | |
} | |
echo '</ul>'; | |
} | |
wp_reset_postdata(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment