Magento 2 - 在结帐页面,城市ID显示在送货地址而不是城市名称

问题描述 投票:0回答:1

我在结帐页面上放了一个城市下拉菜单。 (见下图)

city-dropdown

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

因此,此城市值将被保存为数据库中的城市属性ID,并且正在获取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>

.... \插件\结帐\型号\结帐\ 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;
}

但是我在结帐页面上收到了addressId = null的错误。那是因为我的客户addressId返回一个空值。所以我在解决这个方面遇到了麻烦。如果有人能解决这个问题,我真的很感激。

提前致谢。

magento2 dropdown checkout city shipping-method
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.