0

I got some great advice on how to create a module to specify the price of a product when it is added to cart here: Dynamically calculated prices save before add to cart

I have a custom script within configurable.phtml that determines the price of an item depending on the configurable attributes (and other details). I need to pass this price string into the module's method within the Observer.php. How would I do this?

The /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

The Observer class at /app/code/local/{namespace}/{yourmodule}/Model/Observer.php (see comment next to $price declaration):

 class <namespace>_<modulename>_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = "your custom price logic"; //I need the value generated in the javascript here
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }



}

The javascript function that declares price (I need this in module observer):

function selectAndAddToCart(value)
{
    var product_id= <?=$product_id ?>;
    var colorSelected = $j("#attribute92 option:selected").val();

    $j('#attribute136 option[value="' + value + '"]').prop('selected',true);

    $j('#attribute136').removeClass('validation-failed').addClass('validation-passed');

    var price = newPriceArray[value][colorSelected]; //the value I need to pass to module
    console.log('The newPriceArray in selectAndAddToCart ' + price); //this is logging correctly


}
  • I was thinking Mage::register('dynamic_tier_price', price); in javascript then call that variable, but doesn't seem to make the most sense, especially since this variable could change several times while user is selecting/deselecting configurable attributes – CaitlinHavener Jun 4 '13 at 3:05
  • I think you have an issue of scope, particularly local vs. server. Without some sort of AJAX handling there's no connection between your JS and Magento observers. – benmarks Jun 4 '13 at 3:20
  • You can save your javascript value in hidden input type and get hidden parameter in your observer function. – Mufaddal Jun 4 '13 at 4:14
  • 2
    You need to rebuild your logic to work from the buy options submitted in the form POST fields when the product is added to the cart. Never, ever, should you simply accept a price from the client-side and assume it's what you should sell the product for. – davidalger Jun 4 '13 at 4:35
  • I have to pull certain variables for the price logic from a table on the database. I could perhaps have the price come from the php script that the jquery ajax call collects. I'm still not understanding how I would pass the price to the module: via php or javascript. Either would help. – CaitlinHavener Jun 4 '13 at 13:55

Browse other questions tagged or ask your own question.