/**
  * Configure environment for `testSave` and `testSaveAndDuplicate` methods
  *
  * @return array
  */
 protected function configureSaveTest()
 {
     $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\Simple')->disableOriginalConstructor()->setMethods(['beforeSave', 'save'])->getMock();
     $productTypeMock->expects($this->once())->method('beforeSave')->will($this->returnSelf());
     $productTypeMock->expects($this->once())->method('save')->will($this->returnSelf());
     $this->productTypeInstanceMock->expects($this->once())->method('factory')->with($this->model)->will($this->returnValue($productTypeMock));
     $this->model->getResource()->expects($this->any())->method('addCommitCallback')->will($this->returnSelf());
     $this->model->getResource()->expects($this->any())->method('commit')->will($this->returnSelf());
 }
Example #2
0
 /**
  * Get product tier price by qty
  *
  * @param   float $qty
  * @param   Product $product
  * @return  float|array
  * @deprecated (MAGETWO-31465)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function getTierPrice($qty, $product)
 {
     $allGroups = $this->_groupManagement->getAllCustomersGroup()->getId();
     $prices = $product->getData('tier_price');
     if ($prices === null) {
         $attribute = $product->getResource()->getAttribute('tier_price');
         if ($attribute) {
             $attribute->getBackend()->afterLoad($product);
             $prices = $product->getData('tier_price');
         }
     }
     if ($prices === null || !is_array($prices)) {
         if ($qty !== null) {
             return $product->getPrice();
         }
         return [['price' => $product->getPrice(), 'website_price' => $product->getPrice(), 'price_qty' => 1, 'cust_group' => $allGroups]];
     }
     $custGroup = $this->_getCustomerGroupId($product);
     if ($qty) {
         $prevQty = 1;
         $prevPrice = $product->getPrice();
         $prevGroup = $allGroups;
         foreach ($prices as $price) {
             if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) {
                 // tier not for current customer group nor is for all groups
                 continue;
             }
             if ($qty < $price['price_qty']) {
                 // tier is higher than product qty
                 continue;
             }
             if ($price['price_qty'] < $prevQty) {
                 // higher tier qty already found
                 continue;
             }
             if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) {
                 // found tier qty is same as current tier qty but current tier group is ALL_GROUPS
                 continue;
             }
             if ($price['website_price'] < $prevPrice) {
                 $prevPrice = $price['website_price'];
                 $prevQty = $price['price_qty'];
                 $prevGroup = $price['cust_group'];
             }
         }
         return $prevPrice;
     } else {
         $qtyCache = [];
         foreach ($prices as $priceKey => $price) {
             if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) {
                 unset($prices[$priceKey]);
             } elseif (isset($qtyCache[$price['price_qty']])) {
                 $priceQty = $qtyCache[$price['price_qty']];
                 if ($prices[$priceQty]['website_price'] > $price['website_price']) {
                     unset($prices[$priceQty]);
                     $qtyCache[$price['price_qty']] = $priceKey;
                 } else {
                     unset($prices[$priceKey]);
                 }
             } else {
                 $qtyCache[$price['price_qty']] = $priceKey;
             }
         }
     }
     return $prices ? $prices : [];
 }
Example #3
0
 /**
  * Return assigned images for specific stores
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param int|array $storeIds
  * @return array
  *
  */
 public function getAssignedImages($product, $storeIds)
 {
     if (!is_array($storeIds)) {
         $storeIds = [$storeIds];
     }
     $mainTable = $product->getResource()->getAttribute('image')->getBackend()->getTable();
     $connection = $this->getConnection();
     $select = $connection->select()->from(['images' => $mainTable], ['value as filepath', 'store_id'])->joinLeft(['attr' => $this->getTable('eav_attribute')], 'images.attribute_id = attr.attribute_id', ['attribute_code'])->where('entity_id = ?', $product->getId())->where('store_id IN (?)', $storeIds)->where('attribute_code IN (?)', ['small_image', 'thumbnail', 'image']);
     $images = $connection->fetchAll($select);
     return $images;
 }
Example #4
0
 /**
  * Get product tier price by qty
  *
  * @param   float                    $qty
  * @param   \Magento\Catalog\Model\Product $product
  * @return  float|array
  */
 public function getTierPrice($qty, $product)
 {
     $allGroups = CustomerGroupServiceInterface::CUST_GROUP_ALL;
     $prices = $product->getData('tier_price');
     if (is_null($prices)) {
         if ($attribute = $product->getResource()->getAttribute('tier_price')) {
             $attribute->getBackend()->afterLoad($product);
             $prices = $product->getData('tier_price');
         }
     }
     if (is_null($prices) || !is_array($prices)) {
         if (!is_null($qty)) {
             return $product->getPrice();
         }
         return array(array('price' => $product->getPrice(), 'website_price' => $product->getPrice(), 'price_qty' => 1, 'cust_group' => $allGroups));
     }
     $custGroup = $this->_getCustomerGroupId($product);
     if ($qty) {
         $prevQty = 1;
         $prevPrice = 0;
         $prevGroup = $allGroups;
         foreach ($prices as $price) {
             if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) {
                 // tier not for current customer group nor is for all groups
                 continue;
             }
             if ($qty < $price['price_qty']) {
                 // tier is higher than product qty
                 continue;
             }
             if ($price['price_qty'] < $prevQty) {
                 // higher tier qty already found
                 continue;
             }
             if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) {
                 // found tier qty is same as current tier qty but current tier group is ALL_GROUPS
                 continue;
             }
             if ($price['website_price'] > $prevPrice) {
                 $prevPrice = $price['website_price'];
                 $prevQty = $price['price_qty'];
                 $prevGroup = $price['cust_group'];
             }
         }
         return $prevPrice;
     } else {
         $qtyCache = array();
         foreach ($prices as $i => $price) {
             if ($price['cust_group'] != $custGroup && $price['cust_group'] != $allGroups) {
                 unset($prices[$i]);
             } else {
                 if (isset($qtyCache[$price['price_qty']])) {
                     $j = $qtyCache[$price['price_qty']];
                     if ($prices[$j]['website_price'] < $price['website_price']) {
                         unset($prices[$j]);
                         $qtyCache[$price['price_qty']] = $i;
                     } else {
                         unset($prices[$i]);
                     }
                 } else {
                     $qtyCache[$price['price_qty']] = $i;
                 }
             }
         }
     }
     return $prices ? $prices : array();
 }
