/** * Get product url * * @param Mage_Catalog_Model_Product $product * @param bool $useSid * @return string */ public function getProductUrl($product, $useSid = true) { $cacheUrlKey = 'url_' . ($product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : 'NONE') . '_' . intval($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(); $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); } if (VPROF) { Varien_Profiler::start('REWRITE: ' . __METHOD__); } $rewrite = $this->getUrlRewrite(); if ($product->getStoreId()) { $rewrite->setStoreId($product->getStoreId()); } else { $rewrite->setStoreId(AO::app()->getStore()->getId()); } $idPath = 'product/' . $product->getId(); if ($product->getCategoryId() && !$product->getDoNotUseCategoryId() && AO::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())); if (VPROF) { Varien_Profiler::stop('REWRITE: ' . __METHOD__); } $this->getUrlInstance()->setUseSession($originalSid); return $this->getData($cacheUrlKey); } if (VPROF) { Varien_Profiler::stop('REWRITE: ' . __METHOD__); } if (VPROF) { 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); if (VPROF) { Varien_Profiler::stop('REGULAR: ' . __METHOD__); } } return $url; }
public function testGetCategoryId() { $this->assertFalse($this->_model->getCategoryId()); $category = new Varien_Object(array('id' => 5)); Mage::register('current_category', $category); try { $this->assertEquals(5, $this->_model->getCategoryId()); Mage::unregister('current_category'); } catch (Exception $e) { Mage::unregister('current_category'); throw $e; } }
/** * Retrieve url for new review creation * * @param Mage_Catalog_Model_Product $product * @return string */ public function getNewUrl($product) { $params = array('id' => $product->getId()); if ($product->getCategoryId()) { $params['category'] = $product->getCategoryId(); } return $this->_getUrl('review/product/list', $params); }
/** * 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; }
/** * @param Mage_Catalog_Model_Product $product * @return boolean */ protected function _changeUrlKey(Mage_Catalog_Model_Product &$product) { if ($product->getCategoryId() || !$this->_getHelper()->isUrlRewriteEnable()) { return false; } //if category already exist or rewrite categories is disabled $categoryIds = $product->getCategoryIds(); $categoryId = null; $product->setData("request_path", null); /** Process categories if they exists */ if (is_array($categoryIds) && count($categoryIds)) { $categoryId = $categoryIds[count($categoryIds) - 1]; //get the last element } return $this->_getUrlModel()->getProductUrl($product, null, $categoryId); }
/** * Create Product array from Mage_Catalog_Model_Product * * @param Mage_Catalog_Model_Product $product * @return array */ public function getProductData(Mage_Catalog_Model_Product $product) { try { $data = array('url' => $product->getProductUrl(), 'title' => htmlspecialchars($product->getName()), 'spider' => 1, 'price' => $product->getPrice(), 'description' => urlencode($product->getDescription()), 'tags' => htmlspecialchars($product->getMetaKeyword()), 'images' => array(), 'vars' => array('sku' => $product->getSku(), 'storeId' => '', 'typeId' => $product->getTypeId(), 'status' => $product->getStatus(), 'categoryId' => $product->getCategoryId(), 'categoryIds' => $product->getCategoryIds(), 'websiteIds' => $product->getWebsiteIds(), 'storeIds' => $product->getStoreIds(), 'groupPrice' => $product->getGroupPrice(), 'formatedPrice' => $product->getFormatedPrice(), 'calculatedFinalPrice' => $product->getCalculatedFinalPrice(), 'minimalPrice' => $product->getMinimalPrice(), 'specialPrice' => $product->getSpecialPrice(), 'specialFromDate' => $product->getSpecialFromDate(), 'specialToDate' => $product->getSpecialToDate(), 'relatedProductIds' => $product->getRelatedProductIds(), 'upSellProductIds' => $product->getUpSellProductIds(), 'getCrossSellProductIds' => $product->getCrossSellProductIds(), 'isSuperGroup' => $product->isSuperGroup(), 'isGrouped' => $product->isGrouped(), 'isConfigurable' => $product->isConfigurable(), 'isSuper' => $product->isSuper(), 'isSalable' => $product->isSalable(), 'isAvailable' => $product->isAvailable(), 'isVirtual' => $product->isVirtual(), 'isRecurring' => $product->isRecurring(), 'isInStock' => $product->isInStock(), 'weight' => $product->getSku())); // Add product images if (self::validateProductImage($product->getImage())) { $data['images']['full'] = array("url" => $product->getImageUrl()); } if (self::validateProductImage($product->getSmallImage())) { $data['images']['smallImage'] = array("url" => $product->getSmallImageUrl($width = 88, $height = 77)); } if (self::validateProductImage($product->getThumbnail())) { $data['images']['thumb'] = array("url" => $product->getThumbnailUrl($width = 75, $height = 75)); } return $data; return $data; } catch (Exception $e) { Mage::logException($e); } }
/** * 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); }
/** * Check product category * * @param Mage_Catalog_Model_Product $product * @param array $params * * @return int|null */ protected function _getCategoryIdForUrl($product, $params) { if (isset($params['_ignore_category'])) { return null; } else { return $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } }