1

I making observer, that detects when someone reorders a discontinued product and should replace that product with a new product.

My question is: How do I retrieve a product object with a specific SKU? For example I want to retrieve the product with the SKU D1105 and add that to the cart using $observer->getEvent()->setProduct( $myDD1105Product );.

I need to know, how to retrieve a product with the SKU D1105 and add it to the cart. Any advice would be greatly appreciated.

<?php
class MHT_Reorder_Model_Observer
{

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

        $replacementSku = 'D1105';
        $blackListSKUs = ['D1100', 'D1101'];

        foreach ($blackListSKUs as $blacklistSku) {
            // if product SKU does not equal $blacklistSku: continue iterating
            if (strcasecmp($product->getSku(), $blacklistSku) != 0)
                continue;

            // How do I get the product from Mage/database that has the sku D1105?
            $event->setProduct()
            break;
        }
    }
}
0

To load any product using it sku, you need to used following code.

$sku = 'D1105';
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
0
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku','your_value'
);

That's it !

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.