L
L
Leonid2018-02-21 17:58:40
WordPress
Leonid, 2018-02-21 17:58:40

"Featured Products" in WooCommerce from the same category and with the same attribute value - how?

How to make it so that on the product page (in the product card) in the Recommended products section, products from the same category and with the same value of a certain product attribute are displayed?
For example, for a jewelry store:
each product has an attribute: metal (silver, gold, etc.)
for example, on the page of a product from the category " Earrings " in which metal = silver , products from the same category should be displayed in the "Recommended" block "Earrings" and with the same attribute value metal = silver .
I probably will not find a ready-made solution for this? Need to code the functionality yourself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
petrmish, 2021-02-25
@petrmish

Here's php, essentially running on shortcode woo. I think ... it is pointless to make comments on the application, everything is already obvious, the conclusion of everything is by color, model. Here are two examples with and without a title. I would like to note that it also displays the current product. The code only considers attributes.

function design_shortcode() {

$terms = get_the_terms($post->ID, 'pa_design');
if ($terms && !is_wp_error($terms)) {
    foreach ($terms as $term)
        $design_name = $term->name;
        $design_slug = $term->slug;
}

if ($design_name != ''){
    echo do_shortcode( '[product_attribute attribute="design" filter="'.$design_slug.'" per_page="6" columns="6" orderby="rand" order="rand"]' ); }
}
add_shortcode('alldesing', 'design_shortcode');

function primary_color_shortcode() {
    
$terms = get_the_terms($post->ID, 'pa_primary-color');
if ($terms && !is_wp_error($terms)) {
    foreach ($terms as $term)
        $primary_color_name = $term->name;
        $primary_color_slug = $term->slug;
}

if ($primary_color_name != ''){
    echo '<h3 class="main-title uppercase" align="left">Другие товары в цвете '.$primary_color_name.'</h2>'
    echo do_shortcode( '[product_attribute attribute="primary-color" filter="'.$primary_color_slug.'" per_page="6" columns="6" orderby="rand" order="rand"]' ); }
}
add_shortcode('allprimarycolor', 'primary_color_shortcode');

I
its2easyy, 2018-03-06
@its2easyy

MB try to get the category of the current product and the value of the desired attribute and then run wp_query with the same category and attribute restriction and with the desired posts_per_page, and then display the desired products

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question