【问题标题】:Extending a Magento widget layout within a theme在主题中扩展 Magento 小部件布局
【发布时间】:2017-07-07 13:09:44
【问题描述】:

我正在使用 Magento 2 的内置目录产品列表小部件将特色产品小部件添加到网站的主页。我希望做的是扩展在vendor/magento/module-catolog/widget/etc/widget.xml 中找到的小部件布局文件,以便在使用WYSIWIG 编辑器在cms 块中添加此小部件时添加另一个模板选项。

现在,我有一个位于app/design/frontend/Vendor/theme 的主题,我尝试将我自己的widget.xml 文件放在app/design/frontend/Vendor/theme/Magento_CatalogWidget/widget.xml,但这似乎无法覆盖原始widget.xml 文件。

这是我的代码:

<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
            placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
        <label translate="true">Catalog Products List</label>
        <description translate="true">List of Products</description>
        <parameters>
            <parameter name="template" xsi:type="select" required="true" visible="true">
                <label translate="true">Template</label>
                <options>
                    <option name="default" value="product/widget/content/grid.phtml" selected="true">
                        <label translate="true">Products Grid Template</label>
                    </option>
                    <option name="default" value="product/widget/content/alternate-grid.phtml" selected="true">
                        <label translate="true">Alternate Products Grid Template</label>
                    </option>
                </options>
            </parameter>
        </parameters>
    </widget>
</widgets>

我正在尝试在模板参数中添加一个选项,以便在插入小部件时可以选择“备用产品网格模板”。

这是来自magento-catalog-widget 的原始.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
            placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
        <label translate="true">Catalog Products List</label>
        <description translate="true">List of Products</description>
        <parameters>
            <parameter name="title" xsi:type="text" required="false" visible="true">
                <label translate="true">Title</label>
            </parameter>
            <parameter name="show_pager" xsi:type="select" visible="true"
                       source_model="Magento\Config\Model\Config\Source\Yesno">
                <label translate="true">Display Page Control</label>
            </parameter>
            <parameter name="products_per_page" xsi:type="text" required="true" visible="true">
                <label translate="true">Number of Products per Page</label>
                <depends>
                    <parameter name="show_pager" value="1" />
                </depends>
                <value>5</value>
            </parameter>
            <parameter name="products_count" xsi:type="text" required="true" visible="true">
                <label translate="true">Number of Products to Display</label>
                <value>10</value>
            </parameter>
            <parameter name="template" xsi:type="select" required="true" visible="true">
                <label translate="true">Template</label>
                <options>
                    <option name="default" value="product/widget/content/grid.phtml" selected="true">
                        <label translate="true">Products Grid Template</label>
                    </option>
                </options>
            </parameter>
            <parameter name="cache_lifetime" xsi:type="text" visible="true">
                <label translate="true">Cache Lifetime (Seconds)</label>
                <description translate="true">86400 by default, if not set. To refresh instantly, clear the Blocks HTML Output cache.</description>
            </parameter>
            <parameter name="condition" xsi:type="conditions" visible="true" required="true" sort_order="10"
                       class="Magento\CatalogWidget\Block\Product\Widget\Conditions">
                <label translate="true">Conditions</label>
            </parameter>
        </parameters>
        <containers>
            <container name="content">
                <template name="grid" value="default" />
            </container>
            <container name="content.top">
                <template name="grid" value="default" />
            </container>
            <container name="content.bottom">
                <template name="grid" value="default" />
            </container>
        </containers>
    </widget>
</widgets>

我知道如果我成功扩展文件,这将起作用,因为我已经尝试编辑原始文件并在管理中添加小部件时看到了我的新选项。显然这不是最佳实践,我希望仅在我的自定义主题中扩展 widget.xml 文件。

请注意,我的模板位于app/design/frontend/Vendor/theme/Magento_CatalogWidget/templates/product/widget/content 中,其中我同时拥有grid.phtmlalternate-grid.phtml。我没有将alternate-grid.phtml 放在magento_catalog_widget 目录中,但是在更改该目录(在我的主题之外)中的原始widget.xml 文件时,我能够访问它。这就是为什么我相当有信心这个问题是覆盖小部件的布局文件的问题。

如果有人能回答这个问题,我认为这对其他开发人员也很有帮助,因为这将允许开发人员自定义任何 Magento 内置小部件中的选项,而不会干扰基本框架。

【问题讨论】:

    标签: xml magento layout widget


    【解决方案1】:

    你需要创建app/design/frontend/Vendor/theme/etc/widget.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
    /**
     * Copyright © 2013-2017 Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
        <widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
                placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
            <label translate="true">Catalog Products List</label>
            <description translate="true">List of Products</description>
            <parameters>
                <parameter name="template" xsi:type="select" required="true" visible="true">
                    <label translate="true">Template</label>
                    <option name="list" value="product/widget/content/alternate-grid.phtml">
                        <label translate="true">Alternate Products Grid Template</label>
                    </option>
                </parameter>
            </parameters>
        </widget>
    </widgets>
    

    【讨论】:

      【解决方案2】:

      我知道这有点晚了,但我只是想出了如何做到这一点,也许它会对某人有所帮助。

      文件app/design/frontend/Vendor/theme/etc/widget.xml 应如下所示:

      <widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
      <widget id="products_list" class="Magento\CatalogWidget\Block\Product\ProductsList" is_email_compatible="true"
              placeholder_image="Magento_CatalogWidget::images/products_list.png" ttl="86400">
          <label translate="true">Catalog Products List</label>
          <description translate="true">List of Products</description>
          <parameters>
              <parameter name="template" xsi:type="select" required="true" visible="true">
                  <label translate="true">Template</label>
                  <options>
                      <option name="default" value="product/widget/content/grid.phtml">
                          <label translate="true">Products Grid Template</label>
                      </option>
                      <option name="alternate_grid" value="product/widget/content/alternate-grid.phtml">
                          <label translate="true">Alternate Grid Template</label>
                      </option>
                  </options>
              </parameter>
          </parameters>
          <containers>
              <container name="content">
                  <template name="grid" value="default"/>
                  <template name="alternate_grid" value="alternate_grid"/>
              </container>
              <container name="content.top">
                  <template name="grid" value="default"/>
                  <template name="alternate_grid" value="alternate_grid"/>
              </container>
              <container name="content.bottom">
                  <template name="grid" value="default"/>
                  <template name="alternate_grid" value="alternate_grid"/>
              </container>
          </containers>
      </widget>
      

      模板文件应该在app/design/frontend/Vendor/theme/Magento_CatalogWidget/templates/product/widget/content/alternate-grid.phtml

      并且不要忘记在创建/编辑 module.xml 后清除缓存

      【讨论】:

        猜你喜欢
        • 2017-09-23
        • 2015-02-21
        • 2014-09-05
        • 2016-08-22
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 2017-02-16
        • 1970-01-01
        相关资源
        最近更新 更多