2

Our Shop-Customers should upload an image per Item they add to the basket. I managed to upload the image and insert it to an asset folder.

How can i assign the image to the lineItem (maybe as an option)?

This didn't work, as options is not writable:

craft()->on('commerce_lineItems.onBeforeSaveLineItem', function($event){
    $imageId = craft()->ultraschall->uploadCustomerImage(); // add image to asset folder
    if(isset($imageId) && is_numeric($imageId)){
        $lineItem = $event->params['lineItem'];
        $lineItem->options['imageId'] = $imageId;
    } 
});
2
  • To be honest I'd have expected that to work, but you could perhaps instead save the asset id to the note field if you're not using that for something else. Hacky though. What error do you actually get? Apr 6 '17 at 0:30
  • I get following error: "Indirect modification of overloaded property Craft\Commerce_LineItemModel::$options has no effect", regardless if the option field exists in the form or not.
    – phaetons
    Apr 6 '17 at 8:01
1

I got a solution. You can't overwrite array elements at this point, but the whole array. I also found it better to use the event "commerce_cart.onBeforeAddToCart". My working code is now:

craft()->on('commerce_cart.onBeforeAddToCart', function($event) {
    $imageId = craft()->ultraschall->uploadCustomerImage(); // add image to asset folder
    if(isset($imageId) && is_numeric($imageId)){
        $lineItem = $event->params['lineItem'];
        $options = $lineItem->options;
        $options['image'] = $imageId;
        $lineItem->options = $options;
    }
});

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.