/** * Check unique url_key value in catalog_product_entity_url_key table. * * @param Mage_Catalog_Model_Product $object * @return Mage_Catalog_Model_Product_Attribute_Backend_Urlkey * @throws Mage_Core_Exception */ protected function _validateUrlKey($object) { $connection = $object->getResource()->getReadConnection(); $select = $connection->select()->from($this->getAttribute()->getBackendTable(), array('count' => new Zend_Db_Expr('COUNT(\'value_id\')')))->where($connection->quoteInto('entity_id <> ?', $object->getId()))->where($connection->quoteInto('value = ?', $object->getUrlKey())); $result = $connection->fetchOne($select); if ((int) $result) { throw new Mage_Core_Exception(Mage::helper('catalog')->__("Product with the '%s' url_key attribute already exists.", $object->getUrlKey())); } return $this; }
/** * Initialize product view layout * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_ProductController */ protected function _initProductLayout($product) { $update = $this->getLayout()->getUpdate(); $update->addHandle('default'); $this->addActionLayoutHandles(); $update->addHandle('PRODUCT_TYPE_' . $product->getTypeId()); $update->addHandle('PRODUCT_' . $product->getId()); if ($product->getPageLayout()) { $this->getLayout()->helper('page/layout')->applyHandle($product->getPageLayout()); } $this->loadLayoutUpdates(); $update->addUpdate($product->getCustomLayoutUpdate()); $update->merge(strtolower($this->getFullActionName()) . '_FINAL'); $this->generateLayoutXml()->generateLayoutBlocks(); if ($product->getPageLayout()) { $this->getLayout()->helper('page/layout')->applyTemplate($product->getPageLayout()); } $currentCategory = Mage::registry('current_category'); if ($root = $this->getLayout()->getBlock('root')) { $root->addBodyClass('product-' . $product->getUrlKey()); if ($currentCategory instanceof Mage_Catalog_Model_Category) { $root->addBodyClass('categorypath-' . $currentCategory->getUrlPath())->addBodyClass('category-' . $currentCategory->getUrlKey()); } } return $this; }
/** * Initialize product view layout * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_ProductController */ protected function _initProductLayout($product) { $update = $this->getLayout()->getUpdate(); $update->addHandle('default'); $this->addActionLayoutHandles(); $update->addHandle('PRODUCT_TYPE_' . $product->getTypeId()); $update->addHandle('PRODUCT_' . $product->getId()); $this->loadLayoutUpdates(); $this->generateLayoutXml()->generateLayoutBlocks(); $currentCategory = Mage::registry('current_category'); if ($root = $this->getLayout()->getBlock('root')) { $root->addBodyClass('product-' . $product->getUrlKey()); if ($currentCategory instanceof Mage_Catalog_Model_Category) { $root->addBodyClass('categorypath-' . $currentCategory->getUrlPath())->addBodyClass('category-' . $currentCategory->getUrlKey()); } } return $this; }
/** * @param Mage_Catalog_Model_Product $product the product model * @return string URL */ public function getUrl($product) { // New url rewrites with enterprise 1.13 $magentoVersion = Mage::getVersion(); if (version_compare($magentoVersion, '1.13', '>=')) { $requestPath = $product->getUrlKey(); if ($requestPath) { return Mage::getUrl('', array('_direct' => $requestPath)); } } $rewrite = null; if ($categories = $product->getCategoryIds()) { $rewrite = Mage::getModel('catalog/url')->getResource()->getRewriteByIdPath('product/' . $product->getId() . '/' . reset($categories), 1); } if (!$rewrite) { $rewrite = Mage::getModel('catalog/url')->getResource()->getRewriteByIdPath('product/' . $product->getId(), 1); } return Mage::getUrl('', array('_direct' => $rewrite->getRequestPath())); }
/** * Initialize product view layout * * @param Mage_Catalog_Model_Product $product * @return Mage_Catalog_ProductController */ protected function _initProductLayout($product) { $update = $this->getLayout()->getUpdate(); $update->addHandle('default'); $this->addActionLayoutHandles(); $update->addHandle('PRODUCT_TYPE_' . $product->getTypeId()); $update->addHandle('PRODUCT_' . $product->getId()); if ($product->getPageLayout()) { $this->getLayout()->helper('page/layout')->applyHandle($product->getPageLayout()); } $this->loadLayoutUpdates(); $update->addUpdate($product->getCustomLayoutUpdate()); $this->generateLayoutXml()->generateLayoutBlocks(); if ($product->getPageLayout()) { $this->getLayout()->helper('page/layout')->applyTemplate($product->getPageLayout()); } //$currentCategory = Mage::registry('current_category'); if ($root = $this->getLayout()->getBlock('root')) { $root->addBodyClass('product-' . $product->getUrlKey()); } return $this; }
/** * Get product url * * @param Mage_Catalog_Model_Product $product * @return string */ public function getProductUrl($product) { Varien_Profiler::start('REWRITE: ' . __METHOD__); $rewrite = $this->getUrlRewrite(); if ($product->getStoreId()) { $rewrite->setStoreId($product->getStoreId()); } $idPath = 'product/' . $product->getId(); if ($product->getCategoryId() && Mage::getStoreConfig('catalog/seo/product_use_categories')) { $idPath .= '/' . $product->getCategoryId(); } $rewrite->loadByIdPath($idPath); if ($rewrite->getId()) { $url = $this->getUrlInstance()->getBaseUrl() . $rewrite->getRequestPath(); Varien_Profiler::stop('REWRITE: ' . __METHOD__); return $url; } Varien_Profiler::stop('REWRITE: ' . __METHOD__); Varien_Profiler::start('REGULAR: ' . __METHOD__); $url = $this->getUrlInstance()->getUrl('catalog/product/view', array('id' => $product->getId(), 's' => $product->getUrlKey(), 'category' => $product->getCategoryId())); Varien_Profiler::stop('REGULAR: ' . __METHOD__); return $url; }
/** * Retrieve Product URL using UrlDataObject * * @param Mage_Catalog_Model_Product $product * @param array $params * @return string */ public function getUrl(Mage_Catalog_Model_Product $product, $params = array()) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_store'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $idPath = sprintf('product/%d', $product->getEntityId()); if ($categoryId) { $idPath = sprintf('%s/%d', $idPath, $categoryId); } $rewrite = $this->getUrlRewrite(); $rewrite->setStoreId($storeId)->loadByIdPath($idPath); if ($rewrite->getId()) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_store'])) { $storeId = Mage::app()->getStore($routeParams['_store'])->getId(); } if ($storeId != Mage::app()->getStore()->getId()) { $routeParams['_store_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = array(); } return $this->getUrlInstance()->setStore($storeId)->getUrl($routePath, $routeParams); }
/** * Retrieve product URL based on requestPath param * * @param Mage_Catalog_Model_Product $product * @param string $requestPath * @param array $routeParams * * @return string */ protected function _getProductUrl($product, $requestPath, $routeParams) { if (!empty($requestPath)) { return $this->getUrlInstance()->getDirectUrl($requestPath, $routeParams); } $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); $categoryId = $this->_getCategoryIdForUrl($product, $routeParams); if ($categoryId) { $routeParams['category'] = $categoryId; } return $this->getUrlInstance()->getUrl('catalog/product/view', $routeParams); }
/** * Returns all the urls related to product * * Uses code from magneto-varnish. * * @see https://github.com/madalinoprea/magneto-varnish/blob/master/code/Varnish/Model/Observer.php#L133 * @param Mage_Catalog_Model_Product $product */ protected function _getUrlsForProduct($product) { $urls = array(); $store_id = $product->getStoreId(); $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); $routeParams['_store'] = !$store_id ? 1 : $store_id; $url = Mage::getUrl($routePath, $routeParams); $urls[] = $url; // Collect all rewrites $rewrites = Mage::getModel('core/url_rewrite')->getCollection(); if (!Mage::getConfig('catalog/seo/product_use_categories')) { $rewrites->getSelect()->where("id_path = 'product/{$product->getId()}'"); } else { // Also show full links with categories $rewrites->getSelect()->where("id_path = 'product/{$product->getId()}' OR id_path like 'product/{$product->getId()}/%'"); } foreach ($rewrites as $r) { unset($routeParams); $routePath = ''; $routeParams['_direct'] = $r->getRequestPath(); $routeParams['_store'] = $r->getStoreId(); $url = Mage::getUrl($routePath, $routeParams); $urls[] = $url; } return $urls; }
/** * Get product url * * @param Mage_Catalog_Model_Product $product * @param bool $useSid * @return string */ public function getProductUrl($product, $useSid = null) { if ($useSid === null) { $useSid = Mage::app()->getUseSessionInUrl(); } $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : 0; $cacheUrlKey = sprintf('url_%d_%d', $categoryId, $useSid); $url = $product->getData($cacheUrlKey); if (is_null($url)) { if ($product->getStoreId()) { $this->getUrlInstance()->setStore($product->getStoreId()); } // auto add SID to URL $originalSid = $this->getUrlInstance()->getUseSession(); if ($originalSid != $useSid) { $this->getUrlInstance()->setUseSession($useSid); } if ($product->hasData('request_path') && $product->getRequestPath() != '') { $this->setData($cacheUrlKey, $this->getUrlInstance()->getDirectUrl($product->getRequestPath())); $this->getUrlInstance()->setUseSession($originalSid); return $this->getData($cacheUrlKey); } Varien_Profiler::start('REWRITE: ' . __METHOD__); $rewrite = $this->getUrlRewrite(); if ($product->getStoreId()) { $rewrite->setStoreId($product->getStoreId()); } else { $rewrite->setStoreId(Mage::app()->getStore()->getId()); } $idPath = 'product/' . $product->getId(); if ($product->getCategoryId() && !$product->getDoNotUseCategoryId() && Mage::getStoreConfig('catalog/seo/product_use_categories')) { $idPath .= '/' . $product->getCategoryId(); } $rewrite->loadByIdPath($idPath); //echo $this->getUrlInstance()->getBaseUrl(); if ($rewrite->getId()) { $this->setData($cacheUrlKey, $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath())); Varien_Profiler::stop('REWRITE: ' . __METHOD__); $this->getUrlInstance()->setUseSession($originalSid); return $this->getData($cacheUrlKey); } Varien_Profiler::stop('REWRITE: ' . __METHOD__); Varien_Profiler::start('REGULAR: ' . __METHOD__); $url = $this->getUrlInstance()->getUrl('catalog/product/view', array('id' => $product->getId(), 's' => $product->getUrlKey(), 'category' => $product->getCategoryId())); $this->getUrlInstance()->setUseSession($originalSid); Varien_Profiler::stop('REGULAR: ' . __METHOD__); } return $url; }