Created
May 1, 2018 09:10
-
-
Save martinherweg/1e45c09e20f3faa29306fe6523b45d85 to your computer and use it in GitHub Desktop.
Craft Commerce 2 Extend Snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Event::on(LineItems::class, LineItems::EVENT_POPULATE_LINE_ITEM, function (LineItemEvent $event) { | |
$purchasable = $event->lineItem->purchasable; | |
$lineItem = $event->lineItem; | |
$productImageUrl = ''; | |
$imageOptimize = []; | |
if (count($purchasable->product->productImage) > 0) { | |
$productImageUrl = $purchasable->product->productImage->one()->getUrl(); | |
if (Craft::$app->plugins->isPluginInstalled('image-optimize')) { | |
$imageOptimizeField = $purchasable->product->productImage->one()->imageOptimize; | |
$imageOptimize = $imageOptimizeField; | |
} | |
} | |
$lineItem->snapshot = array_merge( | |
$lineItem->snapshot, | |
[ | |
'productImageUrl' => $productImageUrl, | |
'productImageOptimize' => $imageOptimize, | |
] | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment