自定义送货方式 - 在collectRates中检索送货地址

时间:2016-02-25 12:31:51

标签: php magento2

我目前正在为Magento 2制定自定义送货方式,我遇到了一个问题:我不知道如何找回送货地址。

我首先尝试使用collectRates()方法中的类似内容:

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

但它没有用($city$street是空的。)

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

是否有其他方法可以检索送货地址?

感谢。

2 个答案:

答案 0 :(得分:1)

你试过这样的吗?

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

希望这有帮助!

答案 1 :(得分:0)

按照帕拉维的回答,但有更多细节。

/**
 * @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编译。

bin/magento setup:di:compile

注意:

需要使用JS mixin文件来发送任何自定义属性(如果使用它们)。

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

我最终也检查了网址。

    // 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);
            }
        }