Skip to content

Instantly share code, notes, and snippets.

@martinherweg
Created May 1, 2018 09:10
Show Gist options
  • Save martinherweg/1e45c09e20f3faa29306fe6523b45d85 to your computer and use it in GitHub Desktop.
Save martinherweg/1e45c09e20f3faa29306fe6523b45d85 to your computer and use it in GitHub Desktop.
Craft Commerce 2 Extend Snapshots
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