Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom theme doesn't apply XML layout updates when applied to a product #7710

Closed
chickenland opened this issue Dec 7, 2016 · 19 comments
Closed
Labels
Area: Frontend bug report Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@chickenland
Copy link
Contributor

I was looking to open this issue, but thought I'd try and find one before I did. I think this issue is linked to #7503, but is manifesting is achieved in a different way

We have a theme that does not inherit, and it's XML updates appear to be ignored.

Preconditions

Magento 2.1.2, PHP 5.6.24
Environment: Windows XAMPP / Windows WSL
Developer Mode

Steps to reproduce

Set the theme for a specific storeview to Purenet/Theme (our default theme for the store) via Content > Design > Configuration > Edit.

Create and install a theme that has no inheritence. Add layout changes. For this example, I created a theme with a catalog_product_view.xml file that removes the main title for the product as Purenet/Client.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="page.main.title" remove="true"/>
</body>
</page>
  1. Deploy static content for the 'Purenet Client' theme:
  2. bin/magento setup:static:deploy --theme="Purenet/Client" en_GB
  3. Apply the 'Purenet Client' theme to a category via Products > Catalog > Actual Product > Schedule Design Update > Set Dates & New theme to be Purenet/Client. Save Product.

Expected result

When checking the the product page, you should not see the main product title.

Actual result

On the Product Page, the main product title is displayed.

Setting a similar XML layout file on a category in the same manner appears to work.

Investigation

So far, it looks like _fetchPackageLayoutUpdates within lib/internal/Magento/Framework/View/Model/Layout/Merge.php doesn't capture the XML that contains the handle updates. I found moving the XML into catalog_product_prices made it added as an update, however, it was still not removed.

@shavounet
Copy link

I can confirm this bug : the new Theme is correctly set by \Magento\Catalog\Helper\Product\View::initProductLayout ; yet, only templates are changed and layout XML updates are ignored...

@shavounet
Copy link

Also, I didn't manage to use my theme in the "Schedule a design update" in product form (outside of any category configuration...)

@shavounet
Copy link

I saw the line "Custom themes now inherit parent XML configuration information as expected." in Magento's 2.1.3 release notes, but after upgrading to 2.1.3 and cleaning every cache, it's still not working.

@shavounet
Copy link

I may have fixed it in my codebase... I used a before plugin on the execute method of catalog/product/view controller. The idea is to set the code bellow as soon as possible (from \Magento\Catalog\Helper\Product\View::initProductLayout).

            $settings = $this->_catalogDesign->getDesignSettings($product);

            if ($settings->getCustomDesign()) {
                $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
            }

The bug is due to the layout being loaded before the custom design is set. It happens twice :

  • vendor/magento/module-catalog/Controller/Product/View.php:108 (with the 'addDefaultHandle')
  • vendor/magento/module-catalog/Helper/Product/View.php:195 (with the 'getUpdate')

@bunsworth94
Copy link

@shavounet I've tried your solution however it doesn't seem to work? Could you expand on this more please?

@shavounet
Copy link

Here is the main code of the plugin I use, on Magento\Catalog\Controller\Product\View :

    /**
     * Custom fix for https://github.com/magento/magento2/issues/7710
     * It allows to use a custom them for product page
     * @see \Magento\Catalog\Controller\Product\View::execute
     * @param View $subject
     */
    public function beforeExecute(View $subject)
    {
        $productId = (int) $subject->getRequest()->getParam('id');
        $categoryId = (int) $subject->getRequest()->getParam('category', false);

        $product = $this->initProduct($productId, $categoryId);
        if($product) {
            $settings = $this->_catalogDesign->getDesignSettings($product);

            if ($settings->getCustomDesign()) {
                $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
            }
        }
    }

    /**
     * Get the product with the given ID, and optionally set the category, if given
     * @param $productId
     * @param $categoryId
     * @return \Magento\Catalog\Model\Product|ProductInterface|bool
     */
    protected function initProduct($productId, $categoryId)
    {

        try {
            $product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId());
        } catch (NoSuchEntityException $e) {
            return false;
        }

        try {
            $category = $this->categoryRepository->get($categoryId);
            $product->setCategory($category);
        } catch (NoSuchEntityException $e) {
            // Do nothing
        }

        return $product;
    }

