Magento – Accessing data in catalog_product_save_before observer

catalogevent-observer

I created an catalog_product_save_before observer to update some attributes based on other attributes. Specifically, I use m2epro to update my ebay listings. Sometimes I want ebay to show less in stock than I actually have, so I created 2 new attributies, ebay_qty and ebay_max_qty.

In my observer, I have the following code:

$product = $observer->getEvent()->getProduct();
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

$Qty = $stock->getQty();
$EBayQty = $Qty;
$MaxQty = $product->getEbayMaxQty();

if (($MaxQty > 0) && ($EBayQty > $MaxQty))
    $EBayQty = $MaxQty;

$product->setData('ebay_qty', $EBayQty);

This is grabbing the qty currently in the database. How do I get the new Qty about to be saved?

Best Answer

Looks like no one has the answer to this. After searching for hours, I'm wondering if its even possible. I'm guessing it is as if I do a var_dump($product), Qty is listed, but can't figure out how to access it.

I did find a work around that might help someone else. I added a second function to the class and is called by the catalog_product_save_after observer

The config.xml file now looks like this:

    <events>
        <catalog_product_save_before>
            <observers>
                <me_createebaydescription>
                    <class>me_createebaydescription/observer</class>
                    <method>ebayDescriptionUpdate</method>
                    <type>singleton</type>
                </me_createebaydescription>
            </observers>
        </catalog_product_save_before>
        <catalog_product_save_after>
            <observers>
                <me_createebaydescription>
                    <class>me_createebaydescription/observer</class>
                    <method>ebayQtyUpdate</method>
                    <type>singleton</type>
                </me_createebaydescription>
            </observers>
        </catalog_product_save_after>
    </events>

I used the UpdateAttributes function and added the following function to the observer:

public function ebayQtyUpdate(Varien_Event_Observer $observer)
{
    $product = $observer->getEvent()->getProduct();

    $ID = Array($product->getId());
    $UpdateData = Array('ebay_qty'=>$this->GetEBayQty($product));

    Mage::getSingleton('catalog/product_action')
        ->updateAttributes($ID, $UpdateData, 0);
}
Related Topic