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
}