0

I have a view with a custom template and I need to query my products using some conditions. I'm running a videogame store, so I have a Content Type and Production Variation called "Games", and those games have a plataform assigned (Content Type field). So now I'm creating a custom PS3 categorie view and I need to query only PS3 games.

This is what I have currently:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_product')
    ->propertyCondition('status', 1)
    ->propertyCondition('type', 'games')
    ->fieldOrderBy('commerce_price', 'amount', 'ASC');
$result = $query->execute();

That query returns a list of "games", but I need to filter them by plataform.

PS: I don't know if any one can share information about querying products in Commerce. I have to say that Commerce API is very poorly explained.

1 Answer 1

2

Commerce Products are entities, so you can query them exactly like any other entity (like you're already doing). You may consider the commerce API docs for entities to be poor, but that's simply because there aren't any - you can apply standard Drupal entity logic.

It seems the only thing you're missing is the platform filter that you mention; that's just a matter of adding a field condition to the EntityFieldQuery:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_product')
  ->propertyCondition('status', 1)
  ->propertyCondition('type', 'games')
  ->fieldCondition('field_platform', 'value', 'PS3')
  ->fieldOrderBy('commerce_price', 'amount', 'ASC');
$result = $query->execute();

Your mileage may vary if field_platform isn't a text field. For example, if it's a taxonomy term reference, you'll need to find the ID of the taxonomy term and use something like

  ->fieldCondition('field_platform', 'tid', 123)

Or if it's an entity reference field you'll need the entity ID and to use something like this

  ->fieldCondition('field_platform', 'target_id', 123)
5
  • I tried your solution, but it's not working :(.. ·%$##$· editor!!
    – nikoskip
    Oct 7, 2013 at 14:52
  • You'll need to change the names/types of fields to suit your installation, you won't be able to just copy-paste the code above
    – Clive
    Oct 7, 2013 at 14:52
  • Yeah, I adapted the above code to my requirements. Without platform filter, returns something: pastebin.com/kL3D4sSD But using platform filter, returns and empty array: pastebin.com/6RNJbeWh
    – nikoskip
    Oct 7, 2013 at 14:57
  • As I can understand (I'm new working with Drupal), the field 'platform' is part of the Content Type "Games", so is not part of the Node. Query above is trying to filter the Node using a field that doesn't exists in Node.
    – nikoskip
    Oct 7, 2013 at 15:03
  • Ah ok, you'll need to run a custom query for that - EntityFieldQuery can't join across multiple entities, just one level at a time
    – Clive
    Oct 7, 2013 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.