|
3 | 3 | namespace Algolia\AlgoliaSearch\Helper;
|
4 | 4 |
|
5 | 5 | use Magento\Catalog\Model\Product\ImageFactory;
|
| 6 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ProductTypeConfigurable; |
6 | 7 | use Magento\Framework\App\Helper\Context;
|
7 | 8 | use Magento\Framework\View\Asset\Repository;
|
8 | 9 | use Magento\Framework\View\ConfigInterface;
|
@@ -61,6 +62,50 @@ public function getUrl()
|
61 | 62 | return $url;
|
62 | 63 | }
|
63 | 64 |
|
| 65 | + protected function initBaseFile() |
| 66 | + { |
| 67 | + $model = $this->_getModel(); |
| 68 | + $baseFile = $model->getBaseFile(); |
| 69 | + if (!$baseFile) { |
| 70 | + if ($this->getImageFile()) { |
| 71 | + $model->setBaseFile($this->getImageFile()); |
| 72 | + } else { |
| 73 | + $model->setBaseFile($this->getProductImage()); |
| 74 | + } |
| 75 | + } |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Configurable::setImageFromChildProduct() only pulls 'image' type |
| 81 | + * and not the type set by the imageHelper |
| 82 | + * |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + private function getProductImage() |
| 86 | + { |
| 87 | + $imageUrl = $this->getProduct()->getImage(); |
| 88 | + if (!$this->getImageFile() && $this->getType() !== 'image' |
| 89 | + && $this->getProduct()->getTypeId() == ProductTypeConfigurable::TYPE_CODE) { |
| 90 | + $imageUrl = $this->getConfigurableProductImage() ?: $imageUrl; |
| 91 | + } |
| 92 | + |
| 93 | + return $imageUrl; |
| 94 | + } |
| 95 | + |
| 96 | + private function getConfigurableProductImage() |
| 97 | + { |
| 98 | + $childProducts = $this->getProduct()->getTypeInstance()->getUsedProducts($this->getProduct()); |
| 99 | + foreach ($childProducts as $childProduct) { |
| 100 | + $childImageUrl = $childProduct->getData($this->getType()); |
| 101 | + if ($childImageUrl && $childImageUrl !== 'no_selection') { |
| 102 | + return $childImageUrl; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
64 | 109 | public function removeProtocol($url)
|
65 | 110 | {
|
66 | 111 | return str_replace(['https://', 'http://'], '//', $url);
|
|
0 commit comments