0

I can not understand please help. In his module I use catalog_controller_product_init event

config.xml

<frontend>
    <catalog_controller_product_init>
        <observers>
            <ZM_Digi_stock_price_update>
                <class>ZM_Digi_Model_Observer</class>
                <method>updateProduct</method>
                    <type>singleton</type>
            </ZM_Digi_stock_price_update>
        </observers>
    </catalog_controller_product_init>
</frontend>

observer.php

public function updateProduct($observer)
    {  
        $product = $observer->getProduct();

        if (Mage::helper('digi')->getAtributeSet($product) == 'Digi') {
            Mage::getSingleton('digi/digi')->updateStockStatus($product);   
            Mage::getSingleton('digi/digi')->updatePrice($product);
        }
    }

Digi.php (model)

public function updateStockStatus($product)   
    {
        $product_digid = Mage::helper('digi')->getProductDigiId($product);      
        $digi_stock = Mage::helper('digi')->getInstock($product_digid);
        $stock_item = Mage::getModel('cataloginventory/stock_item')->assignProduct($product);
        $store_stock = $stock_item->getIsInStock();
        if ($store_stock != $digi_stock) {
            if ($digi_stock == 0) {
                $stock_item->setQty(0);
                $stock_item->setIsInStock(0);
                $stock_item->save();
            } else {
                $stock_item->setQty(1);
                $stock_item->setIsInStock(1);
                $stock_item->save();
            }
        }
    }

    public function updatePrice($product)   
    {
        $store_price = $product->getFinalPrice();
        $digi_price = Mage::getModel('digi/digi')->digiPrice($product);
        if ($store_price != $digi_price) {
            if ($digi_price != ''){
                $product->setPrice($digi_price);
                $product->getResource()->saveAttribute($product, 'price');
            } else{
                $product->setPrice(0);
                $product->getResource()->saveAttribute($product, 'price');
            }
        }
    }

Model update availability and prices of products.

This code is a good work out in the admin area, but on the site update prices and availability in two stages. 1 page loading. Record the new values (see the oldest) 2 page loading. We see the new values.

I understand the matter in the boot sequence

I tried using catalog_controller_product_init_before event but this event does not contain any information on the product ...

I want to ask how it is possible to implement, and update, and display in one page load?

  • Please let us know on which page you want to achieve the above, product page, product listing ? – Giuseppe Feb 9 '16 at 13:49
  • to view the product page – ZMage Feb 9 '16 at 13:53
  • In which scope, you have registered your observer ? – Prateek Feb 9 '16 at 13:59
  • registered in <frontend> – ZMage Feb 9 '16 at 14:02
2

Having examined the event catalog_controller_product_init_before in the class Mage_Catalog_Helper Product, namely description

     * @param int $productId
     * @param Mage_Core_Controller_Front_Action $controller
     * @param Varien_Object $params
     *
     * @return false|Mage_Catalog_Model_Product
     */
    public function initProduct($productId, $controller,...

can be used $productId, which is not in the standard event

// Init and load product
        Mage::dispatchEvent('catalog_controller_product_init_before', array(
            'controller_action' => $controller,
            'params' => $params,
        ));

I tried to rewrite the class Mage_Catalog_Helper_Product

// Init and load product
        Mage::dispatchEvent('catalog_controller_product_init_before', array(
            'controller_action' => $controller,
            'params' => $params,
            'product_id' => $productId,
        ));

and config.xml

<frontend>
    <catalog_controller_product_init_before>
       ...
    </catalog_controller_product_init_before>
</frontend>

and a observer

public function updateProduct($observer)
    {  
        $productId = $observer->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);

With these changes, price and availability are updated immediately when the page first loads. While this solution is fine with me, but if someone will suggest how to solve this problem without having to rewrite the class will be glad) Thank.

0

I am not clear about your question.

But as per as,my thought, catalog_controller_product_init only fire whenever product detail page is render at frontend using Mage_Catalog_Helper_Product function initProduct() .

Also this event does not fire at adminhtml area as per as my thought.

@Question is till why call this event

  • I am sorry I do not have a model. I've edited the basic message. – ZMage Feb 9 '16 at 14:05
  • I used the model updates the product settings globally, just for her call through the site I use catalog_controller_product_init event. Only the update takes place in two steps as I wrote in the main report. – ZMage Feb 9 '16 at 14:23
0

If you have to run code only in the product page a "quick and dirty" solution is to put your code inside the "catalog/product/view.phtml".

<?php $_product = $this->getProduct();
//Add these lines 
if (Mage::helper('digi')->getAtributeSet($_product) == 'Digi') {
            Mage::getSingleton('digi/digi')->updateStockStatus($_product);   
            Mage::getSingleton('digi/digi')->updatePrice($_product);
        }
?>

Just below the Later we can find another solution as soon as we know exactly what you are trying to do.

From the code seems to me that you want to change price and stock information of a products getting data from a "digi" external source.

If this is your goal you have to do also other tasks in order to have the digi price added to the cart ...

  • In this case the basket is not used. The module I have redefined the basket through checkout_cart_product_add_after event. I just have to when viewing the goods, on the product page, displayed actual information about pricing and availability (this information is taken from other resources via XML queries). I tried using catalog_controller_product_init_before event if you manually set the product to the Observer, the update takes place in a single page load. – ZMage Feb 9 '16 at 14:42
  • In this case I'will try to semplify things, I edited my answer, give it a try and let us know – Giuseppe Feb 9 '16 at 14:50

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.