无法重新声明先前声明的function() - Magento

时间:2015-08-29 21:39:22

标签: php magento

我有一个脚本工作,其中发送评论,其中包括价格。 observer.php 监控checkout_cart_product_add_after,提取价格并使用它来提高原始产品价格。

这一切都很棒!

    <?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);

}

}

}

但是,我需要的是如果发送了额外的评论,这也会增加产品的总价格。

目前,每当我尝试发送另一条评论时,都会收到致命的错误消息:

Cannot redeclare runMyFunction() (previously declared...

Cannot redeclare getStringBetween() (previously declared...

我期待您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您的getStringBetween功能已在change_price功能中定义。每次调用change_price时,它都会尝试重新定义getStringBetween,但它不能,因为它已经被定义。将getStringBetween移到change_price之外。我认为runMyFunction的定义类似。