Magento 2自定义地址属性未加载到结帐页面的通讯簿中

时间:2018-07-14 02:36:25

标签: php magento magento2 magento2.2.3

我创建了一个Magento模块到2.2.4和2.2.3版本,并在“送货地址”和“客户地址”中添加了自定义属性。当在结帐页面中创建新地址并显示我的自定义属性并保存在订单地址和报价地址中时,该属性工作正常。

问题是,当从通讯录中加载地址时,我的自定义属性无法加载,并且在送货地址中是必需的,我无法付款

在“客户”>“ Magento管理员的地址”中编辑自定义属性时,未保存。我在客户地址的自定义属性中手动将值设置到数据库中,但未在结帐页面中加载

因此,custom属性仅在写入新地址时起作用,而不能仅按顺序和地址引用保存在地址簿中,因此我在数据库中手动设置值,但不起作用,并且我需要在输入以下内容时将其加载到保存的地址中结帐页面。

结帐页面示例

enter image description here

请帮助我。

DD \ Checkoutaddress \ 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\Block\Checkout\LayoutProcessor">
        <plugin name="add_custom_field_checkout_form" type="DD\Checkoutaddress\Model\Plugin\Checkout\LayoutProcessor" sortOrder="100"/>
    </type>

    <type name="Magento\Checkout\Model\ShippingInformationManagement">
        <plugin name="save_custom_field" type="DD\Checkoutaddress\Model\Plugin\Checkout\SaveAddressInformation" />
    </type>
</config>

DD \ Checkoutaddress \ 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="mobilenumber" type="string" />
    </extension_attributes>

    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="mobilenumber" type="string" />
    </extension_attributes>
</config>

DD \ Checkoutaddress \ Model \ Plugin \ Checkout \ LayoutProcessor.php

<?php
namespace DD\Checkoutaddress\Model\Plugin\Checkout;

class LayoutProcessor
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    ) {
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['mobilenumber'] = [
            'component' => 'Magento_Ui/js/form/element/abstract',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/input',
                'options' => [],
                'id' => 'custom-field'
            ],
            'dataScope' => 'shippingAddress.custom_attributes.mobilenumber',
            'label' => 'Mobile Number',
            'required' => true,
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => ['required-entry' => true],
            'sortOrder' => 69,
            'id' => 'mobilenumber'
        ]; 

        return $jsLayout;
    }
}

DD \ Checkoutaddress \ Model \ Plugin \ Checkout \ SaveAddressInformation.php

<?php 
namespace DD\Checkoutaddress\Model\Plugin\Checkout;

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) {
            $mobilenumber = $shippingAddressExtensionAttributes->getmobilenumber();

            $shippingAddress->setMobilenumber($mobilenumber);
        }
    }
}

DD \ Checkoutaddress \ Setup \ CustomerSetup.php

<?php

namespace DD\Checkoutaddress\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {

    } 

    public function installCustomerAddressAttributes($customerSetup) {


        $customerSetup -> addAttribute('customer_address',
            'mobilenumber',
            [
            'label' => 'Mobile Phone',
            'system' => 0,
            'user_defined' => true,
            'position' => 101,
            'sort_order' =>101,
            'visible' =>  true,
            'default_value' => '',
            'note' => '',
                        'type' => 'varchar',
                        'input' => 'text',
            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer_address', 'mobilenumber')->setData('is_user_defined',1)->setData('default_value','')-> setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit']) -> save();
    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 
} 

DD \ Checkoutaddress \ Setup \ InstallData.php

<?php

namespace DD\Checkoutaddress\Setup;

use Magento\Eav\Setup\EavSetup;
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.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerSetup = $objectManager->create('DD\Checkoutaddress\Setup\CustomerSetup');
                $customerSetup->installAttributes($customerSetup);


                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $salesSetup = $objectManager->create('Magento\Sales\Setup\SalesSetup');
                $salesSetup->addAttribute('order_address', 'mobilenumber', ['type' =>'varchar']);

                $quoteSetup = $objectManager->create('Magento\Quote\Setup\QuoteSetup');                
                $quoteSetup->addAttribute('quote_address', 'mobilenumber', ['type' =>'varchar']);
        }

    }
}

require-config.js

var config = {
    config: {
        mixins: {
            'Magento_Checkout/js/action/set-shipping-information': {
                'DD_Checkoutaddress/js/action/set-shipping-infomation-mixin': true
            }
        }
    }
};

set-shipping-infomation-mixin.js

/*jshint browser:true jquery:true*/
/*global alert*/
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']['mobilenumber'] = shippingAddress.customAttributes['mobilenumber'];
            // pass execution to original action ('Magento_Checkout/js/action/set-shipping-information')
            return originalAction();
        });
    };
});

1 个答案:

答案 0 :(得分:0)

数据库中的客户表在保存书籍地址,运输地址和帐单地址方面有所不同,为此,您需要在Installation.php中添加以下内容

$eavSetup->addAttribute('customer_address', 'phonecode', 
[
'type' => 'varchar',
'input' => 'text',
'label' => 'phonecode',
'visible' => true,
'required' => false,
'user_defined' => true,
'system'=> false,
'group'=> 'General',
'global' => true,
'visible_on_front' => true,
]);
$customAttribute = $this->eavConfig->getAttribute('customer_address', 
'phonecode');
$customAttribute->setData(
'used_in_forms',
['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
);

它将在'eav_attribute'表中添加不同的属性以保存不同的地址,然后您可以使用自定义字段提交表单,数据将保存在'customer_address_entity_varchar'中。