/** * Purge product * * @param Mage_Catalog_Model_Product $product * @param bool $purgeParentProducts * @param bool $purgeCategories * @return Phoenix_VarnishCache_Model_Control_Catalog_Product */ public function purge(Mage_Catalog_Model_Product $product, $purgeParentProducts = false, $purgeCategories = false) { if ($this->_canPurge()) { $idsToPurge = array(); $categoryIdsToPurge = array(); $idsToPurge[] = $product->getId(); $this->_getSession()->addSuccess(Mage::helper('varnishcache')->__('Varnish cache for "%s" has been purged.', $product->getName())); if ($purgeParentProducts) { // purge parent products $productRelationCollection = $this->_getProductRelationCollection()->filterByChildId($product->getId()); foreach ($productRelationCollection as $productRelation) { $idsToPurge[] = $productRelation->getParentId(); } // purge categories of parent products if ($purgeCategories) { $categoryProductCollection = $this->_getCategoryProductRelationCollection()->filterAllByProductIds($productRelationCollection->getAllIds()); foreach ($categoryProductCollection as $categoryProduct) { $categoryIdsToPurge[] = $categoryProduct->getCategoryId(); } } } $this->_purgeByIds($idsToPurge); if ($purgeCategories) { foreach ($product->getCategoryCollection() as $category) { $categoryIdsToPurge[] = $category->getId(); } $this->_getSession()->addSuccess(Mage::helper('varnishcache')->__('Varnish cache for the product\'s categories has been purged.')); } $this->_purgeCategoriesByIds($categoryIdsToPurge); } return $this; }
public function testGetCategoryCollection() { // empty $collection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Mage_Catalog_Model_Resource_Category_Collection', $collection); // fixture $this->_model->setId(1); $fixtureCollection = $this->_model->getCategoryCollection(); $this->assertInstanceOf('Mage_Catalog_Model_Resource_Category_Collection', $fixtureCollection); $this->assertNotSame($fixtureCollection, $collection); $ids = array(); foreach ($fixtureCollection as $category) { $ids[] = $category->getId(); } $this->assertEquals(array(2, 3, 4), $ids); }
/** * Generates current product categories path * * @return array */ protected function _getProductBreadcrumbCategory() { if (!is_null($this->_currentProduct)) { $productCatsList = $this->_currentProduct->getCategoryCollection()->exportToArray(); if (count($productCatsList) > 0) { $curr_path = ''; foreach ($productCatsList as $category_id => $category_data) { if ($curr_path == '' || $curr_path . '/' . $category_id == $category_data['path']) { $curr_path = $category_data['path']; $actualCat = $category_data; } } $this->_currentCategory = Mage::getModel('catalog/category')->load($actualCat['entity_id']); } } return $this->_currentCategory; }
/** * get the longest url for a product * from http://magento.stackexchange.com/questions/52969/get-product-path-from-id-with-category-path-in-url * @param Mage_Catalog_Model_Product|null $product [description] * @return String full url of the product */ public static function getFullProductUrl(Mage_Catalog_Model_Product $product = null) { // Force display deepest child category as request path. $categories = $product->getCategoryCollection(); $deepCatId = 0; $path = ''; $productPath = false; foreach ($categories as $category) { // Look for the deepest path and save. if (substr_count($category->getData('path'), '/') > substr_count($path, '/')) { $path = $category->getData('path'); $deepCatId = $category->getId(); } } // Load category. $category = Mage::getModel('catalog/category')->load($deepCatId); // Remove .html from category url_path. $categoryPath = str_replace('.html', '', $category->getData('url_path')); // Get product url path if set. $productUrlPath = $product->getData('url_path'); // Get product request path if set. $productRequestPath = $product->getData('request_path'); // If URL path is not found, try using the URL key. if ($productUrlPath === null && $productRequestPath === null) { $productUrlPath = $product->getData('url_key'); } // Now grab only the product path including suffix (if any). if ($productUrlPath) { $path = explode('/', $productUrlPath); $productPath = array_pop($path); } elseif ($productRequestPath) { $path = explode('/', $productRequestPath); $productPath = array_pop($path); } // Now set product request path to be our full product url including deepest category url path. if ($productPath !== false) { if ($categoryPath) { // Only use the category path is one is found. $product->setData('request_path', $categoryPath . '/' . $productPath); } else { $product->setData('request_path', $productPath); } } return $product->getProductUrl(); }
private function _getSubSubCategory($storeId, Mage_Catalog_Model_Product $product) { $rootLevel = Mage::helper('remarketing')->getCategoryRootIdForStore($storeId); $rootPath = array(1); if ($rootLevel) { $rootPath[] = $rootLevel + 1; } $categoryLevel = Mage::helper('remarketing')->getCategoryLevel(); if ($this->_skipCategories == null) { $this->_skipCategories = array_unique(array_merge(Mage::helper('remarketing')->getInactiveCategories(), Mage::helper('remarketing')->getCategoriesToSkip())); } $categories = $product->getCategoryCollection(); $path = $this->_getFirstPathByPosition($categories, $categoryLevel + 1, $rootPath); $result = array(); if (isset($path[$categoryLevel - 1])) { $result['category'] = $this->_getCategoryField($path[$categoryLevel - 1], 'name'); } if (isset($path[$categoryLevel])) { $result['sub_category'] = $this->_getCategoryField($path[$categoryLevel], 'name'); } return $result; }
private function _getCategoryInformation($storeId, Mage_Catalog_Model_Product $product) { $rootLevel = Mage::helper('remarketing')->getCategoryRootIdForStore($storeId); $rootPath = array(1); if ($rootLevel) { $rootPath[] = $rootLevel; } $categoryLevel = Mage::helper('remarketing')->getCategoryLevel(); $categories = $product->getCategoryCollection(); $path = $this->_getFirstPathByPosition($categories, $categoryLevel + 1, $rootPath); $result = array(); if (isset($path[$categoryLevel - 1])) { $result['category'] = $this->_getCategoryName($path[$categoryLevel - 1]); } if (isset($path[$categoryLevel])) { $result['sub_category'] = $this->_getCategoryName($path[$categoryLevel]); } return $result; }
/** * Get an array of all product categories from given store_id * * @param Mage_Catalog_Model_Product $product * @param int $store_id * @return array */ public function getProductCategoriesByStore(Mage_Catalog_Model_Product $product, $store_id) { $rootCategoryId = Mage::app()->getStore($store_id)->getRootCategoryId(); $collection = $product->getCategoryCollection()->addFieldToFilter('is_active', 1)->setStoreId($store_id)->addAttributeToSelect('name')->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%")); return $collection; }
/** * Catalog product initialize after loading * * @param Mage_Catalog_Model_Product $model * @return void */ public function catalogProductLoadAfter($model) { if (!$model->getId()) { return; } if (!$this->_role->hasWebsiteAccess($model->getWebsiteIds())) { $this->_throwLoad(); } //var_dump($this->_role->hasExclusiveAccess($model->getWebsiteIds())); //echo "|"; if (!$this->_role->hasExclusiveAccess($model->getWebsiteIds())) { //echo "here?"; $model->unlockAttributes(); $attributes = $model->getAttributes(); foreach ($attributes as $attribute) { /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */ if ($attribute->isScopeGlobal() || $attribute->isScopeWebsite() && count($this->_role->getWebsiteIds()) == 0 || !in_array($model->getStore()->getId(), $this->_role->getStoreIds())) { $model->lockAttribute($attribute->getAttributeCode()); } } $model->setInventoryReadonly(true); $model->setRelatedReadonly(true); $model->setCrosssellReadonly(true); $model->setUpsellReadonly(true); $model->setWebsitesReadonly(true); $model->lockAttribute('website_ids'); $model->setOptionsReadonly(true); $model->setCompositeReadonly(true); if (!in_array($model->getStore()->getId(), $this->_role->getStoreIds())) { $model->setAttributesConfigurationReadonly(true); } $model->setDownloadableReadonly(true); $model->setGiftCardReadonly(true); $model->setIsDeleteable(false); $model->setIsDuplicable(false); $model->unlockAttribute('category_ids'); foreach ($model->getCategoryCollection() as $category) { $path = implode("/", array_reverse($category->getPathIds())); if (!$this->_role->hasExclusiveCategoryAccess($path)) { $model->setCategoriesReadonly(true); $model->lockAttribute('category_ids'); break; } } if (!$this->_role->hasStoreAccess($model->getStoreIds())) { $model->setIsReadonly(true); } } else { /* * We should check here amount of websites to which admin user assigned * and not to those product itself. So if admin user assigned * only to one website we will disable ability to unassign product * from this one website */ if (count($this->_role->getWebsiteIds()) == 1) { $model->setWebsitesReadonly(true); $model->lockAttribute('website_ids'); } } }
/** * Retrieve the category and subcategory for a product * * @param int $storeId Magento store ID * @param Mage_Catalog_Model_Product $product Current product * * @return array */ private function _getCategoryInformation($storeId, Mage_Catalog_Model_Product $product) { /* @var Listrak_Remarketing_Helper_Data $helper */ $helper = Mage::helper('remarketing'); $rootLevel = $helper->getCategoryRootIdForStore($storeId); $rootPath = array(1); if ($rootLevel) { $rootPath[] = $rootLevel; } $categoryLevel = $helper->getCategoryLevel(); if ($this->_skipCategories == null) { $this->_skipCategories = array_unique(array_merge($helper->getInactiveCategories(), $helper->getCategoriesToSkip())); } /* @var Mage_Catalog_Model_Resource_Category_Collection $categories */ $categories = $product->getCategoryCollection(); $path = $this->_getFirstPathByPosition($categories, $categoryLevel + 1, $rootPath); $result = array(); if (isset($path[$categoryLevel - 1])) { $result['category'] = $this->_getCategoryField($path[$categoryLevel - 1], 'name'); } if (isset($path[$categoryLevel])) { $result['sub_category'] = $this->_getCategoryField($path[$categoryLevel], 'name'); } return $result; }
/** * return a fragment containing the nodes for the product's category links * or null if there are none. * @param string $attrValue * @param string $attribute * @param Mage_Catalog_Model_Product $product * @param DOMDocument $doc * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function passCategoryLinks($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) { $frag = $doc->createDocumentFragment(); $categories = $product->getCategoryCollection(); $all = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name'); foreach ($categories as $category) { $pathArr = explode('/', $category->getPath()); array_walk($pathArr, function (&$val) use($all) { $part = $all->getItemById((int) $val); $val = $part ? $part->getName() : null; }); $catString = implode('-', array_filter($pathArr)); if ($catString) { $frag->appendChild($doc->createElement('CategoryLink'))->addAttributes(array('import_mode' => 'Replace'))->addChild('Name', $catString); } } return $frag->hasChildNodes() ? $frag : null; }
/** * Return array of categories for the product. * The items in the array are strings combined of the complete category * path to the products own category. * * Structure: * array ( * /Electronics/Computers * ) * * @param Mage_Catalog_Model_Product $product the product model. * * @return array */ protected function buildCategories(Mage_Catalog_Model_Product $product) { $data = array(); /** @var Nosto_Tagging_Helper_Data $helper */ $helper = Mage::helper('nosto_tagging'); $categoryCollection = $product->getCategoryCollection(); foreach ($categoryCollection as $category) { $categoryString = $helper->buildCategoryString($category); if (!empty($categoryString)) { $data[] = $categoryString; } } return $data; }
/** * Return array of categories for the product. * The items in the array are strings combined of the complete category * path to the products own category. * * Structure: * array ( * /Electronics/Computers * ) * * @param Mage_Catalog_Model_Product $product the product model. * * @return array */ public function getProductCategories(Mage_Catalog_Model_Product $product) { $data = array(); if ($product instanceof Mage_Catalog_Model_Product) { $categoryCollection = $product->getCategoryCollection(); foreach ($categoryCollection as $category) { $categoryString = Mage::helper('nosto_tagging')->buildCategoryString($category); if (!empty($categoryString)) { $data[] = $categoryString; } } } return $data; }
/** * Return all categories = product category + parent categories * @param Mage_Catalog_Model_Product $product * @return array */ public function getAllCategoryIdsFromProduct(Mage_Catalog_Model_Product $product) { $_ids = array(); $categoryCollection = $product->getCategoryCollection(); if (!empty($categoryCollection)) { /** @var Mage_Catalog_Model_Category $category */ foreach ($categoryCollection as $category) { foreach ($this->getAllCategoryIdsFromCategory($category) as $_id) { $_ids[$_id] = $_id; } } } return $_ids; }
/** * @param Mage_Catalog_Model_Product $product * @return string */ public function getSecondaryCategories(Mage_Catalog_Model_Product $product) { $categories = array(); $collection = $product->getCategoryCollection()->addAttributeToSelect('name', 'inner')->addAttributeToFilter('is_active', 1)->addAttributeToFilter('entity_id', array('nin' => Mage::app()->getStore()->getRootCategoryId())); foreach ($collection as $category) { if ($category->getName()) { $categories[] = $category->getName(); } } return implode(self::SEPARATOR, $categories); }