/** * @param Mage_Catalog_Model_Product $product * * @return string */ public function getProductUrl(Mage_Catalog_Model_Product $product) { $productPath = $product->getRequestPath(); if ($this->isSeoUrlExists()) { $url = Mage::helper('amseourl/product_url_rewrite')->getProductPath($product); if ($url) { $productPath = $url; } } elseif ($product->getUrlPath()) { $productPath = $product->getUrlPath(); } return rtrim(Mage::getUrl('', array('_direct' => $productPath)), '/'); }
/** * 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); }
/** * Get product request path * * @param Mage_Catalog_Model_Product $product * @return string * @deprecated since 1.13.0.2 */ protected function _getProductRequestPath(Mage_Catalog_Model_Product $product) { /** * Initialize request_path value */ $product->getProductUrl(); /** @var $helper Enterprise_Catalog_Helper_Data */ $helper = $this->_factory->getHelper('enterprise_catalog'); return $helper->getProductRequestPath($product->getRequestPath(), $product->getStoreId()); }
/** * 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()) { $url = $product->getData('url'); if (!empty($url)) { return $url; } $requestPath = $product->getRequestPath(); if (empty($requestPath)) { $requestPath = $this->_getRequestPath($product, $this->_getCategoryIdForUrl($product, $params)); $product->setRequestPath($requestPath); } if (isset($params['_store'])) { $storeId = $this->_getStoreId($params['_store']); } else { $storeId = $product->getStoreId(); } if ($storeId != $this->_getStoreId()) { $params['_store_to_url'] = true; } // reset cached URL instance GET query params if (!isset($params['_query'])) { $params['_query'] = array(); } $this->getUrlInstance()->setStore($storeId); $productUrl = $this->_getProductUrl($product, $requestPath, $params); $product->setData('url', $productUrl); return $product->getData('url'); }
protected function _addItemXml($xml, Mage_Catalog_Model_Product $product, $selectedCategory) { $item = $xml->addChild("item"); $name = $this->_nameFormat; foreach ($this->_nameAttributes as $attr) { $attrData = $product->getData($attr); if (isset($this->_attributeOptions[$attr]) && isset($this->_attributeOptions[$attr][$attrData])) { $attrData = $this->_attributeOptions[$attr][$attrData]; } $name = str_replace($this->_attributeSymbol . $attr, $attrData, $name); } $item->name = $name; // Generating full URL for a product $categorySuffix = Mage::getStoreConfig("catalog/seo/category_url_suffix"); if ($categorySuffix && strstr($selectedCategory['url'], $categorySuffix)) { $item->link = str_replace($categorySuffix, "/" . $product->getRequestPath(), $selectedCategory['url']); } else { $item->link = $selectedCategory['url'] . "/" . $product->getRequestPath(); } /** @var $taxHelper Mage_Tax_Helper_Data */ $taxHelper = Mage::helper('tax'); $priceIncludingTax = $taxHelper->getPrice($product, $product->getFinalPrice(1), true); $item->price = $priceIncludingTax; //$item->price = number_format((float)$product->getFinalPrice(1), 2); $imageUrl = Mage::getUrl('feedsalidzini/info/image/', array('product' => $product->getId())); $item->image = $_SERVER["SCRIPT_FILENAME"] ? str_replace($_SERVER["SCRIPT_FILENAME"] . "/", "", $imageUrl) : $imageUrl; $categoryName = $selectedCategory['name']; $parentId = $selectedCategory['parent']; while ($parentId) { if ($this->_tmpCategories[$parentId]['level'] > 1) { $categoryName = $this->_tmpCategories[$parentId]['name'] . " >> " . $categoryName; $parentId = $this->_tmpCategories[$parentId]['parent']; } else { break; } } $item->category_full = $categoryName; $item->category_link = $selectedCategory['url']; $item->manufacturer = $this->_attributeOptions['manufacturer'][$product->getManufacturer()]; if ($product->getModel()) { $item->model = $product->getModel(); } if ($this->_includeStock && (!$this->_isComplexProduct($product) || $this->_emulateStock)) { $item->in_stock = $this->_getStock($product, $this->_stock[$product->getId()]); } if ($this->_includeDeliveryCost) { $item->delivery_cost_riga = $this->_getDeliveryCost($product); } return $xml; }
/** * 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; }
public function getObject(Mage_Catalog_Model_Product $product) { $type = $this->config->getMappedProductType($product->getTypeId()); $this->logger->start('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); $this->logger->log('Product type (' . $product->getTypeId() . ', mapped to: ' . $type . ')'); $defaultData = array(); $transport = new Varien_Object($defaultData); Mage::dispatchEvent('algolia_product_index_before', array('product' => $product, 'custom_data' => $transport)); $defaultData = $transport->getData(); $defaultData = is_array($defaultData) ? $defaultData : explode("|", $defaultData); $visibility = (int) $product->getVisibility(); $visibleInCatalog = Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds(); $visibleInSearch = Mage::getSingleton('catalog/product_visibility')->getVisibleInSearchIds(); $customData = array('objectID' => $product->getId(), 'name' => $product->getName(), 'url' => Mage::getBaseUrl() . $product->getRequestPath(), 'visibility_search' => (int) in_array($visibility, $visibleInSearch), 'visibility_catalog' => (int) in_array($visibility, $visibleInCatalog)); $additionalAttributes = $this->config->getProductAdditionalAttributes($product->getStoreId()); $groups = null; if ($this->isAttributeEnabled($additionalAttributes, 'description')) { $customData['description'] = $product->getDescription(); } $categories = array(); $categories_with_path = array(); $_categoryIds = $product->getCategoryIds(); if (is_array($_categoryIds) && count($_categoryIds) > 0) { $categoryCollection = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')->addAttributeToFilter('entity_id', $_categoryIds)->addFieldToFilter('level', array('gt' => 1))->addIsActiveFilter(); $rootCat = Mage::app()->getStore($product->getStoreId())->getRootCategoryId(); foreach ($categoryCollection as $category) { // Check and skip all categories that is not // in the path of the current store. $path = $category->getPath(); $path_parts = explode("/", $path); if (isset($path_parts[1]) && $path_parts[1] != $rootCat) { continue; } $categoryName = $category->getName(); if ($categoryName) { $categories[] = $categoryName; } $category->getUrlInstance()->setStore($product->getStoreId()); $path = array(); foreach ($category->getPathIds() as $treeCategoryId) { $name = $this->getCategoryName($treeCategoryId, $product->getStoreId()); if ($name) { $path[] = $name; } } $categories_with_path[] = $path; } } foreach ($categories_with_path as $result) { for ($i = count($result) - 1; $i > 0; $i--) { $categories_with_path[] = array_slice($result, 0, $i); } } $categories_with_path = array_intersect_key($categories_with_path, array_unique(array_map('serialize', $categories_with_path))); $categories_hierarchical = array(); $level_name = 'level'; foreach ($categories_with_path as $category) { for ($i = 0; $i < count($category); $i++) { if (isset($categories_hierarchical[$level_name . $i]) === false) { $categories_hierarchical[$level_name . $i] = array(); } $categories_hierarchical[$level_name . $i][] = implode(' /// ', array_slice($category, 0, $i + 1)); } } foreach ($categories_hierarchical as &$level) { $level = array_values(array_unique($level)); } foreach ($categories_with_path as &$category) { $category = implode(' /// ', $category); } $customData['categories'] = $categories_hierarchical; $customData['categories_without_path'] = $categories; if (false === isset($defaultData['thumbnail_url'])) { $thumb = Mage::helper('algoliasearch/image')->init($product, 'thumbnail')->resize(75, 75); try { $customData['thumbnail_url'] = $thumb->toString(); $customData['thumbnail_url'] = str_replace(array('https://', 'http://'), '//', $customData['thumbnail_url']); } catch (\Exception $e) { $this->logger->log($e->getMessage()); $this->logger->log($e->getTraceAsString()); $customData['thumbnail_url'] = str_replace(array('https://', 'http://'), '//', Mage::getDesign()->getSkinUrl($thumb->getPlaceholder())); } } if (false === isset($defaultData['image_url'])) { $image = Mage::helper('algoliasearch/image')->init($product, $this->config->getImageType())->resize($this->config->getImageWidth(), $this->config->getImageHeight()); try { $customData['image_url'] = $image->toString(); $customData['image_url'] = str_replace(array('https://', 'http://'), '//', $customData['image_url']); } catch (\Exception $e) { $this->logger->log($e->getMessage()); $this->logger->log($e->getTraceAsString()); $customData['image_url'] = str_replace(array('https://', 'http://'), '//', Mage::getDesign()->getSkinUrl($image->getPlaceholder())); } if ($this->isAttributeEnabled($additionalAttributes, 'media_gallery')) { $product->load('media_gallery'); $customData['media_gallery'] = array(); foreach ($product->getMediaGalleryImages() as $image) { $customData['media_gallery'][] = str_replace(array('https://', 'http://'), '//', $image->getUrl()); } } } $sub_products = null; $ids = null; if ($type == 'configurable' || $type == 'grouped' || $type == 'bundle') { if ($type == 'bundle') { $ids = array(); $selection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product); foreach ($selection as $option) { $ids[] = $option->product_id; } } if ($type == 'configurable' || $type == 'grouped') { $ids = $product->getTypeInstance(true)->getChildrenIds($product->getId()); $ids = call_user_func_array('array_merge', $ids); } if (count($ids)) { $sub_products = $this->getProductCollectionQuery($product->getStoreId(), $ids, false)->load(); } else { $sub_products = array(); } } if (false === isset($defaultData['in_stock'])) { $stockItem = $product->getStockItem(); $customData['in_stock'] = (int) $stockItem->getIsInStock(); } // skip default calculation if we have provided these attributes via the observer in $defaultData if (false === isset($defaultData['ordered_qty']) && $this->isAttributeEnabled($additionalAttributes, 'ordered_qty')) { $customData['ordered_qty'] = (int) $product->getOrderedQty(); } if (false === isset($defaultData['total_ordered']) && $this->isAttributeEnabled($additionalAttributes, 'total_ordered')) { $customData['total_ordered'] = (int) $product->getTotalOrdered(); } if (false === isset($defaultData['stock_qty']) && $this->isAttributeEnabled($additionalAttributes, 'stock_qty')) { $customData['stock_qty'] = (int) $product->getStockQty(); } if (Mage::helper('core')->isModuleEnabled('Mage_Review')) { if ($this->isAttributeEnabled($additionalAttributes, 'rating_summary')) { $customData['rating_summary'] = (int) $product->getRatingSummary(); } } foreach ($additionalAttributes as $attribute) { if (isset($customData[$attribute['attribute']])) { continue; } $value = $product->getData($attribute['attribute']); $attribute_ressource = $product->getResource()->getAttribute($attribute['attribute']); if ($attribute_ressource) { $attribute_ressource = $attribute_ressource->setStoreId($product->getStoreId()); if ($value === null) { /** Get values as array in children */ if ($type == 'configurable' || $type == 'grouped' || $type == 'bundle') { $values = array(); $all_sub_products_out_of_stock = true; foreach ($sub_products as $sub_product) { $isInStock = (int) $sub_product->getStockItem()->getIsInStock(); if ($isInStock == false) { continue; } $all_sub_products_out_of_stock = false; $value = $sub_product->getData($attribute['attribute']); if ($value) { $value_text = $sub_product->getAttributeText($attribute['attribute']); if ($value_text) { $values[] = $value_text; } else { $values[] = $attribute_ressource->getFrontend()->getValue($sub_product); } } } if (is_array($values) && count($values) > 0) { $customData[$attribute['attribute']] = array_values(array_unique($values)); } if ($customData['in_stock'] && $all_sub_products_out_of_stock) { // Set main product out of stock if all // sub-products is out of stock. $customData['in_stock'] = 0; } } } else { $value_text = $product->getAttributeText($attribute['attribute']); if ($value_text) { $value = $value_text; } else { $attribute_ressource = $attribute_ressource->setStoreId($product->getStoreId()); $value = $attribute_ressource->getFrontend()->getValue($product); } if ($value) { $customData[$attribute['attribute']] = $value; } } } } $msrpEnabled = method_exists(Mage::helper('catalog'), 'canApplyMsrp') ? (bool) Mage::helper('catalog')->canApplyMsrp($product) : false; if (false === $msrpEnabled) { $this->handlePrice($product, $sub_products, $customData); } else { unset($customData['price']); } $transport = new Varien_Object($customData); Mage::dispatchEvent('algolia_subproducts_index', array('custom_data' => $transport, 'sub_products' => $sub_products)); $customData = $transport->getData(); $customData = array_merge($customData, $defaultData); $customData['type_id'] = $type; $this->castProductObject($customData); $this->logger->stop('CREATE RECORD ' . $product->getId() . ' ' . $this->logger->getStoreName($product->storeId)); return $customData; }