Skip to content

Commit

Permalink
Fix issue with Magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Jul 15, 2020
1 parent c6a3af3 commit bde04b0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Plugin/Block/Product/ImagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace MagePal\CatalogLazyLoad\Plugin\Block\Product;

use Closure;
use Magento\Catalog\Block\Product\Image;
use MagePal\CatalogLazyLoad\Helper\Data;

Expand All @@ -30,31 +31,41 @@ public function __construct(

/**
* @param Image $subject
* @param \Closure $proceed
* @param Closure $proceed
* @return mixed
*/
public function aroundToHtml(Image $subject, \Closure $proceed)
public function aroundToHtml(Image $subject, Closure $proceed)
{
if ($this->helper->isEnabled() && $this->helper->applyLazyLoad()) {
$orgImageUrl = $subject->getImageUrl();
$subject->setImageUrl('');

$customAttributes = trim(
$subject->getCustomAttributes() . 'magepal-data-original'
);
//Magento 2.4.0
if (is_array($subject->getCustomAttributes())) {
$customAttributes = array_merge(
$subject->getCustomAttributes(),
['magepal-data-original' => 'placeholder']
);
} else {
$customAttributes = trim(
$subject->getCustomAttributes() . 'magepal-data-original'
);
}

$subject->setCustomAttributes($customAttributes);

$result = $proceed();

$find = [
'img class="',
'magepal-data-original="placeholder"',
'magepal-data-original'
];

$replace = [
'img class="lazy swatch-option-loading ',
sprintf(' data-original="%s"', $orgImageUrl),
sprintf(' data-original="%s"', $orgImageUrl)
];

return str_replace($find, $replace, $result);
Expand Down

0 comments on commit bde04b0

Please sign in to comment.