i'm doing custom checkout with my module, this module create 2 input in first step (shipping address step) and this first step is working fine, but in the 2nd step, it throw error with status code 500, lets take a look at the description image below :
First step:
First step working fine with status code 200 and sending extension attribute
.
But in the second step, when i click at "Place Order", it return status code 500 with this error message:
So here is my code:
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\etc\di.xml
<?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\ShippingAddressManagement">
<plugin name="custom_quote_delivery" type="Aht\MagentoCheckoutCustom\Plugin\Quote\Model\ShippingAddressManagement" sortOrder="1" disabled="false"/>
</type>
</config>
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\etc\extension_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
<attribute code="delivery_instruction" type="string"/>
<attribute code="delivery_type" type="string"/>
</extension_attributes>
</config>
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\Plugin\Quote\Model\ShippingAddressManagement.php
<?php
namespace Aht\MagentoCheckoutCustom\Plugin\Quote\Model;
class ShippingAddressManagement
{
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger
) {
$this->logger = $logger;
}
public function beforeAssign(
\Magento\Quote\Model\ShippingAddressManagement $subject,
$cartId,
\Magento\Quote\Api\Data\AddressInterface $address
) {
// try {
$extAttributes = $address->getExtensionAttributes();
$deliveryInstruction = $extAttributes->getDeliveryInstruction();
$deliveryType = $extAttributes->getDeliveryType();
$address->setDeliveryInstruction($deliveryInstruction);
$address->setDeliveryType($deliveryType);
// } catch (\Exception $e) {
// $this->logger->critical($e->getMessage());
// }
}
}
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\Setup\UpgradeSchema.php
<?php
namespace Aht\MagentoCheckoutCustom\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '1.1.0', '<')) {
if ($installer->tableExists('quote')) {
$connection = $installer->getConnection();
$tableName = $installer->getTable('quote');
$connection->addColumn(
$tableName,
'delivery_instruction',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'nullable' => false,
'comment' => 'Delivery Instruction'
]
);
$connection->addColumn(
$tableName,
'delivery_type',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'nullable' => false,
'comment' => 'Delivery Type'
]
);
}
}
$installer->endSetup();
}
}
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\view\frontend\requirejs-config.js
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
var config = {
config: {
mixins: {
'Magento_Checkout/js/action/set-shipping-information': {
'Aht_MagentoCheckoutCustom/js/action/set-shipping-information': true
}
}
}
};
C:\xampp\htdocs\magento\app\code\Aht\MagentoCheckoutCustom\view\frontend\web\js\action\set-shipping-information.js
define([
'jquery',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
'use strict';
return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction) {
var shippingAddress = quote.shippingAddress();
if (shippingAddress['extension_attributes'] === undefined) {
shippingAddress['extension_attributes'] = {};
}
shippingAddress['extension_attributes']['delivery_instruction'] = $('[name="delivery_instruction"]').val();
shippingAddress['extension_attributes']['delivery_type'] = $('[name="delivery_type"]').val();
// pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
return originalAction();
});
};
});
Ok, so i've success created 2 column in "Quote
" table named "delivery_instruction
" and "delivery_type
".
And i've follow a lot of source and topic (devdoc, stackexchange, github) but i thing i saw 2 ways to do this.. the sohelrana09
way on github is using another way as what i did.
Here is sohelrana09
way:
https://github.com/sohelrana09/magento2-module-delivery-date/tree/master/SR/DeliveryDate
And the way i'm following to is from here and the devdoc:
Save a custom attribute from shipping method to quote_address
https://devdocs.magento.com/guides/v2.2/howdoi/checkout/checkout_new_field.html
I've also found another way convert 2 way somehow .. but doesn't have example
Save Custom field in shipping address at Checkout in Magento 2
Okay so i dont know which one is correct, and if both correct, which one is better, Thanks for reading, Have a good day :)