【问题标题】:I'm trying to show configurable swatches on custom category page (magento 1.9)我正在尝试在自定义类别页面(magento 1.9)上显示可配置的色板
【发布时间】:2025-11-21 18:50:01
【问题描述】:

我使用的是 Magento 1.9.1.0,并且在类别和产品视图页面上有可配置的样本。

我正在尝试创建一个自定义“商店”页面,其中列出了所有产品(商店只有 +/-20 个),并且在产品下方显示了可配置的样本。

我可以通过多种方式创建列出所有产品的 Shop 页面。通过 CMS、local.xml 或使用控制器等...没有问题。

这是 local.xml 示例。 *我有适当的路线设置,默认类别设置为“是锚”。

<mystore_site_index_shop>
    <reference name="content">
            <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                <block type="core/text_list" name="product_list.name.after" as="name.after" />
                <block type="core/text_list" name="product_list.after" as="after" />
                <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                    <block type="page/html_pager" name="product_list_toolbar_pager"/>
                </block>
            </block>
    </reference>
</mystore_site_index_shop>

我还在 configureswatches.xml 中添加了对页面的页面引用

<mystore_site_index_shop>
    <update handle="product_list"/>
</mystore_site_index_shop>

将适当的 .js 文件加载到页面...但是没有显示样本。

有人对我如何实现这一点有任何建议吗?我必须在这里遗漏一些明显的东西..

谢谢!

【问题讨论】:

    标签: php magento magento-1.9 magento-1.9.1


    【解决方案1】:

    如果你去 app/code/core/Mage/ConfigurableSwatches/etc/config.xml 您将在第 83 行的 catalog_block_product_list_collection 事件中看到观察者。

      <catalog_block_product_list_collection>
                    <observers>
                        <configurableswatches>
                            <class>configurableswatches/observer</class>
                            <method>productListCollectionLoadAfter</method>
                        </configurableswatches>
                    </observers>
                </catalog_block_product_list_collection>
    如果您评论此代码,您将不会在产品列表页面上看到样本。

    在事件中添加您的观察者方法以添加样本。

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的指点...关于如何为此设置观察者还有更多建议吗?我已将(上面的)块复制到我的模块 config.xml 中,但我确信它还有更多内容。事件/观察者对我来说真的很陌生
    【解决方案2】:

    在列表页面上添加色板是通过多种方式完成的

    我会为你提供方法。

    方法一: 你可以使用这个免费的模块,也可以通过学习模块来学习做事方式

    http://magebug.blogspot.in/2013/06/magento-how-to-display-color-options-in.html

    方法 2:[这将显示可配置产品的所有选项]

    <?php if($_product->isConfigurable()): ?>
      //get attributes
      <?php $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ?>
      <?php if(count($attributes)): ?>
        <ul>
        <?php foreach($attributes as $att): ?>
          <?php $pAtt=$att->getProductAttribute();
            //get the child products
            $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
            $frontValues =array() ?>
          <li><?php echo $pAtt->getFrontendLabel() ?>
           <ul>
           <?php foreach($allProducts as $p): ?>
             //check stock, status, ...
             //do not show unsaleable options
             <?php if(!$p->isSaleable()) continue; ?>
             <?php $out=$p->getAttributeText($pAtt->getName()); ?>
             <?php $frontValues[$out]=$out; ?>
           <?php endforeach ?>
            <li><?php echo implode('</li><li>', $frontValues) ?></li>
           </ul>
          </li>
        <?php endforeach ?>
        </ul>
      <?php endif ?>
    <?php endif ?>
    

    您可以用图片或标签替换下拉菜单。

    希望这对你有用。

    【讨论】:

      【解决方案3】:

      尝试在 mystore_site_index_shop 块之后的 local.xml 文件中添加产品列表更新。

      在local.xml中

      <mystore_site_index_shop>
         <update handle="product_list"/>
      </mystore_site_index_shop>
      

      --编辑--

      即使我能够使用我上面发布的方法来完成这项工作。据我了解,正确的方法是将您的整个主题构建为 rwd 主题的子主题,让您无需任何技巧即可使用色板。

      在 app/design/frontend/YOURTHEME/default/etc/theme.xml 中

      <?xml version="1.0"?>
      <theme>
      <parent>rwd/default</parent>
      </theme>
      

      【讨论】:

      • 在 '..index_shop' 块之后添加 'product_list' 块就成功了!我实际上是直接使用 RWD 主题——奇怪吗?谢谢!
      【解决方案4】:

      如果你查看 Mage_ConfigurableSwatches_Model_Observer,你有

        public function convertLayerBlock(Varien_Event_Observer $observer)
      {
          $front = Mage::app()->getRequest()->getRouteName();
          $controller = Mage::app()->getRequest()->getControllerName();
          $action = Mage::app()->getRequest()->getActionName();
      
          // Perform this operation if we're on a category view page or search results page
          if (($front == 'catalog' && $controller == 'category' && $action == 'view')
              || ($front == 'catalogsearch' && $controller == 'result' && $action == 'index')) {
      
              // Block name for layered navigation differs depending on which Magento edition we're in
              $blockName = 'catalog.leftnav';
              if (Mage::getEdition() == Mage::EDITION_ENTERPRISE) {
                  $blockName = ($front == 'catalogsearch') ? 'enterprisesearch.leftnav' : 'enterprisecatalog.leftnav';
              } elseif ($front == 'catalogsearch') {
                  $blockName = 'catalogsearch.leftnav';
              }
              Mage::helper('configurableswatches/productlist')->convertLayerBlock($blockName);
          }
      }
      

      如您所见,条件需要您的路由 ($front,$controller,$action) 来处理。您可以通过重写并添加您的条件来覆盖此观察者。

      【讨论】:

        最近更新 更多