A few more notes on Drupal Commerce

I add a little more code to more understand the receipt of some data from the Drupal Commerce product.

Below will be a fragment of the code for the custom module of which the purpose of which is to assemble the ideids of products with a specific field, and then get url images from this field, the name of the product and the link to this product.

$pids = \Drupal::entityQuery('commerce_product')->condition('field_slide_img.target_id', '0', '>')->execute();
   $products =  \Drupal\commerce_product\Entity\Product::loadMultiple($pids);
   $product_list = [];
   foreach ($products as $key => $value) {
       $num = $key;
       $aliasManager = \Drupal::service('path_alias.manager');
       $product_list[$num]['link'] = $aliasManager->getAliasByPath('/product/' . $value->product_id->value);

       $product_list[$num]['name'] = $value->title->value;

       $product_list[$num]['image'] = NULL;
       if ($value->field_slide_img->target_id) {
           $product_image_file = File::load($value->field_slide_img->target_id);
           $product_image_file_url = $product_image_file->createFileUrl();
           $product_list[$num]['image'] = $product_image_file_url;
       }
   }
   ksort($product_list);
   return $product_list;

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.