public function getItemImages() { $this->item['item_imgs'] = array(); $name = basename($this->product->getImageUrl()); if ($images = $this->product->getMediaGalleryImages()) { if (count($images) > 0) { $position = 0; foreach ($images as $image) { $itemImg = array(); $itemImg['img_id'] = $image->getId(); $itemImg['img_url'] = $this->getItemImageUrl($image->getFile()); $itemImg['position'] = ++$position; if ($position > 1 && $name == basename($image->getFile())) { $first = $itemImg; $first['position'] = 0; } else { $this->item['item_imgs'][] = $itemImg; } } if (isset($first)) { $this->item['item_imgs'] = array_merge(array($first), $this->item['item_imgs']); } return $this->item['item_imgs']; } } $itemImg = array(); $itemImg['img_id'] = 0; $itemImg['img_url'] = $this->getItemImageUrl(); $itemImg['position'] = 0; $this->item['item_imgs'][] = $itemImg; return $this->item['item_imgs']; }
protected function _getProductImageFile() { $product = new Mage_Catalog_Model_Product(); $product->load(1); $images = $product->getMediaGalleryImages()->getItems(); $image = reset($images); return $image['file']; }
/** * Get the first image from the image gallery. * * @param Mage_Catalog_Model_Product $product * * @return string */ private function _getImageFromGallery(Mage_Catalog_Model_Product $product) { $gallery = $product->getMediaGalleryImages(); if (!$gallery->count()) { return ''; } $image = $gallery->getFirstItem(); if (!$image || !$image->hasFile()) { return ''; } return $image->getFile(); }
/** * @param Mage_Catalog_Model_Product $product * @return array */ protected function _getGalleryImageItems(Mage_Catalog_Model_Product $product) { try { $galleryImagesCollection = $product->getMediaGalleryImages(); } catch (Exception $e) { // Do nothing here under the assumption that this product has no gallery images return array(); } if (!is_object($galleryImagesCollection) || $galleryImagesCollection->count() < 1) { return array(); } return $galleryImagesCollection->getItems(); }
public function testGetMediaGalleryImages() { /** @var $model Mage_Catalog_Model_Product */ $model = Mage::getModel('Mage_Catalog_Model_Product'); $this->assertEmpty($model->getMediaGalleryImages()); $this->_model->setMediaGallery(array('images' => array(array('file' => 'magento_image.jpg')))); $images = $this->_model->getMediaGalleryImages(); $this->assertInstanceOf('Varien_Data_Collection', $images); foreach ($images as $image) { $this->assertInstanceOf('Varien_Object', $image); $image = $image->getData(); $this->assertArrayHasKey('file', $image); $this->assertArrayHasKey('url', $image); $this->assertArrayHasKey('id', $image); $this->assertArrayHasKey('path', $image); $this->assertStringEndsWith('magento_image.jpg', $image['file']); $this->assertStringEndsWith('magento_image.jpg', $image['url']); $this->assertStringEndsWith('magento_image.jpg', $image['path']); } }
/** * Get alternative image HTML of the given product * * @param Mage_Catalog_Model_Product $product Product * @param int $w Image width * @param int $h Image height * @param string $imgVersion Image version: image, small_image, thumbnail * @return string */ public function getAltImgHtml($product, $w, $h, $imgVersion = 'small_image') { $column = $this->getCfg('category/alt_image_column'); $value = $this->getCfg('category/alt_image_column_value'); $product->load('media_gallery'); if ($gal = $product->getMediaGalleryImages()) { if ($altImg = $gal->getItemByColumnValue($column, $value)) { return '<img class="alt-img" src="' . Mage::helper('infortis/image')->getImg($product, $w, $h, $imgVersion, $altImg->getFile()) . '" alt="' . $product->getName() . '" />'; } } return ''; }
/** * Get alternative image HTML of the given product * * @param Mage_Catalog_Model_Product $product Product * @param int $w Image width * @param int $h Image height * @param string $imgVersion Image version: image, small_image, thumbnail * @return string */ public function getAltImgHtml($product, $w, $h, $imgVersion = 'small_image') { $column = $this->getConfig('category_product/alt_image_column'); $value = $this->getConfig('category_product/alt_image_column_value'); $product->load('media_gallery'); if ($gal = $product->getMediaGalleryImages()) { $altImg = $gal->getItemByColumnValue($column, $value); if (!$altImg) { $altImg = $gal->getItemByColumnValue('position', 1); } if (isset($altImg) && $altImg->getFile()) { return Mage::helper('themesettings/image')->getImg($product, $w, $h, $imgVersion, $altImg->getFile()); } else { return ''; } } return ''; }
/** * extracting image data from a given Mage_Catalog_Model_Product object * @param Mage_Catalog_Model_Product $product * @return array | null */ protected function _extractImageData(Mage_Catalog_Model_Product $product) { $media = $product->getMediaGalleryImages(); return $media && $media->count() ? ['id' => $product->getSku(), 'image_data' => $this->_getMediaData($media, $product)] : null; }
/** * Returns data about images associated with product * @param Mage_Catalog_Model_Product $product * @return array|null */ protected function _createImageUrls(Mage_Catalog_Model_Product $product) { $result = array(); //get media images associated with product $images = $product->getMediaGalleryImages(); if (!empty($images)) { foreach ($images as $image) { $isDeleted = $image->getRemoved(); if (!$isDeleted) { $label = $image->getLabel(); $data = array('url' => $this->_getNotSecureImageUrl($image->getUrl()), 'height' => null, 'width' => null, 'label' => $label ? $this->_createLocalizedValue($label) : null, 'altText' => null, 'tags' => null); $result[] = $data; } } } $image = $product->getData('image'); if (!empty($image) && $image != 'no_selection') { $imageUrl = (string) Mage::helper('catalog/image')->init($product, 'image'); $label = $product->getData('image_label'); $data = array('url' => $this->_getNotSecureImageUrl($imageUrl), 'height' => null, 'width' => null, 'label' => $label ? $this->_createLocalizedValue($label) : null, 'altText' => null, 'tags' => null); $result[] = $data; } $image = $product->getData('small_image'); if (!empty($image) && $image != 'no_selection') { $imageUrl = (string) Mage::helper('catalog/image')->init($product, 'small_image'); $label = $product->getData('small_image_label'); $data = array('url' => $this->_getNotSecureImageUrl($imageUrl), 'height' => null, 'width' => null, 'label' => $label ? $this->_createLocalizedValue($label) : null, 'altText' => null, 'tags' => null); $result[] = $data; } $image = $product->getData('thumbnail'); if (!empty($image) && $image != 'no_selection') { $imageUrl = (string) Mage::helper('catalog/image')->init($product, 'thumbnail'); $label = $product->getData('thumbnail_label'); $data = array('url' => $this->_getNotSecureImageUrl($imageUrl), 'height' => null, 'width' => null, 'label' => $label ? $this->_createLocalizedValue($label) : null, 'altText' => null, 'tags' => array('THUMBNAIL')); $result[] = $data; } return count($result) ? $result : null; }
public function getObject(Mage_Catalog_Model_Product $product) { $this->logger->start('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); $this->logger->log('Product type (' . $product->getTypeId() . ')'); $defaultData = array(); $transport = new Varien_Object($defaultData); Mage::dispatchEvent('algolia_product_index_before', array('product' => $product, 'custom_data' => $transport)); $defaultData = $transport->getData(); $defaultData = is_array($defaultData) ? $defaultData : explode("|", $defaultData); $customData = array('objectID' => $product->getId(), 'name' => $product->getName(), 'url' => $product->getProductUrl()); $additionalAttributes = $this->config->getProductAdditionalAttributes($product->getStoreId()); $groups = null; if ($this->isAttributeEnabled($additionalAttributes, 'description')) { $customData['description'] = $product->getDescription(); } $categories = array(); $categories_with_path = array(); $_categoryIds = $product->getCategoryIds(); if (is_array($_categoryIds) && count($_categoryIds) > 0) { $categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')->addAttributeToFilter('entity_id', $_categoryIds)->addIsActiveFilter(); foreach ($categoryCollection as $category) { $categoryName = $category->getName(); if ($categoryName) { $categories[] = $categoryName; } $category->getUrlInstance()->setStore($product->getStoreId()); $path = array(); foreach ($category->getPathIds() as $treeCategoryId) { $name = $this->getCategoryName($treeCategoryId, $product->getStoreId()); if ($name) { $path[] = $name; } } $categories_with_path[] = $path; } } foreach ($categories_with_path as $result) { for ($i = count($result) - 1; $i > 0; $i--) { $categories_with_path[] = array_slice($result, 0, $i); } } $categories_with_path = array_intersect_key($categories_with_path, array_unique(array_map('serialize', $categories_with_path))); $categories_hierarchical = array(); $level_name = 'level'; foreach ($categories_with_path as $category) { for ($i = 0; $i < count($category); $i++) { if (isset($categories_hierarchical[$level_name . $i]) === false) { $categories_hierarchical[$level_name . $i] = array(); } $categories_hierarchical[$level_name . $i][] = implode(' /// ', array_slice($category, 0, $i + 1)); } } foreach ($categories_hierarchical as &$level) { $level = array_values(array_unique($level)); } foreach ($categories_with_path as &$category) { $category = implode(' /// ', $category); } $customData['categories'] = $categories_hierarchical; $customData['categories_without_path'] = $categories; if (false === isset($defaultData['thumbnail_url'])) { try { $customData['thumbnail_url'] = $product->getThumbnailUrl(); $customData['thumbnail_url'] = str_replace(array('https://', 'http://'), '//', $customData['thumbnail_url']); } catch (\Exception $e) { } } if (false === isset($defaultData['image_url'])) { try { $customData['image_url'] = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getImage()); $customData['image_url'] = str_replace(array('https://', 'http://'), '//', $customData['image_url']); } catch (\Exception $e) { } if ($this->isAttributeEnabled($additionalAttributes, 'media_gallery')) { $product->load('media_gallery'); $customData['media_gallery'] = array(); foreach ($product->getMediaGalleryImages() as $image) { $customData['media_gallery'][] = str_replace(array('https://', 'http://'), '//', $image->getUrl()); } } } $sub_products = null; $ids = null; if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped' || $product->getTypeId() == 'bundle') { if ($product->getTypeId() == 'bundle') { $ids = array(); $selection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product); foreach ($selection as $option) { $ids[] = $option->product_id; } } if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped') { $ids = $product->getTypeInstance(true)->getChildrenIds($product->getId()); } if (count($ids)) { $sub_products = $this->getProductCollectionQuery($product->getStoreId(), $ids, false)->load(); } else { $sub_products = array(); } } if (false === isset($defaultData['in_stock'])) { $stockItem = $product->getStockItem(); $customData['in_stock'] = (int) $stockItem->getIsInStock(); } // skip default calculation if we have provided these attributes via the observer in $defaultData if (false === isset($defaultData['ordered_qty']) && $this->isAttributeEnabled($additionalAttributes, 'ordered_qty')) { $customData['ordered_qty'] = (int) $product->getOrderedQty(); } if (false === isset($defaultData['stock_qty']) && $this->isAttributeEnabled($additionalAttributes, 'stock_qty')) { $customData['stock_qty'] = (int) $product->getStockQty(); } if (Mage::helper('core')->isModuleEnabled('Mage_Review')) { if ($this->isAttributeEnabled($additionalAttributes, 'rating_summary')) { $customData['rating_summary'] = (int) $product->getRatingSummary(); } } foreach ($additionalAttributes as $attribute) { if (isset($customData[$attribute['attribute']])) { continue; } $value = $product->getData($attribute['attribute']); $attribute_ressource = $product->getResource()->getAttribute($attribute['attribute']); if ($attribute_ressource) { $attribute_ressource = $attribute_ressource->setStoreId($product->getStoreId()); if ($value === null) { /** Get values as array in children */ if ($product->getTypeId() == 'configurable' || $product->getTypeId() == 'grouped' || $product->getTypeId() == 'bundle') { $values = array(); foreach ($sub_products as $sub_product) { $stock = (int) $sub_product->getStockItem()->getIsInStock(); if ($stock == false) { continue; } $value = $sub_product->getData($attribute['attribute']); if ($value) { $value_text = $sub_product->getAttributeText($attribute['attribute']); if ($value_text) { $values[] = $value_text; } else { $values[] = $attribute_ressource->getFrontend()->getValue($sub_product); } } } if (count($values) > 0) { $customData[$attribute['attribute']] = array_values(array_unique($values)); } } } else { $value_text = $product->getAttributeText($attribute['attribute']); if ($value_text) { $value = $value_text; } else { $attribute_ressource = $attribute_ressource->setStoreId($product->getStoreId()); $value = $attribute_ressource->getFrontend()->getValue($product); } if ($value) { $customData[$attribute['attribute']] = $value; } } } } $this->handlePrice($product, $sub_products, $customData); if ($this->config->isCustomerGroupsEnabled()) { $groups = Mage::getModel('customer/group')->getCollection(); foreach ($groups as $group) { $group_id = (int) $group->getData('customer_group_id'); $this->handlePrice($product, $sub_products, $customData, $groups, $group_id); } } $transport = new Varien_Object($customData); Mage::dispatchEvent('algolia_subproducts_index', array('custom_data' => $transport, 'sub_products' => $sub_products)); $customData = $transport->getData(); $customData = array_merge($customData, $defaultData); $customData['type_id'] = $product->getTypeId(); $this->castProductObject($customData); $this->logger->stop('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); return $customData; }