/**
  * Retrieve Product Url path (with category if exists)
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Catalog\Model\Category $category
  *
  * @return string
  */
 public function getUrlPath($product, $category = null)
 {
     $path = $product->getData('url_path');
     if ($path === null) {
         $path = $product->getUrlKey() === false ? $this->prepareProductDefaultUrlKey($product) : $this->prepareProductUrlKey($product);
     }
     return $category === null ? $path : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path;
 }
Exemple #2
0
 /**
  * @param Category $category
  * @return void
  */
 protected function updateUrlPathForChildren($category)
 {
     foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
         $childCategory->unsUrlPath();
         $childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
         $childCategory->getResource()->saveAttribute($childCategory, 'url_path');
     }
 }
 /**
  * Retrieve Product Url path (with category if exists)
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Catalog\Model\Category $category
  *
  * @return string
  */
 public function getUrlPath($product, $category = null)
 {
     $path = $product->getData('url_path');
     if ($path === null) {
         $path = $this->generateUrlKey($product);
     }
     return $category === null ? $path : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path;
 }
    /**
     * @dataProvider getUrlPathWithParentDataProvider
     * @param string $urlKey
     * @param bool $isCategoryNew
     * @param bool $level
     * @param int $parentCategoryParentId
     * @param string $parentUrlPath
     * @param string $result
     */
    public function testGetUrlPathWithParent(
        $urlKey,
        $isCategoryNew,
        $level,
        $parentCategoryParentId,
        $parentUrlPath,
        $result
    ) {
        $urlPath = null;
        $parentLevel = CategoryUrlPathGenerator::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING - 1;
        $this->category->expects($this->any())->method('getParentId')
            ->will($this->returnValue('parent_id'));
        $this->category->expects($this->any())->method('getLevel')
            ->will($this->returnValue($level));
        $this->category->expects($this->any())->method('getUrlPath')->will($this->returnValue($urlPath));
        $this->category->expects($this->any())->method('getUrlKey')->will($this->returnValue($urlKey));
        $this->category->expects($this->any())->method('isObjectNew')->will($this->returnValue($isCategoryNew));

        $methods = ['__wakeup', 'getUrlPath', 'getParentId', 'getLevel', 'dataHasChangedFor', 'load'];
        $parentCategory = $this->getMock('Magento\Catalog\Model\Category', $methods, [], '', false);
        $parentCategory->expects($this->any())->method('getParentId')
            ->will($this->returnValue($parentCategoryParentId));
        $parentCategory->expects($this->any())->method('getLevel')->will($this->returnValue($parentLevel));
        $parentCategory->expects($this->any())->method('getUrlPath')->will($this->returnValue($parentUrlPath));
        $parentCategory->expects($this->any())->method('dataHasChangedFor')
            ->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));

        $this->categoryRepository->expects($this->any())->method('get')->with('parent_id')
            ->will($this->returnValue($parentCategory));

        $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));
    }
Exemple #5
0
 /**
  * Category view action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
         return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
     }
     $category = $this->_initCategory();
     if ($category) {
         $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
         $settings = $this->_catalogDesign->getDesignSettings($category);
         // apply custom design
         if ($settings->getCustomDesign()) {
             $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
         }
         $this->_catalogSession->setLastViewedCategoryId($category->getId());
         $page = $this->resultPageFactory->create();
         // apply custom layout (page) template once the blocks are generated
         if ($settings->getPageLayout()) {
             $page->getConfig()->setPageLayout($settings->getPageLayout());
         }
         $hasChildren = $category->hasChildren();
         if ($category->getIsAnchor()) {
             $type = $hasChildren ? 'layered' : 'layered_without_children';
         } else {
             $type = $hasChildren ? 'default' : 'default_without_children';
         }
         if (!$hasChildren) {
             // Two levels removed from parent.  Need to add default page type.
             $parentType = strtok($type, '_');
             $page->addPageLayoutHandles(['type' => $parentType]);
         }
         $page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
         // apply custom layout update once layout is loaded
         $layoutUpdates = $settings->getLayoutUpdates();
         if ($layoutUpdates && is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $page->addUpdate($layoutUpdate);
                 $page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]);
             }
         }
         $page->getConfig()->addBodyClass('page-products')->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))->addBodyClass('category-' . $category->getUrlKey());
         return $page;
     } elseif (!$this->getResponse()->isRedirect()) {
         return $this->resultForwardFactory->create()->forward('noroute');
     }
 }
Exemple #6
0
 /**
  * Init layout for viewing product page
  *
  * @param \Magento\Framework\View\Result\Page $resultPage
  * @param \Magento\Catalog\Model\Product $product
  * @param null|\Magento\Framework\Object $params
  * @return \Magento\Catalog\Helper\Product\View
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function initProductLayout(ResultPage $resultPage, $product, $params = null)
 {
     $settings = $this->_catalogDesign->getDesignSettings($product);
     $pageConfig = $resultPage->getConfig();
     if ($settings->getCustomDesign()) {
         $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
     }
     // Apply custom page layout
     if ($settings->getPageLayout()) {
         $pageConfig->setPageLayout($settings->getPageLayout());
     }
     $urlSafeSku = rawurlencode($product->getSku());
     // Load default page handles and page configurations
     if ($params && $params->getBeforeHandles()) {
         foreach ($params->getBeforeHandles() as $handle) {
             $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()], $handle);
         }
     }
     $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()]);
     if ($params && $params->getAfterHandles()) {
         foreach ($params->getAfterHandles() as $handle) {
             $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()], $handle);
         }
     }
     // Apply custom layout update once layout is loaded
     $update = $resultPage->getLayout()->getUpdate();
     $layoutUpdates = $settings->getLayoutUpdates();
     if ($layoutUpdates) {
         if (is_array($layoutUpdates)) {
             foreach ($layoutUpdates as $layoutUpdate) {
                 $update->addUpdate($layoutUpdate);
             }
         }
     }
     $currentCategory = $this->_coreRegistry->registry('current_category');
     $controllerClass = $this->_request->getFullActionName();
     if ($controllerClass != 'catalog-product-view') {
         $pageConfig->addBodyClass('catalog-product-view');
     }
     $pageConfig->addBodyClass('product-' . $product->getUrlKey());
     if ($currentCategory instanceof \Magento\Catalog\Model\Category) {
         $pageConfig->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($currentCategory))->addBodyClass('category-' . $currentCategory->getUrlKey());
     }
     return $this;
 }
 /**
  * @param Category $category
  * @return void
  */
 protected function updateUrlPathForCategory(Category $category)
 {
     $category->unsUrlPath();
     $category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
     $category->getResource()->saveAttribute($category, 'url_path');
 }