Example #5
0
 /**
  * Retrieve small image url
  *
  * @param ModelProduct|\Magento\Framework\DataObject $product
  * @return string|bool
  */
 public function getSmallImageUrl($product)
 {
     $url = false;
     $attribute = $product->getResource()->getAttribute('small_image');
     if (!$product->getSmallImage()) {
         $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/small_image.jpg');
     } elseif ($attribute) {
         $url = $attribute->getFrontend()->getUrl($product);
     }
     return $url;
 }
Example #6
0
 /**
  * Remove don't applicable attributes data
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return void
  */
 protected function _removeNotApplicableAttributes($product)
 {
     $entityType = $product->getResource()->getEntityType();
     foreach ($this->_eavConfig->getEntityAttributeCodes($entityType, $product) as $attributeCode) {
         $attribute = $this->_eavConfig->getAttribute($entityType, $attributeCode);
         $applyTo = $attribute->getApplyTo();
         if (is_array($applyTo) && count($applyTo) > 0 && !in_array($product->getTypeId(), $applyTo)) {
             $product->unsetData($attribute->getAttributeCode());
         }
     }
 }
Example #7
0
 /**
  * Gets the 'tear_price' array from the product
  *
  * @param Product $product
  * @param string $key
  * @param bool $returnRawData
  * @return array
  */
 protected function getExistingPrices($product, $key, $returnRawData = false)
 {
     $prices = $product->getData($key);
     if ($prices === null) {
         $attribute = $product->getResource()->getAttribute($key);
         if ($attribute) {
             $attribute->getBackend()->afterLoad($product);
             $prices = $product->getData($key);
         }
     }
     if ($prices === null || !is_array($prices)) {
         return $returnRawData ? $prices : [];
     }
     return $prices;
 }
 /**
  * {@inheritdoc}
  */
 public function getResource()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getResource');
     if (!$pluginInfo) {
         return parent::getResource();
     } else {
         return $this->___callPlugins('getResource', func_get_args(), $pluginInfo);
     }
 }
Example #9
0
 /**
  * Save image information to DB.
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param array $images
  * @return void
  */
 protected function storeImage($product, $images)
 {
     $baseImage = '';
     $i = 1;
     $mediaAttribute = $this->eavConfig->getAttribute('catalog_product', 'media_gallery');
     foreach ($images as $image) {
         if (empty($image)) {
             $this->errors[] = $product->getSku();
             continue;
         }
         if (strpos($image, '_main') !== false) {
             $baseImage = $image;
         }
         $id = $this->galleryResource->insertGallery(['attribute_id' => $mediaAttribute->getAttributeId(), 'entity_id' => $product->getId(), 'value' => $image]);
         $this->galleryResource->insertGalleryValueInStore(['value_id' => $id, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, 'entity_id' => $product->getId(), 'label' => 'Image', 'position' => $i, 'disables' => 0]);
         $this->galleryResource->bindValueToEntity($id, $product->getId());
         $i++;
     }
     if (empty($baseImage)) {
         $baseImage = $images[0];
     }
     if ($baseImage) {
         $imageAttribute = $product->getResource()->getAttribute('image');
         $smallImageAttribute = $product->getResource()->getAttribute('small_image');
         $thumbnailAttribute = $product->getResource()->getAttribute('thumbnail');
         $adapter = $product->getResource()->getConnection();
         foreach ([$imageAttribute, $smallImageAttribute, $thumbnailAttribute] as $attribute) {
             $table = $imageAttribute->getBackend()->getTable();
             /** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter*/
             $data = [$attribute->getBackend()->getEntityIdField() => $product->getId(), 'attribute_id' => $attribute->getId(), 'value' => $baseImage];
             $adapter->insertOnDuplicate($table, $data, ['value']);
         }
     }
 }
Example #10
0
 /**
  * Return assigned images for specific stores
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param int|array $storeIds
  * @return array
  *
  */
 public function getAssignedImages($product, $storeIds)
 {
     if (!is_array($storeIds)) {
         $storeIds = array($storeIds);
     }
     $mainTable = $product->getResource()->getAttribute('image')->getBackend()->getTable();
     $read = $this->_getReadAdapter();
     $select = $read->select()->from(array('images' => $mainTable), array('value as filepath', 'store_id'))->joinLeft(array('attr' => $this->getTable('eav_attribute')), 'images.attribute_id = attr.attribute_id', array('attribute_code'))->where('entity_id = ?', $product->getId())->where('store_id IN (?)', $storeIds)->where('attribute_code IN (?)', array('small_image', 'thumbnail', 'image'));
     $images = $read->fetchAll($select);
     return $images;
 }