我正在创建一个Magento扩展,我在其中使用自定义Helper类来重用我的控制器函数中的一些方法。一切都很好,直到我在助手类中添加了一个新方法。
我称之为此方法,我收到此错误:
Fatal error: Call to undefined method Art_Sync_Helper_Data::updateMagProduct() in /home/vagrant/apps/default/app/code/local/Art/Sync/controllers/ProductUpdateController.php on line 259
这就是这一行:
$product_id = self::$helperData->updateMagProduct($product);
但我已经在我的控制器顶部定义了它:
private static $helperData;
在我的indexAction()方法中: (发生错误的方法相同。($ product_id = self :: ....))
self::$helperData = Mage::helper('sync/data');
我想,我帮助班的功能不正确,但似乎没问题......
public static function updateMagProduct($product)
{// code...}
实际上,我之前添加的所有方法都在工作......
我还试图添加一个返回一个简单String的新方法,但这也行不通。
所以,我的问题,你知道会出现什么问题吗? 您可以在Magento / Zend帮助程序类中使用的函数有限制吗?
==更新==
Controller + indexAction方法:
`class Art_Sync_ProductUpdateController扩展Mage_Core_Controller_Front_Action
{
private static $helper;
private static $helperData;
public function indexAction()
{
self::$helper = Mage::helper('farmadsync/util');
self::$helperData = Mage::helper('farmadsync/data');
self::$helper->log('Farmadsync: Started product & category UPDATE service.');
// get store languages
$default_nl = Mage::getStoreConfig('farmadsync/store/store_nl');
$store_fr = Mage::getStoreConfig('farmadsync/store/store_fr');
$store_en = Mage::getStoreConfig('farmadsync/store/store_en');
$store_de = Mage::getStoreConfig('farmadsync/store/store_de');
// check if valid URL
$errors = false;
if(!isset($_GET['guid']) || empty($_GET['guid']) || $_GET['guid'] != self::$helper->getGUID())
{
self::$helper->log('Farmadsync: Product syncronisation without valid GUID! Aborting... || GET params: ' .print_r($_GET,true));
self::$helper->outputMessagesToBrowser();
$errors = true;
}
if(!$errors)
{
// set maintenance flag
//$maintenance_flag = Mage::getBaseDir() . DS . 'maintenance.flag';
//touch($maintenance_flag);
// disable indexer
self::$helper->disableIndexer();
// get version info from last update by Farmad
$farmad_version_info = Mage::getModel('farmadsync/versioninfo')->load(1);
$cat_version = $farmad_version_info->getCategoryVersion();
$prodInfo_version = $farmad_version_info->getProductinfosVersion();
// get categories
$cat_version = $cat_version + 1;
$farmad_categories = self::$helperData->getFarmadCategories($cat_version);
// get productinfo
self::$helper->log('Farmadsync: Preparing product updates.');
$prodInfo_version = $prodInfo_version + 1;
//
$soapClient = self::$helper->initSoap();
$farmad_products = array();
$result = $soapClient->GetProductInfosDelta(array(
'guid'=>self::$helper->getGUID(),
'webshopId'=>self::$helper->getWebshopID(),
'yourCurrentVersion'=>$prodInfo_version,
));
if($result && $result->GetProductInfosDeltaResult)
{
$farmad_products = $result->GetProductInfosDeltaResult->ProductInfos->ProductInfo;
}
if($farmad_products==null)
{
// no product updates
self::$helper->log('Farmadsync: There are no product updates.');
}
else
{
// get updated products
self::$helper->log('Farmadsync: Get updated products.');
//
// only one result
// check if deleted
if($farmad_products->IsDeleted)
{
set_time_limit(120);
// delete product
self::$helper->log('Farmadsync: Product with cnk: '.$farmad_products->Cnk.' is deleted in Farmad. Deleting product in Magento...');
// get existing magento product from farmad cnk
$mag_product = Mage::getModel('catalog/product')->loadByAttribute('SKU',$farmad_products->Cnk);
Mage::register('isSecureArea',true);
$mag_product->delete();
Mage::unregister('isSecureArea');
self::$helper->log('Farmadsync: Product is successful deleted.');
}
else
{
// check if product is obsolete
if($farmad_products->Obsolete)
{
set_time_limit(120);
// product is obsolete
// delete product
self::$helper->log('Farmadsync: Product with cnk'.$farmad_products->Cnk.' is obsolete in Farmad. Deleting product in Magento...');
// get existing magento product from farmad cnk
$mag_product = Mage::getModel('catalog/product')->loadByAttribute('SKU',$farmad_products->Cnk);
Mage::register('isSecureArea',true);
$mag_product->delete();
Mage::unregister('isSecureArea');
self::$helper->log('Farmadsync: Product is successful deleted.');
}
else
{
set_time_limit(500);
// update product
self::$helper->log('Farmadsync: Ready to update product with cnk: '. $farmad_products->Cnk);
$product_id = Crmart_Farmadsync_Helper_Data::updateMagProduct($farmad_products);
//$product_id = self::$helperData->updateMagProduct($farmad_products);
// import different languages
// ...
}
}
// ...
}
//
//...
}
}
}`
辅助方法:
`class Art_Sync_Helper_Data扩展了Mage_Core_Helper_Abstract
{
private static $helper;
public static function updateMagProduct($farmad_product)
{
self::$helper = Mage::helper('farmadsync/util');
$isNewProduct = false;
//
// category didn't exist yet, so we create the category
$mag_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$farmad_product->Cnk);
if(!$mag_product)
{
$mag_product = Mage::getModel('catalog/product');
$isNewProduct = true;
}
//
self::$helper->log('Farmadsync: Start creating product with cnk: '.$farmad_product->Cnk.'.');
// product don't exist
if(!$isNewProduct)
{
// product exist, so update with new values
// get product categories
$categoryIDs = array();
foreach($farmad_product->CategoryIds as $farmad_productcat_id)
{
// product assigned to multiple categories
if(is_array($farmad_productcat_id))
{
foreach($farmad_productcat_id as $id)
{
$magentoCat = Mage::getModel('farmadsync/category')->load($id);
if(!$magentoCat->hasMagentoCatId())
{
self::$helper->log('Farmadsync: Error while saving product with cnk: '.$farmad_product->Cnk.'. The category isnt found in the shop.');
return -1;
}
array_push($categoryIDs, $magentoCat->getMagentoCatId());
self::$helper->log('Farmadsync: Added category to product.');
}
}
else // product has only one category
{
$magentoCat = Mage::getModel('farmadsync/category')->load($farmad_productcat_id);
if(!$magentoCat->hasMagentoCatId())
{
self::$helper->log('Farmadsync: Error while saving product with cnk: '.$farmad_product->Cnk.'. The category isn\'t found in the shop.');
return -1;
}
array_push($categoryIDs, $magentoCat->getMagentoCatId());
self::$helper->log('Farmadsync: Added category to product.');
}
}
//
$mag_product
->setStoreId('default')
->setCategoryIds($categoryIDs)
->setPrice((float)$farmad_product->SalesPriceWithVatWithoutDiscount)
->setName($farmad_product->DescriptionNl)
->setUrlKey('')
->setDescription($farmad_product->DescriptionNl)
->setShortDescription($farmad_product->DescriptionNl)
->setWebsiteIds(array(1))
->setFarmadsyncIsMedicine($farmad_product->Medicine)
->setFarmadsyncNeedsPrescription($farmad_product->NeedsPrescription)
->setFarmadsyncLeafletUrl($farmad_product->LeafletUrl_Nl)
->setFarmadsyncVersion($farmad_product->Version)
->setFarmadsyncObsolete($farmad_product->Obsolete);
$discountPercentage = (float)$farmad_product->DiscountPercentage;
if($discountPercentage!=0)
{
$price = (float)$farmad_product->SalesPriceWithVatWithoutDiscount;
$discountPrice = $price*((100.0 - $discountPercentage)/100.0);
$mag_product->setSpecialPrice($discountPrice);
self::$helper->log('Farmadsync: Added discount percentage to product.');
}
$taxclassId = null;
switch((int)$farmad_product->VatPercentage)
{
case 6:
$taxclassId = Mage::getStoreConfig('farmadsync/tax/taxclass_6_percent');
break;
case 21:
$taxclassId = Mage::getStoreConfig('farmadsync/tax/taxclass_21_percent');
break;
default:
self::$helper->log('Farmadsync: Could not dectect tax class - Product '.$farmad_product->Cnk.' not imported');
return -1;
break;
}
$mag_product->setTaxClassId($taxclassId);
self::$helper->log('Farmadsync: Added tax class to product.');
// save:
try
{
$mag_product->save();
self::$helper->log('FarmadSync: Finished updating product with cnk: ' . $farmad_product->Cnk);
}
catch(Exception $ex)
{
self::$helper->log('FarmadSync: Error while updating product with cnk: ' . $farmad_product->Cnk . '; ' . $ex->getMessage());
return -1;
}
}
//
return $mag_product->getId();
}
}`
===更新2 ===
我想我找到了。现在,我需要解决它
如果我Cmd +点击方法helperData
,我得到以下屏幕:
Choose Declaration:
- helperData Art_Sync_ProductUpdateController .../includes/src/Art/Sync/Controllers
- helperData Art_Sync_ProductUpdateController .../app/code/local/Art/Sync/controllers
但是,我怎么能强迫一个宣言呢?
(如果我点击其中一个声明,它会在同一个班级中打开。)
===更新3:已解决===
好。我的代码中的一切都很好
我必须在Magento中停用Compilation
...现在,它正在运行,没有错误。 : - )
答案 0 :(得分:0)
您是否在config.xml中定义了帮助程序?像下面的东西;
...
<global>
<helpers>
<sync>
<class>Art_Sync_Helper</class>
</sync>
</helpers>
</global>
...
尝试使用Mage::helper('sync')->updateMagProduct($product);
答案 1 :(得分:0)
确定。我的代码中的一切都很好。
我必须停用Compilationin Magento ......现在,它正在运行,没有错误。 : - )