更改Magento 2.1中的产品视图以获取特定类别

时间:2017-10-14 11:50:25

标签: magento

我需要为特定类别的所有产品自定义产品视图。

我想删除右侧的产品图片和信息(红色)。 然后我需要产品信息(用黄色圈出)来填充宽度(蓝线)。Colour coded screen shot

1 个答案:

答案 0 :(得分:0)

您需要执行以下步骤:

1.您需要创建自定义模块和创建setup/installData.php文件并添加此

<?php

namespace Test\Customcategorymode\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;


class InstallData implements InstallDataInterface {

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY, 'show_mode', [
            'type' => 'varchar',
            'label' => 'Show Mode',
            'input' => 'select',
            'required' => false,
            'source' => 'W3solver\Customcategorymode\Model\Category\Attribute\Source\Showmode',
            'sort_order' => 102,
            'visible' => true,
            'user_defined' => true,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'Display Settings',
                ]
        );


        $setup->endSetup();
    }

}

2.要查看管理面板中的显示模式字段,需要在view/adminhtml/ui_component/category_form.xml

中创建UI组件
<?xml version="1.0" encoding="UTF-8"?>
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <fieldset name="display_settings">
            <field name="show_mode">
                <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">W3solver\Customcategorymode\Model\Category\Attribute\Source\Showmode</item>
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">102</item>
                        <item name="dataType" xsi:type="string">string</item>
                        <item name="formElement" xsi:type="string">select</item>
                        <item name="label" xsi:type="string" translate="true">Show Mode</item>
                    </item>
                </argument>
            </field>   
        </fieldset>
</form>

3.另外在Model/Category/Attribute/Source/Showmode.php中添加源模型并写下以下函数: -

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Test\Customcategorymode\Model\Category\Attribute\Source;

/**
 * Catalog category landing page attribute source
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class ShowMode extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
     public function getAllOptions()
    {
        if (!$this->_options) {
            $this->_options = [
                ['value' => 'type1', 'label' => __('type1')],
                ['value' => 'type2', 'label' => __('type2')],
            ];
        }
        return $this->_options;
    }
}

4.现在您需要覆盖所有要更改的文件

5.将这些条件添加到您想要更改的所有产品文件中:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product




        $cats = $product->getCategoryIds();
        if(count($cats) ){
            $firstCategoryId = $cats[0];
            $category = $objectManager->create('Magento\Catalog\Model\Category')->load($firstCategoryId);
        }

   if ($category->getShowMode() == 'type1') {
            // add your code
   } else {
       // add your code
   }