根据帕拉维的回答,但更详细。
/**
* @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);
}
}