How to add a custom attribute in Product Detail page and save it's value in quote table after the add to cart is clicked?
-
what type of attribute you want, is it customer or product type attribute– Afzel ArshadCommented Aug 24, 2022 at 17:11
-
it is like an input field in the product page. if we type something in that field it should be saved in database.– Mohmad Ashik M ACommented Aug 25, 2022 at 5:18
Add a comment
|
1 Answer
Here im creating a product type custom attribute
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_product_view.xml
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="page.main.title">
<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>
</referenceContainer>
</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; ?>
Here i overrided the PDP Page and called that custom attribute
-
It didn't work for me. I have created all the three files in an empty module. Commented Aug 25, 2022 at 5:17