/** * @magentoDataFixture Mage/Core/_files/store.php * @magentoDbIsolation enabled * @dataProvider saveActionDataProvider * @param array $inputData * @param array $defaultAttributes * @param array $attributesSaved */ public function testSaveAction($inputData, $defaultAttributes, $attributesSaved = array()) { $store = new Mage_Core_Model_Store(); $store->load('fixturestore', 'code'); $storeId = $store->getId(); $this->getRequest()->setPost($inputData); $this->getRequest()->setParam('store', $storeId); $this->getRequest()->setParam('id', 2); $this->dispatch('backend/admin/catalog_category/save'); $messages = Mage::getSingleton('Mage_Backend_Model_Session')->getMessages(false)->getItemsByType(Mage_Core_Model_Message::SUCCESS); $this->assertNotEmpty($messages, "Could not save category"); $this->assertEquals('The category has been saved.', current($messages)->getCode()); $category = new Mage_Catalog_Model_Category(); $category->setStoreId($storeId); $category->load(2); $errors = array(); foreach ($attributesSaved as $attribute => $value) { $actualValue = $category->getData($attribute); if ($value !== $actualValue) { $errors[] = "value for '{$attribute}' attribute must be '{$value}', but '{$actualValue}' is found instead"; } } foreach ($defaultAttributes as $attribute => $exists) { if ($exists !== $category->getExistsStoreValueFlag($attribute)) { if ($exists) { $errors[] = "custom value for '{$attribute}' attribute is not found"; } else { $errors[] = "custom value for '{$attribute}' attribute is found, but default one must be used"; } } } $this->assertEmpty($errors, "\n" . join("\n", $errors)); }
/** * Search category by the name from root category or specified one. * Create category when it doesn't exist if $createIfNotExists * parameter is set. * Search category by store-specific name if $store parameter is set. * * @param string $name * @param bool $createIfNotExists * @param Mage_Catalog_Model_Category $parent * @param null|Mage_Core_Model_Store $store * * @return Mage_Catalog_Model_Category|null */ public function getCategory($name, $createIfNotExists = false, $parent = null, $store = null) { $store = $store instanceof Mage_Core_Model_Store ? $store : Mage::app()->getStore(); $collection = $parent && ($parentId = $parent->getId()) ? $parent->setStoreId($store->getId())->getCollection()->addFieldToFilter('parent_id', $parentId) : Mage::getModel('catalog/category')->setStoreId($store->getId())->load($store->getRootCategoryId())->getCollection(); $collection->addAttributeToFilter('name', $name); if ($collection->count()) { return $collection->getFirstItem(); } if (!$createIfNotExists) { return; } if ($parent && $parent->getId()) { $rootCategory = $parent; } else { $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('parent_id', 1); if (count($collection) != 1) { return null; } $rootCategory = $collection->getFirstItem(); if (!$rootCategory->getId()) { return null; } } $model = Mage::getModel('catalog/category'); $model->setStoreId($rootCategory->getStoreId())->setData(array('name' => $name, 'is_active' => 1, 'include_in_menu' => 1))->setPath($rootCategory->getPath())->setAttributeSetId($model->getDefaultAttributeSetId()); try { $model->save(); } catch (Exception $e) { return null; } return $model; }
public function getRootCategory() { if (!$this->_rootCategory) { $this->_rootCategory = Mage::getModel('catalog/category'); $this->_rootCategory->setStoreId(Mage::app()->getStore()->getId())->load(Mage::app()->getStore()->getRootCategoryId()); } return $this->_rootCategory; }
/** * Write category url_key for category into default store * * @param Mage_Catalog_Model_Category $category * @param Mage_Core_Model_Store $store * @return Mage_Catalog_Model_Category */ protected function _setUrlKeyForDefaultStore(Mage_Catalog_Model_Category $category, Mage_Core_Model_Store $store) { //we should save url key for default store $category->setStoreId(0); $this->_urlModel->getResource()->saveCategoryAttribute($category, 'url_key'); //return current store to category $category->setStoreId($store->getId()); return $category; }
public function testSetGetStoreId() { $this->assertEquals(Mage::app()->getStore()->getId(), $this->_model->getStoreId()); $this->_model->setStoreId(1000); $this->assertEquals(1000, $this->_model->getStoreId()); }
/** * Update category url key * * @param Mage_Catalog_Model_Category $category * @param string $newUrlKey * @param int $storeId */ function processCategory($category, $newUrlKey, $storeId) { $store = Mage::app()->getStore(); Mage::app()->setCurrentStore(Mage::app()->getStore(0)); if (!$this->isEntityProcessed(self::ENTITY_TYPE_CATEGORY, $category->getId() . '-' . $storeId) && !preg_match('~-[a-f0-9]{32}$~i', $newUrlKey)) { $category->setStoreId($storeId); $category->setUrlKey($newUrlKey . '-' . md5($category->getStoreId() . $category->getId())); $category->save(); $this->_reindexCategory($category->getId()); $category->unsetData('request_path'); $category->unsetData('url'); $this->markEntityProcessed(self::ENTITY_TYPE_CATEGORY, $category->getId() . '-' . $category->getStoreId()); } Mage::app()->setCurrentStore($store); }