如何在我的自定义运输方式的 collectRates() 函数中获取客户和购物车。
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\Checkout\Model\Cart $cart,
\Magento\Customer\Model\Session $customerSession,
array $data = []
) {
$this->_cart = $cart;
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->_customerSession = $customerSession;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
if(!$this->_customerSession->isLoggedIn()) {
return false;
}
$customer = $this->_customerSession->getCustomer();
$qty = $this->_cart->getItemsQty();
...
使用客户会话并检查 isLoggedIn() 是否仅适用于前端,但在管理员下订单时返回 false。
如何正确获得客户并计算前端和管理订单放置的每件商品价格?