Discussions

Product select

I am trying to get all the products from my Drupal Commerce site but some are not coming through
Here is the code I am using - but I am getting 1316 rows back and I think that is because I am getting back the physical products and the courses but not the membership type products - how do I get those back please??

914 Items - "Physical Products"
402 Courses
20 Individual Membership
12 Business Membership
1350 Total Catalog

SELECT p.*,n.nid,d.field_product_description_value,d.entity_id,ua.alias FROM commerce_product p left outer JOIN field_data_field_product_reference r on r.field_product_reference_product_id=p.product_id left outer Join node n ON n.nid=r.entity_id left outer join field_data_field_product_description d on d.entity_id = n.nid left outer join url_alias as ua on ua.source=CONCAT('node/',n.nid)

Posted: Sep 2, 2014

Comments

joshmiller Josh Miller on September 2, 2014

Direct SQL statements are not recommended. Entity Field Query can get you there. In this example, I'm using the devel module for it's fancy dpm() functionality. Code was tested in Kickstart 2, which comes with (apparently) 84 products (of various types) out of the box.

<?php
$efq
= new EntityFieldQuery();
$efq->entityCondition('entity_type', 'commerce_product');
$results = $efq->execute();
$products = commerce_product_load_multiple(array_keys($results['commerce_product']));
dpm(count($products));
dpm($products[1]); // replace number with a particular product_id ... or just use a foreach statement, it's an array of objects.
?>

michelle sollicito on September 2, 2014

Wow thanks! I hope I can use this! I tried using EntityFieldQuery but got an error but I think I am working on a fix for that
Class 'EntityFieldQuery' not found in C:\wamp\www\drupal\getentities.php on line 2

joshmiller Josh Miller on September 3, 2014

If you are accessing these products outside of a Drupal environment, you will need to bootstrap Drupal to use entityfieldquery....

<?php
$drupal_dir
= "C:\wamp\www\drupal"; // wherever Drupal is ... not sure Windows paths will work
$current_dir = getcwd();
chdir($drupal_dir);
require_once
'./includes/bootstrap.inc';
define('DRUPAL_ROOT', $drupal_dir);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?>