Jump to content

Get products with specific feature values


svierkant

Recommended Posts

I've made a custom page with a form, so customers can easily search for all products that are meet customer's needs.

 

It was easy to make that form, based on the features:

 

//controller
$network = FeatureValue::getFeatureValuesWithLang($this->context->language->id, 6);

//template
<select name="network">
{foreach from=$options.network item=network name=network}
<option value="{$network.id_feature_value}">{$network.value}</option>
{/foreach}
</select>

 

Now I would like to get all products that have the submitted features. I can't believe there isn't a function that does the trick, but I can't find it. How can I get products with specific tags/features?

 

(I'm new to PrestaShop, I think I'm looking for a WP_Query equivalent)

  • Like 1
Link to comment
Share on other sites

Hi svierkant,

just an idea, haven't tested this at all but maybe try something like loading products into controller and then:

{foreach from=$products item=product name=products}
 {if isset($product.features)}
  {foreach from=$product.features item=feature}
	  {if $feature.name == "$network.value"}
		   [show_product]
	  {/if}
   {/foreach}
  {/if}
{/foreach}

 

what you could also try is the "layered navigation block" module, might be exactly what you are looking for.

 

good luck :)

Edited by defuzed (see edit history)
  • Like 1
Link to comment
Share on other sites

Thanks for your reply!

 

That's an easy solution*, but is there not a function for that? I expected a more OOP-like solution to the problem, so it's possible to re-use code. It would be nice if I can use the 'standard' product list view (with sorting, paging, etc.).

 

I don't want to use the layered navigation block, because we want to make a custom wizard. The product selection is not that easy (it's a very complex product), so we want to make it as easy as possible.

 

* It's better to use the controller to select the products, isn't it?

Link to comment
Share on other sites

The reusable option would be to pack all of this into a module which you could "hook" anywhere you want. Have a look at: http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module and/or just take a look at existing modules to see how they work.

Your custom wizard definitely seems like it should be packed into a module.

If you need any more specific help with module creation etc don't hesitate to post here or pm me :)

Link to comment
Share on other sites

I already made a module: my question is how I can get products with specified feature values.

 

I'm sure it will work the way I'm doing it now, but are you really sure that this is the right way to do? I think it's better to select the products using MySQL instead of PHP. Or isn't it?

Edited by svierkant (see edit history)
Link to comment
Share on other sites

I think this question is related: http://stackoverflow.com/questions/9150417/how-to-get-in-prestashop-filtered-products-by-its-attribute ("I'm wondering about method to retrieve that filtered products directly, where filtering is doing in database.")

 

So it looks there isn't a function for it. Is it OK to do it with PHP, or is it better to write a function that gets the products directly from the database?

  • Like 1
Link to comment
Share on other sites

the stackoverflow accepted answer looks really good. I think you're going to have to use SQL since there is no query for specific attributes as it seems. I think it's usually better to use php if SQL queries are already available, but since there doesn't seem to be one for what you are trying to do, try the stackoverflow solution.

Really interesting question overall since what you want to do is a great idea usability-wise, hope you can figure it out, i will try to look into it more tonight. good luck!

  • Like 1
Link to comment
Share on other sites

Is there a specific reason there isn't a WP_Query equivalent? Using specific products on another page isn't a unusual question, or is it?

 

I hope you don't feel offended that I needed an extra source to verify your answer? ;-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...