首页>Magento>source

我在图库中隐藏了我的缩略图,因为我在类别列表中显示的图像与我想要在图库中显示的图像不同(它有额外的位)。

在JsonConfig对象中,我从样本中获取,我有来自Gallery的图像列表,但不是我的缩略图.

如何修改JsonConfig对象以向其添加项目?

我是否将问题扩展到"如何在JsonConfig对象中添加另一个图像角色?(即不仅仅是缩略图)"

最新回答
  • 2019-12-5
    1 #

    这个想法是为了扩展类 \Magento\Swatches\Block\Product\Renderer\Listing\Configurable   特别是方法 _getAdditionalConfig() .下面是我的代码,我想要提取的2个图像角色叫做 category_mobile   和 category_tablet .

    app/code/Memes/Extendjsonconfig/registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Memes_Extendjsonconfig',
    __DIR__
    );
    

    app/code/Memes/Extendjsonconfig/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="Magento\Swatches\Block\Product\Renderer\Listing\Configurable" type="Memes\Extendjsonconfig\Block\Product\Renderer\Listing\Configurable" />
    </config>
    

    app/code/Memes/Extendjsonconfig/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Memes_Extendjsonconfig" setup_version="0.0.1"/>
    </config>
    

    app/code/Memes/Extendjsonconfig/Block/Product/Renderer/Listing/configurable.php

    <?php
    /**
     * Created by memes (http://chezmemes.com)
     * Date: 26/10/2016
     * Time: 6:02 PM
     */
    namespace Memes\Extendjsonconfig\Block\Product\Renderer\Listing;
    
    class Configurable extends \Magento\Swatches\Block\Product\Renderer\Listing\Configurable
    {
    
    protected function _getAdditionalConfig()
    {
        $image_mobile = 'category_mobile';
        $image_tablet = 'category_tablet';
        return [$image_mobile => $this->getOptionImages($image_mobile), $image_tablet => $this->getOptionImages($image_tablet) ];
    }
    protected function getOptionImages($imageRole)
    {
        $images = [];
        foreach ($this->getAllowProducts() as $product) {
            $mobileImage = $product->getData($imageRole);   
                    if(isset($mobileImage) && $mobileImage != 'no_selection' ){
                        $mobileImageAttr = $product->getCustomAttribute( $imageRole );
                        $image = $mobileImageAttr->getValue();
                    }
                    else{
                        $image="";
                    } 
            $images[$product->getId()] =
                [
                    'path' => $image
                ];
        }
        return $images;
    }
    }
    

    这给了我 http://www.store.com/pub/media/catalog/product文件夹中图像的相对路径 ,然后我可以在我的Javascript中使用它来更新我想要的任何内容。

    这很容易扩展到所需的任何其他属性。

    一个变体是使用插件而不是偏好,这在本问题中讨论

  • 测试magento核心功能
  • checkout:以magento编程方式创建订单