Skip to content

Commit

Permalink
Merge pull request #7 from mygento/rate-ext-attributes
Browse files Browse the repository at this point in the history
Rate ext attributes
  • Loading branch information
luckyraul authored Feb 20, 2018
2 parents c28055c + 1a9e34c commit 6951570
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 2 deletions.
26 changes: 26 additions & 0 deletions Plugin/ExtRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @author Mygento
* @copyright 2017 Mygento (https://www.mygento.ru)
* @package Mygento_Shipment
*/

namespace Mygento\Shipment\Plugin;

class ExtRate
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterImportShippingRate(
\Magento\Quote\Model\Quote\Address\Rate $subject,
$result,
\Magento\Quote\Model\Quote\Address\RateResult\AbstractResult $rate
) {
$result->setEstimate($rate->getEstimate());
$result->setLatitude($rate->getLatitude());
$result->setLongitude($rate->getLongitude());

return $result;
}
}
38 changes: 38 additions & 0 deletions Plugin/ExtShippingMethodManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @author Mygento
* @copyright 2017 Mygento (https://www.mygento.ru)
* @package Mygento_Shipment
*/

namespace Mygento\Shipment\Plugin;

class ExtShippingMethodManagement
{
protected $shippingExtAttr;

public function __construct(
\Magento\Quote\Api\Data\ShippingMethodExtensionFactory $shippingExtAttr
) {
$this->shippingExtAttr = $shippingExtAttr;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterModelToDataObject(
\Magento\Quote\Model\Cart\ShippingMethodConverter $subject,
$result,
\Magento\Quote\Model\Quote\Address\Rate $rateModel
) {
$extensionAttributes =
$result->getExtensionAttributes()
?? $this->shippingExtAttr->create();
$extensionAttributes->setEstimate($rateModel->getEstimate());
$extensionAttributes->setLatitude($rateModel->getLatitude());
$extensionAttributes->setLongitude($rateModel->getLongitude());
$result->setExtensionAttributes($extensionAttributes);

return $result;
}
}
77 changes: 77 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* @author Mygento
* @copyright 2017 Mygento (https://www.mygento.ru)
* @package Mygento_Shipment
*/

namespace Mygento\Shipment\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
const QUOTE_SHIPPING_RATE_TABLE_NAME = 'quote_shipping_rate';

/**
* @param object $installer
* @param object $installer
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function upgrade(
SchemaSetupInterface $setup,
ModuleContextInterface $context
) {
$installer = $setup;

$installer->startSetup();
if (version_compare($context->getVersion(), '2.2.7') < 0) {
$this->updateQuoteShippingRateTable($installer);
}
$installer->endSetup();
}

/**
* Update table 'quote_shipping_rate'
*
* @param object $installer
*/
protected function updateQuoteShippingRateTable($installer)
{
$connection = $installer->getConnection();

$connection->addColumn(
$installer->getTable(self::QUOTE_SHIPPING_RATE_TABLE_NAME),
'estimate',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Delivery estimate date'
]
);
$connection->addColumn(
$installer->getTable(self::QUOTE_SHIPPING_RATE_TABLE_NAME),
'latitude',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Delivery pickpoint latitude'
]
);
$connection->addColumn(
$installer->getTable(self::QUOTE_SHIPPING_RATE_TABLE_NAME),
'longitude',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'comment' => 'Delivery pickpoint longitude'
]
);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mygento/shipment",
"type": "magento2-module",
"version": "2.2.6",
"version": "2.2.7",
"license": "OSL-3.0",
"homepage": "https://github.com/mygento/payment-base",
"description": "Mygento Shipment Base",
Expand Down
9 changes: 9 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Address\Rate">
<plugin name="mygento_shipment_get_rates" type="Mygento\Shipment\Plugin\ExtRate" />
</type>
<type name="Magento\Quote\Model\Cart\ShippingMethodConverter">
<plugin name="mygento_shipment_shipping_method_converter" type="Mygento\Shipment\Plugin\ExtShippingMethodManagement" />
</type>
</config>
8 changes: 8 additions & 0 deletions etc/extension_attributes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config>
<extension_attributes for="Magento\Quote\Api\Data\ShippingMethodInterface">
<attribute code="estimate" type="string" />
<attribute code="latitude" type="string" />
<attribute code="longitude" type="string" />
</extension_attributes>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mygento_Shipment" setup_version="2.2.6"></module>
<module name="Mygento_Shipment" setup_version="2.2.7"></module>
</config>

0 comments on commit 6951570

Please sign in to comment.