I want to add the brand name and quantity in the catalog category page. How do I edit this from the category page?
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?
I want to add the brand name and quantity in the catalog category page. How do I edit this from the category page?
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?
Try this code,
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; ?>