Magento - Customized Module not working

2019-09-12 04:32发布

问题:

I'm creating a module to customize my products price when adding it to the cart but it's not working. I'm using the checkout_cart_product_add_after method.

Steps that I followed:

1) Create a XML (TrediMarketplace_PriceUpdate) at /app/etc/modules and it's already appearing at the Magento interface (System > Configuration > Advanced > Advanced)

2) Create the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <TrediMarketplace_PriceUpdate>
            <version>0.0.1</version>
        </TrediMarketplace_PriceUpdate>
    </modules>
    <global>
        <models>
            <tredimarketplace_priceupdate>
                <class>TrediMarketplace_PriceUpdate_Model</class>
            </tredimarketplace_priceupdate>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <tredimarketplace_priceupdate>
                        <class>tredimarketplace_priceupdate/observer</class>
                        <method>priceUpdate</method>                        
                    </tredimarketplace_priceupdate>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </global>
</config>

The file is hosted at this path: /app/code/local/TrediMarketplace/PriceUpdate

3) Create the Observer.php:

<?php   
    class TrediMarketplace_PriceUpdate_Model_Observer{
        public function priceUpdate(Varien_Event_Observer $observer){
            // Get the quote item
                $item = $observer->getQuoteItem();
            // Ensure we have the parent item, if it has one
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            // Load the custom price
                $price = "300.00";
            // Set the custom price
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
            // Enable super mode on the product.
                $item->getProduct()->setIsSuperMode(true);
        }
    }
?>

The file is hosted at this path: /app/code/local/TrediMarketplace/PriceUpdate/Model

With this steps my expectations are that all products added to the cart appear with $300.00 (value that I fixed on my module) but this is not occurring.

Any suggestion?

回答1:

I have created a compressed module and its fully functional. I used your code snippets and assembled into a module. You might have any syntactical issue but its working. Please clone from here https://github.com/lapitspublic/TrediMarketplace_PriceUpdate.git



标签: php magento