3

How would I add more information (custom fields) to a variant's snapshot?

The custom field I'm adding will change over time, but I need it to stay consistent on invoices etc.

5

Yes using the onPopulateLineItem event. Something like this should work:

craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

    $purchasable = $event->params['purchasable'];
    $lineItem = $event->params['lineItem'];

    $lineItem->options = array_merge(
      $lineItem->options,
      ['customFieldLabel'=>$purchasable->customField]
    );

});
2

For Craft 3 Nathaniel from Pixel & Tonic helped us with the below:

In the following example, I am adding the titles of a category relationship field productColours to the snapshot but only for the products in the product type of clothing.

// Modify product snapshot
Event::on(Variant::class, Variant::EVENT_AFTER_CAPTURE_PRODUCT_SNAPSHOT, function(CustomizeProductSnapshotDataEvent $event) {
    $variant = $event->sender;
    if (!$variant instanceof Variant) {
        return;
    }

    $product = $variant->getProduct();
    if (!$product || $product->type->handle != 'clothing') {
        return;
    }

    $colours = $product->productColours->all();
    if (empty($colours)) {
        return;
    }

    $event->fieldData['productColours'] = ArrayHelper::getColumn($colours, 'title');
});

You will also need to add

use craft\commerce\events\CustomizeProductSnapshotDataEvent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.