Custom related products query

Hello,

I’m trying to create custom related products with this post as a reference - Looper for related products

The only problem is that the query from that post doesn’t display the same products as when there is a related product element.

I’m not sure what that query does, but how can I get the same products from the element?

Best regards,

Hello @ofirdoo,

Thanks for writing to us.

I would suggest you please use the looper provider as a query string. You can add this query string for the related product.

product_cat={{dc:term:slug}}&post_type=product&posts_per_page=3&offset=0&ignore_sticky_posts=1&post__not_in%5B0%5D={{dc:post:id}}

Hope it helps
Thanks

Hello again,

I’m using that but the only difference is post_per_page=-1 to show all.

If you want I can give you access to check out set up of looper.

Best regards

Hi @ofirdoo,

You can share the login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password
– Specific page or Layout and the specific section where you used the query.

To create a secure note, click the key icon underneath your posts.

Thanks

Hello,

I put login details in secure note.

Best regards

Hello @ofirdoo,

Please note that when you insert the query string you need to add the integer value for the post per page. I checked your site, and it seems that you have set the integer value and it is working fine.

Thanks

Hi again.

But the results are not ok. Please check products from my loop and below from the theme element. Completely different products shows as related.

Best regards

Hi @ofirdoo,

I have checked and found that in the Related Product section you are using the Product Categories option in the Looper Provider. I can check the Test product is showing the correct products in the Slider. If that is not the case, can you please elaborate and explain why the result of the looper is not correct for that? It will help us to understand it.

Thanks

Hi,

I added the link for the product in a secure note as that is the dev version. On that product, you can see the difference in my looper and from theme element.

Hello Ofir,

The Looper Provider Query String cannot return the exact product items. The argument for getting the related products is this:

$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 5, // Number of related products to display
    'post__in' => wc_get_related_products( $product_id, 5 ), // Get related products
    'orderby' => 'rand' // Order by random
);

You cannot use this argument for a query string because of this wc_get_related_products() function. Therefore, it is better to create your own Looper Provider Custom instead. If you are not familiar with this type of Looper, kindly check out this documentation for your reference:

Kindly let us know how it goes.

Hi,

I failed on the first step. When I put this code from the documentation, I get a PHP error.

add_filter( 'cs_looper_custom_my_data', function( $result ) { return [ [ 'text' => 'This is some text for the first item.' ], [ 'text' => 'This is some text for the second item.' ] ] });

Is there some mistake inside code or?

Hi Ofir,

You need to add the semicolon at the end of the line where ] ends. You can copy the following code for that:

add_filter( 'cs_looper_custom_my_data', function( $result ) {
    return [
        [ 'text' => 'This is some text for the first item.' ],
        [ 'text' => 'This is some text for the second item.' ]
    ];
});

Use the following Dynamic Content to get the data:

{{dc:looper:field key="text"}}

Then you can add the code suggested by my colleague.

Hope it helps.
Thanks

Hi,

So I’m guessing I need ruenel args code inside that example from documentation?

add_filter( ‘cs_looper_custom_my_data’, function( $result ) {
return [
$args = array(
‘post_type’ => ‘product’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1, // Number of related products to display
‘post__in’ => wc_get_related_products( $product_id, -1 ), // Get related products
‘orderby’ => ‘rand’ // Order by random
);
];
});

But this also returns an error.

Hi Ofir,

You need to send the output of using the code to get the result and return it. If you are not proficient in this, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.

Thanks

Do you mean write a custom loop and then get out via var_dump( http_build_query( $query ) );?

Hi Ofir,

You need to use the argument array as a parameter to the WP_Query and format it according to your looper. Please find the example in the following thread.

Please remember that this comes under custom development and beyond the scope of Theme Support.

Hope it helps.
Thanks

Hello,

thank you. I will check that with another developer.

Hi Ofir,

You are most welcome.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.