1

I created a new shipping method for home delivery and I want to be able to present options such as a building/unit/room number. My thought was when the user selected this method the options would appear with a text field for the user to enter information before moving to the payment page. Any guidance on the best way to make this happen?

This is in Model/Carrier

/**
     * @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')];
    }

I tried to create checkout_index_index.xml and add a custom phtml page to it with

<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>

And then I added in Model

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

and started to modify it but I think I may be headed in the wrong direction,

Thoughts on the best way to accomplish this?

Thanks!

stanhook
  • 63
  • 3
  • 12
  • For example, I want to show user a list of locations to choose from when the shipping method is chosen and I want to collect some information from user like the unit number. – stanhook Jul 19 '19 at 21:54
  • Revision - I want to show user a list of locations that are added in the admin for the user to choose from when the shipping method is chosen and I want to collect some information from user like the unit number. – stanhook Jul 19 '19 at 22:06
  • Well formed question, I can't understand why no one upvoted until now. Any progress here? – mtr.web Nov 05 '20 at 19:37

0 Answers0