Skip to content

Commit

Permalink
Merge pull request #265 from rafaelpatro/bugfix_configurable_products
Browse files Browse the repository at this point in the history
Correção para Produtos Configuráveis
  • Loading branch information
pedro-teixeira authored May 28, 2017
2 parents 7dae8b4 + 631083c commit f13c92d
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,27 @@ protected function _throwError($message, $log = null, $line = 'NO LINE', $custom
$this->_result->append($error);
}

/**
* Retrieves a simple product
*
* @param Mage_Catalog_Model_Product $product Catalog Product
*
* @return Mage_Catalog_Model_Product
*/
protected function _getSimpleProduct($product)
{
$type = $product->getTypeInstance(true);
if ($type->getProduct($product)->hasCustomOptions()
&& ($simpleProductOption = $type->getProduct($product)->getCustomOption('simple_product'))
) {
$simpleProduct = $simpleProductOption->getProduct($product);
if ($simpleProduct) {
return $this->_getSimpleProduct($simpleProduct);
}
}
return $type->getProduct($product);
}

/**
* Generate Volume weight
*
Expand All @@ -441,7 +462,7 @@ protected function _generateVolumeWeight($request)
$items = $this->_getRequestItems($request);

foreach ($items as $item) {
$_product = $item->getProduct();
$_product = $this->_getSimpleProduct($item->getProduct());

if ($_product->getData('volume_altura') == '' || (int) $_product->getData('volume_altura') == 0) {
$itemAltura = $this->getConfigData('altura_padrao');
Expand Down Expand Up @@ -850,8 +871,12 @@ protected function _filterMethodByItemRestriction($request)
$items = $this->_getRequestItems($request);
$intersection = $this->_postMethodsExplode;
foreach ($items as $item) {
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$prodPostMethods = explode(',', $product->getData('postmethods'));
$product = $this->_getSimpleProduct($item->getProduct());
$prodPostMethods = explode(
',', $product->getResource()->getAttributeRawValue(
$product->getId(), 'postmethods', $request->getStoreId()
)
);
$intersection = array_intersect($prodPostMethods, $intersection);
}

Expand Down Expand Up @@ -880,7 +905,7 @@ protected function _filterMethodByItemRestriction($request)
*/
protected function _getFitHeight($item)
{
$product = $item->getProduct();
$product = $this->_getSimpleProduct($item->getProduct());
$height = $product->getData('volume_altura');
$height = ($height > 0) ? $height : (int) $this->getConfigData('altura_padrao');
$fitSize = (float) $product->getData('fit_size');
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/PedroTeixeira/Correios/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<config>
<modules>
<PedroTeixeira_Correios>
<version>4.8.1</version>
<version>4.9.0</version>
<depends>
<Mage_Shipping/>
</depends>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/

/** @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

$status = Mage::getModel('sales/order_status');
$status->setStatus(PedroTeixeira_Correios_Model_Sro::ORDER_SHIPPED_STATUS)
->setLabel('Pedido em Transporte')
->assignState(Mage_Sales_Model_Order::STATE_COMPLETE)
->save();

$status = Mage::getModel('sales/order_status');
$status->setStatus(PedroTeixeira_Correios_Model_Sro::ORDER_WARNED_STATUS)
->setLabel('Dificuldade de Entrega')
->assignState(Mage_Sales_Model_Order::STATE_COMPLETE)
->save();

/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

// Add volume to prduct attribute set
$codigo = 'volume_comprimento';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Comprimento (cm)',
'type' => 'int',
'input' => 'text',
'apply_to' => 'simple,grouped',
'note' => 'Comprimento da embalagem do produto (Para cálculo dos Correios)'
);

$setup->addAttribute('catalog_product', $codigo, $config);

// Add volume to prduct attribute set
$codigo = 'volume_altura';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Altura (cm)',
'type' => 'int',
'input' => 'text',
'apply_to' => 'simple,grouped',
'note' => 'Altura da embalagem do produto (Para cálculo dos Correios)'
);

$setup->addAttribute('catalog_product', $codigo, $config);

// Add volume to prduct attribute set
$codigo = 'volume_largura';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Largura (cm)',
'type' => 'int',
'input' => 'text',
'apply_to' => 'simple,grouped',
'note' => 'Largura da embalagem do produto (Para cálculo dos Correios)'
);

$setup->addAttribute('catalog_product', $codigo, $config);

$codigo = 'postmethods';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Serviços de Entrega',
'type' => 'text',
'input' => 'multiselect',
'source' => 'pedroteixeira_correios/source_postMethods',
'backend' => 'eav/entity_attribute_backend_array',
'apply_to' => 'simple,grouped',
'note' => 'Selecione os serviços apropriados para o produto.'
);

$setup->addAttribute('catalog_product', $codigo, $config);

$codigo = 'fit_size';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Diferença do Encaixe (cm)',
'type' => 'varchar',
'input' => 'text',
'apply_to' => 'simple,grouped',
'note' => 'Exemplo: Se 1 item mede 10cm de altura, e 2 itens encaixados medem 11cm. A diferença é de 1cm.'
);

$setup->addAttribute('catalog_product', $codigo, $config);

$codigo = 'posting_days';
$config = array(
'position' => 1,
'required' => 0,
'label' => 'Prazo de Postagem',
'type' => 'int',
'input' => 'text',
'apply_to' => 'simple,grouped',
'note' => 'O prazo total é o Prazo dos Correios acrescido do maior Prazo de Postagem dos produtos no carrinho.'
);

$setup->addAttribute('catalog_product', $codigo, $config);

// Add Correios Tab
$setIds = $setup->getAllAttributeSetIds('catalog_product');

$attributes = array(
'volume_comprimento',
'volume_altura',
'volume_largura',
'postmethods',
'fit_size',
'posting_days'
);

foreach ( $setIds as $setId ) {

$setup->addAttributeGroup('catalog_product', $setId, 'Correios', 2);
$groupId = $setup->getAttributeGroupId('catalog_product', $setId, 'Correios');

foreach ( $attributes as $attribute ) {
$attributeId = $setup->getAttributeId('catalog_product', $attribute);
$setup->addAttributeToGroup('catalog_product', $setId, $groupId, $attributeId);
}

}

$tableName = $this->getTable('pedroteixeira_correios/postmethod');
$table = $installer->getConnection()
->newTable($tableName)
->addColumn(
'method_id',
Varien_Db_Ddl_Table::TYPE_INTEGER,
null,
array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
),
'ID'
)->addColumn(
'method_code',
Varien_Db_Ddl_Table::TYPE_TEXT,
5,
array(
'nullable' => false,
'default' => '0'
),
'Code'
)->addColumn(
'method_title',
Varien_Db_Ddl_Table::TYPE_TEXT,
255,
array(
'nullable' => false,
'default' => ''
),
'Title'
)->addIndex(
$installer->getIdxName(
$tableName,
array('method_code'),
Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE
),
array('method_code'),
array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)
);
$installer->getConnection()->createTable($table);

$services = array(
array(
'method_code' => '40045',
'method_title' => 'SEDEX A COBRAR SEM CONTRATO',
),
array(
'method_code' => '40215',
'method_title' => 'SEDEX 10 SEM CONTRATO',
),
array(
'method_code' => '40290',
'method_title' => 'SEDEX HOJE SEM CONTRATO',
),
array(
'method_code' => '04510',
'method_title' => 'PAC SEM CONTRATO',
),
array(
'method_code' => '04014',
'method_title' => 'SEDEX SEM CONTRATO',
),
array(
'method_code' => '04669',
'method_title' => 'PAC CONTRATO AGENCIA',
),
array(
'method_code' => '04162',
'method_title' => 'SEDEX CONTRATO AGENCIA',
),
array(
'method_code' => '04693',
'method_title' => 'PAC CONTRATO GRANDES FORMATOS',
),
array(
'method_code' => '10065',
'method_title' => 'CARTA COMERCIAL A FATURAR',
)
);

foreach ($services as $service) {
$installer->getConnection()->insertOnDuplicate($tableName, $service, array('method_code'));
}

$installer->endSetup();
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This source file is subject to the MIT License.
* It is also available through http://opensource.org/licenses/MIT
*
* @category PedroTeixeira
* @package PedroTeixeira_Correios
* @author Pedro Teixeira <hello@pedroteixeira.io>
* @copyright 2015 Pedro Teixeira (http://pedroteixeira.io)
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/pedro-teixeira/correios
*/

/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->updateAttribute(
'catalog_product',
'volume_altura',
'apply_to',
'simple,grouped'
);

$setup->updateAttribute(
'catalog_product',
'volume_largura',
'apply_to',
'simple,grouped'
);

$setup->updateAttribute(
'catalog_product',
'volume_comprimento',
'apply_to',
'simple,grouped'
);

$setup->updateAttribute(
'catalog_product',
'postmethods',
'apply_to',
'simple,grouped'
);

$setup->updateAttribute(
'catalog_product',
'fit_size',
'apply_to',
'simple,grouped'
);

$setup->updateAttribute(
'catalog_product',
'posting_days',
'apply_to',
'simple,grouped'
);

$installer->endSetup();

0 comments on commit f13c92d

Please sign in to comment.