1

I want to add the brand name and quantity in the catalog category page. How do I edit this from the category page?

enter image description here

I see image_with borders.phtml but I can't seem to see where to add the brand name and how do I retrieve it?

2
  • seems like you are using custom theme.. you need to create brand name product attribute and qty in your custom theme product list phtml Commented Aug 27, 2022 at 9:33
  • yes it is a custom theme extended from Luma theme. Can you expound further? Commented Aug 29, 2022 at 21:20

1 Answer 1

1

Try this code,

  • First create your custom attribute with name brand
  • then this attribute will be showing in product admin grid edit form
  • and then have to add brand name of each product from product admin grid edit form,
  • and finally call this attribute anywhere you like

app/code/Vendor/Module/Setup/Patch/Data/Brand.php

<?php

declare (strict_types=1);

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * Class CreateCustomAttr for Create Custom Product Attribute using Data Patch.
 */
class Brand implements DataPatchInterface
{
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private ModuleDataSetupInterface $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @var EavSetupFactory
     */
    private EavSetupFactory $eavSetupFactory;

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

    /**
     * {@inheritdoc}
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            Product::ENTITY,
            'Brand',
            [
                'type' => 'text',
                'frontend' => '',
                'label' => 'Brand',
                'input' => 'textarea',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => false,
                'is_wysiwyg_enabled' => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases(): array
    {
        return [];
    }
}

Vendor/Module/view/frontend/layout/catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="category.products.list">
             <block class="Magento\Catalog\Block\Product\View" name="product.attribute.custom" template="Vendor_Module::product/view/attribute/brand.phtml">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Brand:</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Vendor/Module/view/frontend/templates/product/view/attribute/brand.phtml

<?php

/**
 * @var Magento\Catalog\Block\Product\View $block
 */
$product = $block->getProduct();
$attrData = $product->getData('Brand');
$blockTitle = $this->getData('title');
if (trim($attrData) !== "") : ?>
    <div class="product-full-width-section">
        <h2 class="product-section-title"><?= $blockTitle; ?></h2>
        <?php echo $attrData; ?>
    </div>
<?php endif; ?>
1

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.