【问题标题】:Cannot redeclare function() previously declared - Magento无法重新声明先前声明的函数() - Magento
【发布时间】:2015-08-29 21:39:22
【问题描述】:

我有一个脚本可以在其中发送包含价格的评论。一个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...

我期待您提供任何帮助。

【问题讨论】:

  • 它清楚地告诉你哪里出了问题。这里没有什么可以回答的
  • 重申@samaYo “......我需要的是,如果发送了额外的评论,这也会增加产品总价。”我知道这个问题,我需要帮助解决它。

标签: php magento


【解决方案1】:

您的getStringBetween 函数在change_price 函数中定义。每次调用 change_price 时,它都会尝试重新定义 getStringBetween 但它不能,因为它已经被定义了。将getStringBetween 移出change_price。我会假设runMyFunction 的定义类似。

【讨论】:

    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多