0

I have created a custom EAV attribute called "Force Grid Mod" using the script below. Now I don't know how to show its value in list.phtml. Its working on the backend. Does someone knows how can I show its value in my list.phtml ?

<?php


namespace VendorName\ModuleName\Setup;

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

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav/attribute
         */

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'force_grid_mode',
            [
                        'type' => 'int',
                        'label' => 'Force Grid mod',
                        'input' => 'select',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'required' => false,
                        'sort_order' => 30,
                        'group' => 'Display Settings',
            ]
        );
    }
}

1 Answer 1

4

Do you mean /module-catalog/view/frontend/templates/product/list.phtml? If yes then you can access current category through layer object:

$_layer = $this->getLayer();
$_category = $_layer->getCurrentCategory();

And on this category object you can call your new attribute:

$_category->getForceGridMode()
2
  • I mean app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/list.phtml . I just tried it there but doesn't print nothing.
    – user37025
    Commented Jun 9, 2016 at 21:16
  • Please, apologize my stupidity. Worked fine, it's just because when the answer is No it shows nothing, and only shows 1 when the answer is Yes.
    – user37025
    Commented Jun 9, 2016 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.