【问题标题】:Custom Shipping Method - Retrieve Shipping address in collectRates自定义送货方式 - 在 collectRates 中检索送货地址
【发布时间】:2019-02-22 13:13:04
【问题描述】:

我目前正在研究 Magento 2 的自定义送货方式,但遇到了一个问题:我不知道如何检索送货地址。

我首先在collectRates() 方法中尝试了类似的方法:

$city = $request->getDestCity();
$street = $request->getDestStreet();
$postCode = $request->getDestPostcode();

但它不起作用($city 和 $street 为空)。

这很奇怪,因为我对 getDestPostcode() 方法没有任何问题。

还有其他方法可以检索送货地址吗?

谢谢。

【问题讨论】:

  • 哼..没人知道吗?

标签: php magento2


【解决方案1】:

你试过这样吗?

$shippingId =  $customerSession->getCustomer()->getDefaultShipping();
$address = $objectManager->create('Magento\Customer\Model\Address')-     >load($shippingId);
echo "<pre>";print_r($address->getData());

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    根据帕拉维的回答,但更详细。

    /**
     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
     */
    protected $customerSession;
    
    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
     * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Framework\App\ObjectManager $objectManager
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->rateResultFactory = $rateResultFactory;
        $this->rateMethodFactory = $rateMethodFactory;
        $this->request = $request;
        $this->customerSession = $customerSession;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }
    
    // ...
    
    /**
     * @param RateRequest $request
     * @return bool|Result
     */
    public function collectRates(RateRequest $request)
    {
        if (!$this->getActiveFlag()) {
            // This shipping method is disabled in the configuration
            return false;
        }
    
    
        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->rateResultFactory->create();
    
        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->rateMethodFactory->create();
    
        $method->setCarrier(self::SHIPPING_CODE);
        // Get the title from the configuration, as defined in system.xml
        $method->setCarrierTitle($this->getConfigData('title'));
    
        $method->setMethod(self::SHIPPING_CODE);
        // Get the title from the configuration, as defined in system.xml
        $method->setMethodTitle($this->getConfigData('name'));
    
        // Get the price from the configuration, as defined in system.xml
        $amount = $this->getConfigData('price');
    
        // Get the customer session and shipping address as specified.
        $address =  $this->customerSession->getCustomer()->getAddressById();
        // Note: shippingId will be null on guest checkouts.
        // Request to estimate-shipping-methods-by-address-id will send it.
    
    
         file_put_contents('mydebug.json', '--------------------------\nRequest: \n'.json_encode($request->getData(), JSON_PRETTY_PRINT).PHP_EOL, FILE_APPEND);
         file_put_contents('mydebug.json', '--------------------------\nCustomer Address: \n'.json_encode($address->getData(), JSON_PRETTY_PRINT).PHP_EOL, FILE_APPEND);
    
    
        $method->setPrice($amount);
        $method->setCost($amount);
    
        $result->append($method);
    
        return $result;
    
    }
    

    更改构造函数依赖项/参数后,您需要运行 di compile。

    bin/magento setup:di:compile
    

    注意事项:

    如果使用这些,还需要 JS mixin 文件来发送任何自定义属性。

    getDefaultShipping() 在客人结账时返回 null。

    我最后也检查了 URL。

        // Get the data from post input via php://input
        $request_body = file_get_contents('php://input');
        $postData = json_decode($request_body, true);
    
    
    if (preg_match("/^\/rest\/default\/V1\/carts\/mine\/estimate-shipping-methods-by-address-id/", $_SERVER["REQUEST_URI"])) {
                $address =  $this->customerSession->getCustomer()->getAddressById($postData['addressId']);
                if ($address) {
                    file_put_contents('liamsdebug.json', '--------------------------\nCustomer Address: \n'.json_encode($address->getData(), JSON_PRETTY_PRINT).PHP_EOL, FILE_APPEND);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多