-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7c3a945
commit a4d78d0
Showing
8 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
31 changes: 31 additions & 0 deletions
31
code/Learning/ClothingMaterial/Model/Attribute/Backend/Material.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
code/Learning/ClothingMaterial/Model/Attribute/Frontend/Material.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
code/Learning/ClothingMaterial/Model/Attribute/Source/Material.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters