Skip to content

Commit 727da54

Browse files
authored
Update get product images (#823)
1 parent dd0374d commit 727da54

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Helper/Image.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Helper;
44

55
use Magento\Catalog\Model\Product\ImageFactory;
6+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ProductTypeConfigurable;
67
use Magento\Framework\App\Helper\Context;
78
use Magento\Framework\View\Asset\Repository;
89
use Magento\Framework\View\ConfigInterface;
@@ -61,6 +62,50 @@ public function getUrl()
6162
return $url;
6263
}
6364

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+
64109
public function removeProtocol($url)
65110
{
66111
return str_replace(['https://', 'http://'], '//', $url);

0 commit comments

Comments
 (0)