我创建了一种用于送货上门的新送货方式,并且希望能够提供诸如建筑物/单元/房间号之类的选项。我的想法是,当用户选择此方法时,选项将显示一个文本字段,供用户在移至付款页面之前输入信息。关于实现此目标的最佳方法的任何指导?
这是在型号/载体中
/**
* @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 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,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->_logger = $logger;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* @param RateRequest $request
* @return \Magento\Shipping\Model\Rate\Result|bool
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();
$shippingPrice = $this->getConfigData('price');
$method = $this->_rateMethodFactory->create();
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
return $result;
}
/**
* @return array
*/
public function getAllowedMethods()
{
return [$this->_code=> $this->getConfigData('name')];
}
我尝试创建checkout_index_index.xml并使用
向其中添加一个自定义phtml页面<script type="text/javascript">
require([
'jquery',
'Magento_Checkout/js/model/quote',
], function (jQuery, quote) {
jQuery(document).ready(function () {
quote.shippingMethod.subscribe(function (value) {
if (quote.shippingMethod() && quote.shippingMethod().carrier_code == 'your_custom_shipping_method_code') {
var customBlock = "<div class ='custom-information'><input type="text" id="your_custom_id"></div>";
if((!$('.custom-information').length > 0)) {
$('#checkout-shipping-method-load').append(customBlock);
}
});
});
});
});
</script>
然后我添加了模型
namespace Magento\Checkout\Model;
class GuestShippingInformationManagement implements \Magento\Checkout\Api\GuestShippingInformationManagementInterface
{
/**
* @var \Magento\Quote\Model\QuoteIdMaskFactory
*/
protected $quoteIdMaskFactory;
/**
* @var \Magento\Checkout\Api\ShippingInformationManagementInterface
*/
protected $shippingInformationManagement;
/**
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
* @param \Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement
) {
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->shippingInformationManagement = $shippingInformationManagement;
}
/**
* {@inheritDoc}
*/
public function saveAddressInformation(
$cartId,
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
return $this->shippingInformationManagement->saveAddressInformation(
$quoteIdMask->getQuoteId(),
$addressInformation
);
}
}
并开始对其进行修改,但我认为我可能会走错方向,
对实现此目标的最佳方法有何想法?
谢谢!