Crear un atributo de producto
5 de mayo de 2025
- Temas:
- Configuración
- Desarrollo back-end
Creado para:
- Principiante
- Intermedio
- Administrador
- Usuario
Agregar un atributo de producto es una de las operaciones más populares en Commerce. Los atributos son una forma eficaz de resolver muchas tareas prácticas relacionadas con un producto. Hay un proceso sencillo para añadir un atributo de tipo desplegable a un producto.
En este vídeo:
- Agregue un atributo llamado ropa_material con los valores posibles: Algodón, Cuero, Seda, Vaquero, Piel y Lana
- Haga que este atributo esté visible en la página de vista del producto con el texto en negrita
- Asígnelo al conjunto de atributos predeterminado y agregue una restricción
- Añadir el nuevo atributo
¿Para quién es este vídeo?
- Desarrolladores nuevos en el comercio que necesitan aprender a crear un atributo de producto mediante programación
Contenido de vídeo
Ejemplo de código
Primero cree las carpetas, archivos xml y PHP que sean necesarios:
- app/code/Learning/ClothingMaterial/registration.php
- app/code/Learning/ClothingMaterial/etc/module.xml
- app/code/Learning/ClothingMaterial/Model/Attribute/Backend/Material.php
- app/code/Learning/ClothingMaterial/Model/Attribute/Frontend/Material.php
- app/code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php
- app/code/Learning/ClothingMaterial/Setup/InstallData.php
app/code/Learning/ClothingMaterial/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Learning_ClothingMaterial',
__DIR__);
app/code/Learning/ClothingMaterial/etc/module.xml
Si el módulo utiliza un esquema declarativo y la mayoría lo ha hecho desde la versión 2.3.0, debe omitir setup_version. Sin embargo, si tiene algunos proyectos heredados, puede ver este método utilizado. Consulte developer.adobe.com para obtener más información.
POR FAVOR NOTA: para que este código de ejemplo funcione, usted necesita incluir la setup_version de lo contrario el InstallData.php no se ejecuta.
POR FAVOR NOTA: para que este código de ejemplo funcione, usted necesita incluir la setup_version de lo contrario el InstallData.php no se ejecuta.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Learning_ClothingMaterial" setup_version="0.0.1"/>
</config>
app/code/Learning/ClothingMaterial/Model/Attribute/Backend/Material.php
Asegúrese de utilizar el ID del conjunto de atributos que se encuentra en el proyecto; en este ejemplo, es el número 9.
<?php
declare(strict_types=1);
namespace Learning\ClothingMaterial\Model\Attribute\Backend;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
use Magento\Framework\Exception\LocalizedException;
class Material extends AbstractBackend
{
/**
* @param Product @object
* @throws LocalizedException
*/
public function validate($object)
{
$value =$object->getData($this->getAttribute()->getAttributeCode());
// Be sure to validate that your ID number it is likely to be different
if (($object->getAttributeSetId() == 9) && ($value == 'fur')) {
throw new LocalizedException(__('Bottoms cannot be fur'));
}
}
}
app/code/Learning/ClothingMaterial/Model/Attribute/Frontend/Material.php
<?php
declare(strict_types=1);
namespace Learning\ClothingMaterial\Model\Attribute\Frontend;
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use Magento\Framework\DataObject;
class Material extends AbstractFrontend
{
public function getValue(DataObject $object): string
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
return "<b>$value</b>";
}
}
app/code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php
<?php
declare(strict_types=1);
namespace Learning\ClothingMaterial\Model\Attribute\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
class Material extends AbstractSource
{
/**
* Get all options
*
* @retrun array
*/
public function getAllOptions(): array
{
if(!$this->_options){
$this->_options = [
['label' => __('Cotton'), 'value' => 'cotton'],
['label' => __('Leather'), 'value' => 'leather'],
['label' => __('Silk'), 'value' => 'silk'],
['label' => __('Fur'), 'value' => 'fur'],
['label' => __('Wool'), 'value' => 'wool'],
];
}
return $this->_options;
}
}
app/code/Learning/ClothingMaterial/Setup/InstallData.php
<?php
declare(strict_types=1);
namespace Learning\ClothingMaterial\Setup;
use Learning\ClothingMaterial\Model\Attribute\Frontend\Material as Frontend;
use Learning\ClothingMaterial\Model\Attribute\Source\Material as Source;
use Learning\ClothingMaterial\Model\Attribute\Backend\Material as Backend;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
protected $eavSetupFactory;
public function __construct(
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* {@inheritDoc}
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @suppressWarnings(PHPMD.ExessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
Product::ENTITY,
'clothing_material',
[
'group' => 'Product Details',
'type' => 'varchar',
'label' => 'Clothing Material',
'input' => 'select',
'source' => Source::class,
'frontend' => Frontend::class,
'backend' => Backend::class,
'required' => false,
'sort_order' => 50,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'visible' => true,
'is_html_allowed_on_frontend' => true,
'visible_on_front' => true,
]
);
}
}
Recursos útiles
recommendation-more-help
3a5f7e19-f383-4af8-8983-d01154c1402f