0

I want to display minimum order amount in custom shipping method in checkout and cart page.

These are the exact files i use for it. The minimum order amount is stored in core_config_data. How to do it.Which files i have to edit for that? can you show me an example code.

app\code\Magento\Checkout\view\frontend\web\template\shipping.html

app\code\Magento\Checkout\view\frontend\web\template\cart\shipping-rates.html

anybody pls help.

1 Answer 1

0

It will not be so easy. I am assuming that you have already created the skeleton for your module.

First, you have to create a processor to set up the value from the config (Vendor/Module/etc/frontend/di.xml)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="custom_processor_name" xsi:type="object">Vendor\Module\Block\Checkout\ShippingProcessor</item>
            </argument>
        </arguments>
    </type>
</config>

Secondly, you have to create the processor class (Vendor\Module\Block\Checkout\ShippingProcessor.php):

<?php

declare(strict_types=1);

namespace Vendor\Module\Block\Checkout;

use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Vendor\Module\Model\Config;
use Magento\Framework\Stdlib\ArrayManager;

class ShippingProcessor implements LayoutProcessorInterface
{
    /**
     * @var ArrayManager
     */
    private $arrayManager;

    /**
     * @var Config
     */
    private $config;

    /**
     * @param Config $config
     * @param ArrayManager $arrayManager
     */
    public function __construct(
        Config $config,
        ArrayManager $arrayManager
    ) {
        $this->config = $config;
        $this->arrayManager = $arrayManager;
    }

    /**
     * @param array $jsLayout
     * @return array
     */
    public function process($jsLayout): array
    {
        // this path you can verify on checkout_index_index.xml file
        $path = 'components/checkout/children/steps/children/shipping-step/children/shippingAddress';
        if ($this->arrayManager->get($path, $jsLayout)) {

            $jsLayout = $this->arrayManager->merge(
                $path,
                $jsLayout,
                [
                      'config' => ['template' => 'Vendor_Module/shipping'], 
                      'customValue' => $config->getCustomValue()
                ]
            );
        }

        return $jsLayout;
    }
}

Thirdly, you must create the class responsible for loading the config

<?php

declare(strict_types=1);

namespace Vendor\Module\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
    private const XML_PATH_CUSTOM_VALUE = 'vendor_module/section/custom_value';

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(ScopeConfigInterface $scopeConfig)
    {
        $this->scopeConfig = $scopeConfig;
    }


    /**
     * @return string
     */
    public function getCustomValue(): string
    {
        return $this->scopeConfig->getValue(self::XML_PATH_CUSTOM_VALUE);
    }
}

If you see on processor class I included a new path for the template, that will overwrite the default path of the shipping component Magento_Checkout/shipping by Vendor_Module/shipping, that is your HTML file. You must put it on Vendor/Module/view/frontend/web/template/shipping.html

Finally, you can copy and past the content of the file: vendor/magento/module-checkout/view/frontend/web/template/shipping.html to your file and then include the content from customValue variable by adding this:

<div data-bind="html: customValue"></div>

Please let me know if it helps you of if you need something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.