0

I have created a custom shipping method following this tutorial:

https://www.mageplaza.com/devdocs/magento-2-create-shipping-method/

Now I would need to add a couple of extra variables to admin configuration, and save it in any place to use it later when the checkout is done. How can I do that? Thanks!

3
  • Do you want to add some configuration In STORES > Configuration -> SALES section -> Shipping Methods ?? Commented Oct 11, 2018 at 18:42
  • hey @AdityaShah. Yes I would want that. Really I need to save a couple of config data to use it later in the checkout process, I think this is a good place, but it also could be in other place. Commented Oct 11, 2018 at 18:49

1 Answer 1

0

You can add more field like below in your system.xml under group tag with id="simpleshipping" in above module-

<field id="custom_variable" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
      <label>Custom Variable</label>
</field>

Now you can retrieve value of this field in any file like block or model or controller like this --

protected $_scopeConfig;

public function __construct(
     ...
     \Magento\Store\Model\ScopeInterface $scopeInterface,
     ...
)
{
    ...
    $this->_scopeConfig = $scopeInterface;
    ...
}

Then in your class' method you can call the following:

$this->scopeConfig->getValue('carriers/simpleshipping/custom_variable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
2
  • Thanks, @Shashank, let me try this option. Commented Oct 11, 2018 at 18:50
  • 1
    ok @DanielSilva , let me know in case of any error Commented Oct 11, 2018 at 18:51

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.