5

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

$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:我激活了平面类别,如果我禁用它似乎可以正常工作,但如果我重新索引它仍然无法正常工作。

4

5 回答 5

10

我终于找到了,我不知道为什么,但是这些字段没有正确显示,因为它们是为默认存储 (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();
于 2012-09-13T22:18:04.663 回答
9

I ran into the same issue when updating a category via a data install script. The solution provided in the accepted answer did work for updating the category, but can be improved upon as follows:

  • In the solution, the user that triggers the update script is forced to the admin environment. This can be remedied by saving the current store id and switching back at end of the script.
  • It doesn't seem that adding isSecureArea to the registry or disabling update mode had any use (at least for the use case of updating a category).

I ended up with the following data install script for updating a category (in this example, a category is loaded by name, after which the name is updated):

<?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();
?>
于 2013-08-07T13:44:20.177 回答
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 创建类别

于 2012-09-13T07:34:56.580 回答
-1

看看这个。希望它会帮助你。
http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/

于 2012-09-12T01:18:33.297 回答
-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();
于 2016-05-30T05:52:52.527 回答