我在结帐页面上添加了带有自定义值的下拉自定义字段。它也可以正常工作,也可以将属性值保存在数据库中,但不会显示在订单运送地址中。知道如何显示吗?
InstallSchema.php
$connection->addColumn(
$installer->getTable('quote_address'),
'mob_type',
[
'type' => \Magento\Framework\DB\Ddl\Table ::TYPE_TEXT,
'nullable' => true,
'default' => NULL,
'length' => 255,
'comment' => 'Mob Type'
]
);
$connection->addColumn(
$installer->getTable('sales_order_address'),
'mob_type',
[
'type' => \Magento\Framework\DB\Ddl\Table ::TYPE_TEXT,
'nullable' => true,
'default' => NULL,
'length' => 255,
'comment' => 'Mob Type'
]
);
$installer->endSetup();
插入
use Magento\Checkout\Block\Checkout\LayoutProcessor;
class MobPlugin
{
public function afterProcess(LayoutProcessor $subject, $jsLayout) {
$customAttributeCode = 'mob_type';
$customField = [
'component' => 'Magento_Ui/js/form/element/select',
'config' => [
'customScope' => 'shippingAddress.custom_attributes',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'id' => 'drop-down',
],
'dataScope' => 'shippingAddress.custom_attributes.mob_type',
'label' => 'Mob Type',
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => ['required-entry' => true],
'sortOrder' => 150,
'id' => 'drop-down',
'options' => [
[
'value' => 'local',
'label' => 'Local',
],
[
'value' => 'vip',
'label' => 'VIP',
]
]
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'][$customAttributeCode] = $customField;
return $jsLayout;
}
}
等/ 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\Checkout\Model\ShippingInformationManagement">
<plugin name="save_custom_field" type="Namespace\CustomModule\Plugin\Checkout\SaveAddressInformation" />
</type>
</config>
SaveAddressInformation.php
class SaveAddressInformation
{
protected $quoteRepository;
public function __construct(
\Magento\Quote\Model\QuoteRepository $quoteRepository
) {
$this->quoteRepository = $quoteRepository;
}
/**
* @param \Magento\Checkout\Model\ShippingInformationManagement $subject
* @param $cartId
* @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
*/
public function beforeSaveAddressInformation(
\Magento\Checkout\Model\ShippingInformationManagement $subject,
$cartId,
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
$shippingAddress = $addressInformation->getShippingAddress();
$shippingAddressExtensionAttributes = $shippingAddress->getExtensionAttributes();
if ($shippingAddressExtensionAttributes) {
$customField = $shippingAddressExtensionAttributes->getMobType();
$shippingAddress->setMobType($customField);
}
}
}
上面的插件工作正常,并将值保存在quote_address表中。我也想在订单视图页面和电子邮件模板中显示自定义属性。任何人都知道代码有什么问题。提前致谢