0

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?

bumped to the homepage by Community 17 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • Someone suggested I add a tag m2epro. I rejected the idea because this topic has nothing to do with m2epro, I only included that information to give some background info on why I'm attempting to do what I'm doing so I didn't get useless answers about different ways to do something I'm not trying to do. I'm actually doing a lot more than I show here, but I showed enough to get the answer I need and not confuse the question with unneeded information. Had the person attempted to help answer the question, I would have given their suggestion more credit. – lv2fly Apr 19 '16 at 5:12
0

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);
}
0

Found the answer incase anyone can't figure this out. First, I found catalog_product_prepare_save was a better place to do this than catalog_product_save_before. Don't know why, but someone suggested it, it worked, so not questioning it.

What I was missing was

$request = $observer->getEvent()->getRequest();
$request->getPost('what you're looking for')

You can then override the submitted data before its saved with

$product->setYourAttribute($NewValue);
or
$product->setData('YourAttribute', $NewValue);

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.