@dknzdesign
Copy link

I have this problem to. I've been stuck on it for 4 days! Thank so much for the solution. Initially I thought I had made a mistake in the theme layout but no matter what I did to the xml file, it would not render.
Magento 2 seems like its still buggy even after 2.1 has been released.

@eoss-th
Copy link

eoss-th commented May 4, 2017

So i would like to know that this bug will close in 2.1.7 or not?

@surendragamage
Copy link

Go to your database. select theme table. Check custom theme record. If the value of the type field is 1 change it to 0. This fixed my issue.

@veloraven veloraven added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Jun 12, 2017
@veloraven
Copy link
Contributor

Internal ticket was created for this issue: MAGETWO-69709

@worldserved
Copy link

hi shavounet, i have been struggling with this. i cannot figure out how to implement your fix to try it out. i have a child theme (myfirsttheme) with Magento/blank as parent.
I have another child theme (mysecondtheme) with (myfirsttheme) as parent.
The problem is template files amended get parsed in frontend but the xml files do not. i am only seeking to amend the catalog_product_view.xml and it does not parse at all, overriden by settings in the (myfirsttheme)
i think your fix will be a solution for me but i do not know how to implement them. copied and pasted in code at Magento\Module_Catalog\Controller\Product\View.php it didnt work. and tried it in Magento\Module_Catalog\Helper\Product\View.php that also didnt work either. Please can you tell me if that solution fixes my detailed problem and how i should implement it.
Thank you

@dknzdesign
Copy link

@elghana I have a module that you can add to your site that solves this problem using the solution above.
email me for details: info@dknzdesign.com

@DenisNatalevich
Copy link

My solution:

Override\Override\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Helper\Product\View" type="Override\Override\Model\View"/>
</config>

Override\Override\Model\View.php

<?php
namespace Override\Override\Model;

use Magento\Framework\View\Result\Page as ResultPage;

class View extends \Magento\Catalog\Helper\Product\View
{

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Catalog\Model\Design $catalogDesign,
        \Magento\Catalog\Helper\Product $catalogProduct,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
        array $messageGroups = []
    ) {
        parent::__construct(
            $context,
            $catalogSession,
            $catalogDesign,
            $catalogProduct,
            $coreRegistry,
            $messageManager,
            $categoryUrlPathGenerator
        );
    }

    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();

        // NEW CODE
        $category_ids = $product->getData('category_ids');
        if (is_array($category_ids) && count($category_ids)) {
            foreach($category_ids as $category_id) {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $model = $objectManager->create('Magento\Catalog\Api\CategoryRepositoryInterface');
                $category = $model->get($category_id);
                if($category->getData('custom_apply_to_products')) {
                    $layoutUpdates[] = $category->getData('custom_layout_update');
                }
            }
        }

        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;
    }
}

@magento-engcom-team magento-engcom-team added 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development bug report Area: Frontend Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed labels Sep 11, 2017
@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed 2.2.x Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Oct 19, 2017
@magento-engcom-team
Copy link
Contributor

@chickenland, thank you for your report.
We've created internal ticket(s) MAGETWO-69709 to track progress on the issue.

@kirmorozov
Copy link
Member

Same as #8333

@kirmorozov
Copy link
Member

I provided reference fast patch within #8333

@magento-engcom-team
Copy link
Contributor

Hi @chickenland. Thank you for your report.
The issue has been fixed in #19124 by @drew7721 in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.1 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Dec 21, 2018
@uabassguy
Copy link

uabassguy commented Aug 21, 2019

@magento-engcom-team The issue still exists in 2.3.2, updating the type field in the theme table to 0 fixed it in my case, need a more substantial fix. Half of those commits referenced are just comments

#7710 (comment)

@BtotheT
Copy link

BtotheT commented Mar 31, 2020

@shavounet thank you for providing the plugin code you used - that was the ONLY one that worked for a Cloud 2.3.3 instance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Frontend bug report Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests