I have a script working where a comment is sent which includes a price. An observer.php monitoring checkout_cart_product_add_after
, extracts the price and uses it to increase the original product price.
This all works great!
<?php
class YBizz_PriceChange_Model_Observer {
public function change_price(Varient_Event_Observer $obs) {
$quote = $obs->getEvent()->getQuote();
$custom = $obs->getQuoteItem();
$product_id=$custom->getProductId();
//Get $str
$items = null;
$files = array();
$hlp = Mage::helper('orderattachment');
$obAll = Mage::getSingleton('core/session')->getObjProducts();
if(is_object($obAll)) $items = @$obAll->getItems();
if(!empty($items))
{
foreach($items as $item)
{
$it = $item->getData();
if($product_id == intval($it['set_product_id'])) $files[] = $it;
}
}
foreach($files as $file){
$str = $file['set_comment'];
//Extract Price from $str
$from = "£";
$to = "]";
function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
$var = getStringBetween($str,$from,$to);
//Calc Custom Price
$_product=Mage::getModel('catalog/product')->load($product_id);
$newprice=$_product->getPrice()+$var;
// Set the custom price
$custom->setCustomPrice($newprice);
$custom->setOriginalCustomPrice($newprice);
// Enable super mode on the product.
$custom->getProduct()->setIsSuperMode(true);
}
}
}
However, what I need is if an additional comment is sent, this too will also add to the total product price.
Currently, everytime I attempt to send another comment, I get the fatal error message:
Cannot redeclare runMyFunction() (previously declared...
or
Cannot redeclare getStringBetween() (previously declared...
I look forward to any assistance you can offer.
Your getStringBetween
function is defined in the change_price
function. Every time change_price
is called, it tries to redefine getStringBetween
but it can't because it's already been defined. Move getStringBetween
outside of change_price
. I would assume runMyFunction
is defined similarly.