1

I have created a custom attribute called price_incvat and, following the basic instructions here mashed up with some answers found elsewhere on this site, I have tried to create a module that uses an observer to populate the attribute field in a product with the price of the product plus VAT (20%). I don't really know what I'm doing so of course it hasn't worked. Here's my config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
         <cvnstore_IncVatAttribute>
                <version>0.0.1</version>
         </cvnstore_IncVatAttribute>
    </modules>
    <global>
        <models>
            <cvnstore_incvatattribute>
                   <class>cvnstore_IncVatAttribute_Model</class>
            </cvnstore_incvatattribute>
        </models>
        <events>
            <catalog_product_save_before>
                  <observers>
                      <cvnstore_incvatattribute>
                        <class>cvnstore_incvatattribute/observer</class>
                        <method>incVatUpdate</method>
                        <type>singleton</type>
                      </cvnstore_incvatattribute>
                  </observers>
              </catalog_product_save_before>
           </events>
     </global>
</config>

And my Observer.php

class cvnstore_IncVatAttribute_Model_Observer
    {
        public function incVatUpdate(Varien_Event_Observer $observer)
            {
                $product = $observer->getEvent()->getProduct();
                $price = $product->getPrice();
                $product->setPriceIncvat($price*1.2);
            }
    }

I will be very grateful to anyone who takes the time to set me straight.

  • 1
    At what point are you are trying to update price_incvat after product admin save or on add to cart? – Renon Stewart Jun 18 '15 at 16:11
  • After admin save. We get a data feed from a wholesaler and I want this field to be populated when the product is loaded as part of the import. – Alistair67 Jun 19 '15 at 8:13
  • Well, as you probably could have told me, I seem to be going about this the wrong way. Importing new product info (one of the imports only updates prices and stock levels) doesn't trigger the save_before event, or the save_after or any other of the events I have tried, so nothing is inserted to the price_incvat field. I feel another post coming on! – Alistair67 Jun 19 '15 at 10:32
2

This might help -

public function incVatUpdate(Varien_Event_Observer $observer)
{
    $product = $observer->getEvent()->getProduct();
    $price = $product->getPrice();
    $priceWithVat = $price*1.2;
    $product->setData('price_incvat',$priceWithVat);
}
|improve this answer|||||
  • If you try to re-save within *_save_before you should get a infinite loop – Renon Stewart Jun 18 '15 at 16:14
  • Ah, good point! Next time I will slow down and read... – AreDubya Jun 18 '15 at 19:52
  • Hmm, doesn't seem to work. I am going into the product record, making a small change, then saving the record and the field is still empty. Would I be better using the save_after event? – Alistair67 Jun 19 '15 at 8:28
  • Got it. The problem was that I had defined the namespace with a fully lowercase name - cvnstore. When I changed it to Cvnstore and updated all the references to that in the relevant files, the code you have given worked. Thanks for your help. Now to see if reloading the product datafeed populates my price_incvat field. Fingers crossed. – Alistair67 Jun 19 '15 at 8:44
  • Glad it helped! I think you should be ok with the datafeed based on the event you are grabbing, if not post a comment here. – AreDubya Jun 19 '15 at 15:01

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.