Skip to content

Commit

Permalink
Creacion de atributo para producto
Browse files Browse the repository at this point in the history
1.- Creacion de atributo con lista desplegable ( dropdown-type ) de nombre ( clothing_material ), que contendrá las siguientes opciones :
    [ Cotton, Leather, Silk, Denim, Fur, and Wool ]
2.- El atributo estará visible en la vista del cliente con negritas y sera seleccionable en el lado del backend

      ================    PASOS PARA LA CREACION ===============================
1.- Creacion del nuevo modulo
    directrio : Learning/ClothingMaterial
    registro del moduo : etc/module.xml
    registro del modulo para magento : registration.php
    Más info de como crear un moduo : 38ff335
2.- Creacion de script que agrega atributos a las tablas EAV ( eav_attribute | catalog_eav_attribute ):
    app/code/Learning/ClothingMaterial/Setup/InstallData.php
    OJO en la linea 44 se crea un objeto especia de EAV (eavSetup) y para ello es importante instanciar a eavSetupFactory en el constructor
    ya que catalog es una entidad de EAV
3.- Creacion de source model
    clase que contiene la lista de opciones que contendrá nuestro nuevo a tributo, y que fue referenciado en Installdata en addAttribute()
4.- Creacion de backend model:
    En esta clase se hace la validacion de que el unico valor que se pueda asignar sea lana(wool)
    En este caso el id esta estatico , verificar el id correspondiente en la tabla eav_attribute_set;
5.- Creacion de Frontend model
    aqui definimos que el valor seleccionado se muestre en negritas en la interfaz de la tienda para el cliente
6.- Finalmente actualiamos la BDD : php bin/magento setup:upgrade
    compilamos si es necesario y asignamos permisos como se muestra en el commit de creacion de modulo
7.- Verificamos en nuestra BDD que se encuentre registrado el codigo del atributo en EAV_ATTRIBUTE y en CATALOG_EAV_ATTRIBUTE ;
  • Loading branch information
PerezContrerasLuis committed Jan 18, 2022
1 parent 7c3a945 commit a4d78d0
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
Binary file added code/Learning/ClothingMaterial/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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
{
/**
* Validate
* @param Product $object
* @throws LocalizedException
* @return bool
*/
public function validate($object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if ( ($object->getAttributeSetId() == 10) && ($value == 'wool')) {
throw new LocalizedException(
__('Bottom can not be wool.')
);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
return "<b>$value</b>";
}
}
31 changes: 31 additions & 0 deletions code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Learning\ClothingMaterial\Model\Attribute\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Material extends AbstractSource
{
/**
* Get all options
* @return array
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
['label' => __('Cotton'), 'value' => 'cotton'],
['label' => __('Leather'), 'value' => 'leather'],
['label' => __('Silk'), 'value' => 'silk'],
['label' => __('Denim'), 'value' => 'denim'],
['label' => __('Fur'), 'value' => 'fur'],
['label' => __('Wool'), 'value' => 'wool'],
];
}
return $this->_options;
}
}
68 changes: 68 additions & 0 deletions code/Learning/ClothingMaterial/Setup/InstallData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Learning\ClothingMaterial\Setup;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
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.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
Product::ENTITY,
'clothing_material',
[
'group' => 'General',
'type' => 'varchar',
'label' => 'Clothing Material',
'input' => 'select',
'source' => 'Learning\ClothingMaterial\Model\Attribute\Source\Material',
'frontend' => 'Learning\ClothingMaterial\Model\Attribute\Frontend\Material',
'backend' => 'Learning\ClothingMaterial\Model\Attribute\Backend\Material',
'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_front' => true,
'visible_on_front' => true
]
);
}
}
12 changes: 12 additions & 0 deletions code/Learning/ClothingMaterial/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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">
</module>
</config>
13 changes: 13 additions & 0 deletions code/Learning/ClothingMaterial/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Learning_ClothingMaterial',
__DIR__
);
1 change: 1 addition & 0 deletions etc/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
'Klarna_Ordermanagement' => 1,
'Klarna_Onsitemessaging' => 1,
'Klarna_Kp' => 1,
'Learning_ClothingMaterial' => 1,
'Learning_FirstUnit' => 1,
'Learning_HelloPage' => 1,
'Luisdev_Helloworld' => 1,
Expand Down

0 comments on commit a4d78d0

Please sign in to comment.