0

I'm importing Craft Commerce products from a third party feed using the FeedMe plugin and I would like to change the formatting/data of the price field.

The API is providing the price as cents and I need it to be a number in euro. Eg. 1535 -> 15.35

I've made a module using the EVENT_STEP_BEFORE_ELEMENT_SAVE event but can't figure out how to change the price. The same code works if I want to change the slug.

This is what I have in my module and doesn't work. (A test to set the price of all products to 10)

Event::on(
   Process::class, 
   Process::EVENT_STEP_BEFORE_ELEMENT_SAVE,
      function(FeedProcessEvent $event) {
         $element = $event->element;
        
         if ($element instanceof \craft\commerce\elements\Product) {
            $element->defaultVariant->price = 10;
            }            
      });

This test, to change al the slugs to Test, does work.

Event::on(
   Process::class, 
   Process::EVENT_STEP_BEFORE_ELEMENT_SAVE,
      function(FeedProcessEvent $event) {
         $element = $event->element;
        
         if ($element instanceof \craft\commerce\elements\Product) {
            $element->slug = "Test";
            }            
      });

Anyone has any idea how to alter the price of a commerce product during import?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.