簡體   English   中英

如何通過安裝腳本向Magento添加類別?

[英]How to add a category to Magento via Setup script?

我實際上可以通過安裝腳本添加類別,但由於某些原因,某些字段設置不正確。 這是我的代碼

$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:我已激活“平面類別”,如果禁用它似乎可以正常工作,但是如果我重新編制索引,它仍然無法正常工作。

我終於找到了,我不確定為什么,但是這些字段沒有正確顯示,因為它們是為默認商店(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第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();

通過數據安裝腳本更新類別時遇到了相同的問題。 接受的答案中提供的解決方案確實可以更新類別,但可以在以下方面進行改進:

  • 在該解決方案中,觸發更新腳本的用戶被強制進入管理環境。 可以通過保存當前商店ID並在腳本結尾處切換回來來解決此問題。
  • 似乎在注冊表中添加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();
?>

嘗試這個

<?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創建類別

我已經通過安裝腳本創建了多個類別。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM