Magento 2-在“结帐”页面上,城市ID显示在收货地址中而不是城市名称中

时间:2019-02-15 06:34:53

标签: magento2 dropdown checkout city shipping-method

我在结帐页面上输入了城市下拉列表。 (请参见下图)

city-dropdown

这些城市是从数据库中提取的。

因此,此城市值将作为id保存在数据库中的city属性中,并且该id将被获取并显示在送货地址中,而不是城市名称中。 (请参见下图)。

shipping-address-with-city

这是我实施城市下拉菜单的方式:

.... \\ etc \ di.xml

<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="talliance_layout_checkout" type="Talliance\SwyftShippingMethod\Plugin\Checkout\Model\Checkout\LayoutProcessor" sortOrder="100"/>
   </type>
</config>

.... \ Plugin \ Checkout \ Model \ Checkout \ LayoutProcessor.php

class LayoutProcessor extends CheckoutLayoutProcessor
{
    protected $_cities;

    public function __construct
    (
   \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider,
   \Magento\Ui\Component\Form\AttributeMapper $attributeMapper,
   AttributeMerger $merger,
   City $cities
   )
   {
   $this->_cities = $cities;
   parent::__construct($attributeMetadataDataProvider, $attributeMapper, $merger);
}

/**
 * @param CheckoutLayoutProcessor $subject
 * @param array $jsLayout
 * @return array
 */
public function afterProcess(
    CheckoutLayoutProcessor $subject,
    array  $jsLayout
) {

    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
    ['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = [
        'component' => 'Magento_Ui/js/form/element/select',
        'config' => [
            'customScope' => 'shippingAddress',
            'template' => 'ui/form/field',
            'elementTmpl' => 'ui/form/element/select',
            'id' => 'city'
        ],
        'dataScope' => 'shippingAddress.city',
        'label' => 'City',
        'provider' => 'checkoutProvider',
        'visible' => true,
        'validation' => [ 'required-entry' => true ],
        'sortOrder' => 250,
        'id' => 'city',
        'options' => $this->_cities->toOptionArray()
    ];
    return $jsLayout;
  }
}

我想在结帐时在送货地址中显示城市名称。有谁知道该怎么做?

下面的代码是我尝试使用\ Magento \ Customer \ Api \ Data \ AddressInterface设置和更新特定客户的城市名称而不是城市ID的方法。

public function getCustomerCity() {
    $customerCity = '';
    $checkout = $this->_sessionCheckout->getQuote();
    if ($checkout) {
        $shippingAddress = $checkout->getShippingAddress();
        if ($shippingAddress) {

            //Get customer addressId (this variable returns null when I debug).
            $addressId = $shippingAddress->getCustomerAddressId();
            $customerCity = $shippingAddress->getCity();
            if($customerCity) {
                if (is_numeric($customerCity)) {

                    /** @var \Magento\Customer\Api\Data\AddressInterface $address */
                    $address = $this->_addressRepository->getById($addressId);

                    /** @var \Talliance\SwyftShippingMethod\Model\ResourceModel\SrilankanCity\Collection $collection */
                    $collection = $this->_cityCollection->create();
                    //Get city name from the city id
                    $city_name = $collection->getCityName($customerCity);
                    $address->setCity($city_name);
                    $this->_addressRepository->save($address);
                    $customerCity = trim($city_name);
                }
                $this->_logger->log(100, print_r('customer city: ' . $customerCity, true));
            }
        }
    }
    return $customerCity;
}

但是我在结帐页面上收到一个错误,地址为null。那是因为我的客户addressId是返回一个空值。所以我在解决这个问题上遇到了麻烦。如果有人可以解决此问题,我将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

我在here回答了类似的问题。请查看,它可能会对您有所帮助。

相关问题