Discussions

Item images and links in database

I am student using drupal commerce as a part of my thesis.

As an exercise in PHP I am attempting to display certain items on a page that fall into specific criteria. I know this is possible through Views and Taxonomy, but again this is to show/illustrate an understanding of PHP.

While reading through some related posts, a contributor had said that the specific product-page urls, as well as the image urls, are stored within the database. However, I have been unable to find them.

Was the guy who posted this correct? Could someone help me find where that data is stored?

Posted: Mar 25, 2014

Comments

joshmiller Josh Miller on March 27, 2014

So, in Drupal you almost never want to directly access the database. I'm not a module developer, but I hacked the following together using Entity Field Query...

<?php
// Find products of certain attributes
$query = new EntityFieldQuery(); // using EFQ
$products = $query->entityCondition('entity_type', 'commerce_product')
                    ->
entityCondition('bundle', 'product') // find all the products of type "product"
                   
->propertyCondition('status', 1)
                    ->
execute();
foreach (
$products['commerce_product'] as $product) {
 
$product_entity = commerce_product_load($product->product_id);
 
// Image fields attached to products could be found here, using devel:
 
dpm($product_entity);
 
// Find a product page linked to a product
 
$query2 = new EntityFieldQuery(); // using EFQ
 
$product_pages = $query2->entityCondition('entity_type', 'node')
                      ->
entityCondition('bundle', 'product_display')
                      ->
fieldCondition('field_product','product_id',$product->product_id) // find all the products of type "product"
                     
->propertyCondition('status', 1)
                      ->
execute();
  foreach (
$product_pages['node'] as $page) {
   
$page_entity = node_load($page->nid);
   
// Find a url to this product
   
$options = array('absolute' => TRUE);
   
dpm(url('node/' . $page_entity->nid, $options));
  }
}
?>

It requires understanding Drupal and Drupal Commerce and of course PHP. For example, I'm using Entity Field Query to pull in a list of products based on certain field values. You could easily filter based on product attributes. Once we have Product IDs, we do a full entity load to access the images on the product. Note that some Commerce stores choose to store their product images on a node level or a product entity level. This assumes we're storing them on a product entity level.

Once we have our image, we pull in a list of nodes that display this particular product. Again, we need to know that products are referenced from nodes using a certain field. In this case, it's called "field_product" and I would assume there's only one page per product, but Commerce makes no such assumptions, so we don't either and we loop through all the possibilities that we collected with our second EFQ.