1

I am making a magento 2 module, and I had to create a custom shipping method but I am not able to get the data of fields in the checkout page :

This is a part of my system.xml file

 <section id="carriers" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
 <group id="my_shipping_methode" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
 <field id="myfield" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">

and this is the constructor of my module :

<?php namespace A\B\Model\Carrier; 
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;
class Shipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
 \Magento\Shipping\Model\Carrier\CarrierInterface
{
 /**
 * @var string
 */
 protected $_code = 'my_shipping_methode';
 /**
 * @var string
 */
 protected $myfield = 'test';

And this is part of my javascript file I call in the checkout to get the code of the shipping method :

this.selectedMethod = ko.computed(function() {
 var method = quote.shippingMethod();
 var selectedMethod = method != null ? method.method_code : null;

I tried to do the same for my field but it didn't work

var myfield = method != null ? method.method_myfield : null;

Please help I am lost

asked Apr 1, 2019 at 22:35

2 Answers 2

0

Your protected code should be equal to the name of your system.xml group configuration. Instead of protected $_code = 'thecode';, It should be:

protected $_code = 'my_shipping_methode'; 
answered Apr 2, 2019 at 0:26
7
  • Actually I changed the values of my params just to give the example of the situation i am into, and forgot about that , but still it's not really the problem as it's coorect in my code. I fixed that in my question thanks for pointing that. Commented Apr 2, 2019 at 7:58
  • great. but why you need a javascript? , I think using the class Shipping Magento\Quote\Model\Quote\Address\RateRequest your shipping method will automatically be added in checkout Commented Apr 2, 2019 at 8:05
  • Actually the shipping is added correctly and I can see it but The problem is I want to get the value of one of its fields I added my self (myfield) and send it to javascript to do some stuf with it in the checkout page Commented Apr 2, 2019 at 8:11
  • what is your goal for this? what is your expected output? Commented Apr 2, 2019 at 8:13
  • 1
    Ok thanks I will keep looking for a workaround I am already lost so I don't want to do it overriding things I am new to magento so I don't think that I will be able to do it anyway for now. Commented Apr 2, 2019 at 8:27
0

Solved !!

This is how I did it :

<?php
namespace Mageplaza\Simpleshipping\Model;
use Magento\Checkout\Model\ConfigProviderInterface;
/**
 * Class SampleConfigProvider
 */
class SampleConfigProvider extends \Magento\Framework\App\Helper\AbstractHelper implements ConfigProviderInterface
{
 /**
 * Retrieve assoc array of checkout configuration
 *
 * @return array
 */
 public function getConfig()
 {
 $postTest = $this->scopeConfig->getValue(
 'carriers/simpleshipping/myfield',
 \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
 return [
 'foo' => [
 'bar' => $postTest,
 ],
 ];
 }
}

In my javascript :

getSampleTotal: function () {;
 return window.checkoutConfig.foo.bar;
 }

In my template :

<div class="component-wrapper">
 <p data-bind="html: getSampleTotal()"></p>
 </div>

in 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\Model\CompositeConfigProvider">
 <arguments>
 <argument name="configProviders" xsi:type="array">
 <item name="Mageplaza_Simpleshipping_config_provider" xsi:type="object">Mageplaza\Simpleshipping\Model\SampleConfigProvider</item>
 </argument>
 </arguments>
 </type>
</config>

system.xml

 <section id="carriers" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
 <group id="simpleshipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
 <field id="myfield" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
answered Apr 2, 2019 at 10:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.