2012-09-12 114 views
5

我实际上可以通过安装脚本添加一个类别,但是由于某些原因,某些字段没有正确设置。这里是我的代码如何通过安装脚本添加类别到Magento?

$this->startSetup(); 
Mage::register('isSecureArea', 1); 

$category = Mage::getModel('catalog/category'); 
$category->setPath('1/2') // set parent to be root category 
    ->setName('Category Name') 
    ->setUrlKey('category-name') 
    ->setIsActive(0) 
    ->setIncludeInMenu(1) 
    ->setInfinitescroll(1) 
    ->setDisplayMode('PAGE') 
    ->setLandingPage($idToCmsBlock) 
    ->setPageLayout('anotherLayoutThanDefault') 
    ->setCustomUseParentSettings(0) 
    ->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>') 
->save(); 
$this->endSetup(); 

运行此脚本后,我有一个使用EAVs表中设置的所有值创建的类别。 但是,即使我重新编制扁平表,扁平表也会缺少displayMode,landingPage,pageLayout,customLayoutUpdate。

奇怪的是,如果我去管理员,我可以看到所有这些领域正确设置,但如果我在我的前台去大多数这些领域被忽略。我将不得不去管理员,取消这些价值,并重新设置他们为每个人正常工作。

也让我说我使用setEnabled(1),我的类别将在管理中“启用”,但不显示在前端。

PS:我有平面类别激活,如果我禁用它似乎工作正常,但如果我重新索引它仍然无法正常工作。

+0

到目前为止,我认为我能够发现它与平板电脑有关。如果我禁用他们,我的类别将正常工作。 – zzarbi

回答

9

我终于找到了,我不知道为什么,但这些字段没有正确显示出来,因为他们被插入默认存储(storeId = 1),因为我的脚本正在更新脚本中运行。您需要使用STOREID 0

有了这些信息,你会认为,解决办法是这样的:

$this->startSetup(); 
Mage::register('isSecureArea', 1); 

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

$category = Mage::getModel('catalog/category'); 
$category->setPath('1/2') // set parent to be root category 
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) 
    ->setName('Category Name') 
    ... 
    ->save(); 
$this->endSetup(); 

但这种代码也不起作用。事实上,在研究Mage :: app()(Mage_Core_Model_App Line 804)后,我注意到一个IF条件,如果您在设置脚本中,它总是会返回默认存储。

诀窍是假的,你在安装脚本不是,我的工作的解决方案是:

$this->startSetup(); 
Mage::register('isSecureArea', 1); 

// Force the store to be admin 
Mage::app()->setUpdateMode(false); 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

$category = Mage::getModel('catalog/category'); 
$category->setPath('1/2') // set parent to be root category 
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) 
    ->setName('Category Name') 
    ... 
    ->save(); 
$this->endSetup(); 
+4

这是因为您正在使用升级脚本来处理您应该放入数据升级脚本(创建类别为DML而非DDL)的内容。如果您使用的是数据安装程序,则不需要进行破解 – Enrique

+0

这很有趣!我会研究这一点。 – zzarbi

+0

哇,你解决了我的问题。非常感谢 :) – zuzuleinen

-1
+0

这是在类别 – zzarbi

+0

oops中添加自定义属性。我的错。这应该是一个。 http://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/ –

+0

我也尝试过,我也有同样的效果。其实我甚至复制/粘贴保存类别的管理控制器的逻辑,它仍然无法正常工作。 – zzarbi

1

试试这个

<?php 
require_once "../app/Mage.php"; 
umask(0); 
Mage::app('default'); 
$proxy = new SoapClient("http://127.0.0.1/magento/index.php/api/soap/?wsdl"); 
$sessionId = $proxy->login($magento_webservices_username, $magento_webservices_passwd); 

$data = array('name'=>'Nokia', 
      'description'=>'', 
      'meta_description'=>'', 
      'meta_keywords'=>'', 
      'default_sort_by'=>'price', 
      'available_sort_by'=>'price', 
      'is_active'=>1 
); 
$newCategoryId = $proxy->call($sessionId, 'category.create', array(3, $data, 1)); 
echo "Category ID: ".$newCategoryId; 

?> 

而且也看看Magento create category

+0

是的,我可以使用Magento SOAP API,但我不喜欢。 – zzarbi

9

我通过更新数据的类别时,安装脚本遇到了同样的问题。在accepted answer提供的解决方案并更新类的工作,但如下可以改进:

  • 在上述方案中,触发更新脚本被强制到管理环境中的用户。这可以通过保存当前商店标识并在脚本结尾切换回来进行修复。
  • 似乎并未将isSecureArea添加到注册表中,或者禁用更新模式有任何用处(至少对于更新类别的用例)。

我结束了以下数据安装脚本更新类别(在这个例子中,一类是按名称加载,名称更新后):

<?php 
    $this->startSetup(); 

    //Switch to admin store (workaround to successfully save a category) 
    $originalStoreId = Mage::app()->getStore()->getId(); 
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

    //update category 
    $category = Mage::getModel('catalog/category') 
     ->loadByAttribute('name', 'OLD_CATEGORY_NAME'); 
    if ($category) { 
     $category 
      ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) 
      ->setName('NEW_CATEGORY_NAME') 
      ->save(); 
    } 

    //Set store to original value 
    Mage::app()->setCurrentStore($originalStoreId); 

    $this->endSetup(); 
?> 
-1

我创建了多个通过安装程序脚本分类。

<?php 
$installer = $this; 
$installer->startSetup(); 

Mage::register('isSecureArea', 1); 

$category = Mage::getModel('catalog/category'); 
$category->setPath('1/2/4') // set parent to be root category 
->setName('CAT NAME') //Category Name 
->setIsActive(1) // Category Status 
->setIncludeInMenu(1) // Show in Menu 
->setIsAnchor(1) // used for Layered navigation 
->setDisplayMode('PAGE') // Product Only 
->setPageLayout('one_column') // Page layout 
->save(); 

$installer->endSetup(); 
相关问题