Example #1
0
 /**
  * Get All products from catalog
  *
  * @param int|null $mage_store_id
  * @param string|null $updated_at_min
  * @param string|null $updated_at_max
  * @param int|null $limit
  * @param int|null $page
  * @param int|null $since_id
  * @param string|null $created_at_min
  * @param string|null $created_at_max
  * @param int|null $product_id
  * @return array $collection
  */
 public function getProducts($mage_store_id, $updated_at_min = null, $updated_at_max = null, $limit = null, $page = null, $since_id = null, $created_at_min = null, $created_at_max = null, $product_id = null)
 {
     $pageNumber = null;
     $pageSize = null;
     $collection = $this->collectionFactory->create();
     $collection->addAttributeToSelect('*');
     if ($mage_store_id !== null) {
         $collection->addStoreFilter($mage_store_id);
     }
     if ($updated_at_min != null) {
         $collection->addAttributeToFilter('updated_at', ['gt' => $updated_at_min]);
     }
     if ($updated_at_max != null) {
         $collection->addAttributeToFilter('updated_at', ['lt' => $updated_at_max]);
     }
     if ($since_id != null) {
         $collection->addAttributeToFilter('entity_id', ['gt' => $since_id]);
     }
     if ($created_at_min != null) {
         $collection->addAttributeToFilter('created_at', ['gt' => $created_at_min]);
     }
     if ($created_at_max != null) {
         $collection->addAttributeToFilter('created_at', ['lt' => $created_at_max]);
     }
     if ($product_id != null) {
         $collection->addAttributeToFilter('entity_id', $product_id);
     }
     if ($limit != null) {
         $pageNumber = 1;
         // Note that page numbers begin at 1
         $pageSize = $limit;
     }
     if ($page != null) {
         if (!is_null($pageSize)) {
             $pageNumber = $page + 1;
             // Note that page numbers begin at 1
         }
     }
     if (!is_null($pageSize)) {
         $collection->setPage($pageNumber, $pageSize);
     }
     $map = $this->response_mask;
     $productsArray = [];
     foreach ($collection as $row) {
         $prod = [];
         $mappedArray = $row->getData();
         if ($row->getCategoryIds()) {
             foreach ($row->getCategoryIds() as $category_id) {
                 $prod['categories'][] = $this->getCategory($category_id);
             }
         }
         //find values from mapping array
         foreach ($map['products'] as $element => $value) {
             if (!is_array($value)) {
                 if (array_key_exists($value, $mappedArray)) {
                     $prod[$element] = $mappedArray[$value];
                 }
             }
         }
         $prod['image'] = $this->getImage($row);
         $prod['images'] = $this->getMediaGalleryImages($row);
         $prod['body_html'] = $row->getDescription();
         $prod['id'] = $row->getId();
         $parent_id = $this->getParentId($row->getId());
         if ($row->getTypeId() == 'simple' && $parent_id) {
             $parentProductData = $this->productFactory->create()->load($parent_id);
             if ($parentProductData->getId()) {
                 $prod['url'] = $parentProductData->getProductUrl();
                 $prod['title'] = $parentProductData->getName();
                 $prod['parent_id'] = $parent_id;
             }
         } else {
             $prod['url'] = $row->getProductUrl();
             $prod['title'] = $row->getName();
         }
         $productsArray[] = $prod;
     }
     $object = new DataObject();
     $object->setProducts($productsArray);
     return $object;
 }