1

I'm trying to extend shipping module of magento. I need to set shiping rate based on region.

The problem is I cannot find my shipping modules in admin panel in Stores > Configuration and it's not applied to storefront

here is system.xml:

<?xml version="1.0"?><config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="vendor_id" translate="label" sortOrder="10">
        <label>Vendor_Name</label>
    </tab>
    <section id="zones_shipping" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>Zones Fixed Shipping</label>
        <tab>vendor_id</tab>
        <resource>Vendor_Module::moduleName</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>General Configuration</label>
            <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Enabled</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Fixed Shipping</label>
            </field>
            <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Module Enable</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Display Text</label>
                <comment>This text will display on the frontend.</comment>
            </field>
            <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Title</label>
            </field>
            <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Method Name</label>
            </field>
            <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Price</label>
                <validate>validate-number validate-zero-or-greater</validate>
            </field>
            <field id="handling_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Calculate Handling Fee</label>
                <source_model>Magento\Shipping\Model\Source\HandlingType</source_model>
            </field>
            <field id="handling_fee" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Handling Fee</label>
                <validate>validate-number validate-zero-or-greater</validate>
            </field>
            <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Sort Order</label>
            </field>
            <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Ship to Applicable Countries</label>
                <frontend_class>shipping-applicable-country</frontend_class>
                <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model>
            </field>
            <field id="specificcountry" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Ship to Specific Countries</label>
                <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                <can_be_empty>1</can_be_empty>
            </field>
            <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Show Method if Not Applicable</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Displayed Error Message</label>
            </field>
        </group>
    </section>
</system>

here is config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
  <carriers>

    <Vendor_ModuleName>

      <active>1</active>
      <sallowspecific>0</sallowspecific>

      <model>\Vendor\ModuleName\Model\Carrier\FixedShipping</model>

      <name>Fixed Shipping</name>

      <title>Zoned fixed shipping</title>



    </Vendor_ModuleName>

  </carriers>

</default>

here is my model:

class FixedShipping extends AbstractCarrier implements CarrierInterface
{
/**
 * @var string
 */
protected $_code = 'vendoe_moduleName';

/**
 * @var bool
 */
protected $_isFixed = true;

/**
 * @var \Magento\Shipping\Model\Rate\ResultFactory
 */
protected $_rateResultFactory;

/**
 * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
 */
protected $_rateMethodFactory;

/**
 * @var \Magento\Customer\Model\Session
 */
protected $customerSession;


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,
\Magento\Customer\Model\Session $customerSession,
array $data = []){

    $this->_rateResultFactory = $rateResultFactory;
    $this->_rateMethodFactory = $rateMethodFactory;
    $this->customerSession = $customerSession;
    parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}

/**
* @return array
*/
public function getAllowedMethods(){

    return [$this->getCarrierCode() => __($this->getConfigData('name'))];
}

public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $request){

    if (!$this->isActive()){

        return false;
    }



    $result->append($method);
    return $result;*/

    $item = current($request->getAllItems());
    $quote = null;
    if ($item instanceof QuoteItem){
        $quote = $item->getQuote();
    }
    if($quote){
        $shippingAddress = $quote->getShippingAddress();
        if($shippingAddress['region']['region_code'] == 'Cairo'){
            $shippingPrice = 20;
            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);
        }
        elseif($shippingAddress['region']['region_code'] == 'Alexandria'){
            $shippingPrice = 34;
            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);
        }
        //else{
            $shippingPrice = 1600;
            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);
        //}

        $method = $this->_rateMethodFactory->create();

        $method->setCarrier($_code);
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod($this->getCarrierCode());
        $method->setMethodTitle($this->getConfigData('name'));
        $result->append($method);
        return $result;
    }
    return false;
}}
1

Change your system.xml by following code snippet:

<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="vendor_id" translate="label" sortOrder="10">
            <label>Vendor_Name</label>
        </tab>
        <section id="zones_shipping" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Zones Fixed Shipping</label>
            <tab>vendor_id</tab>
            <resource>Vendor_Module::moduleName</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Fixed Shipping</label>
                </field>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Display Text</label>
                    <comment>This text will display on the frontend.</comment>
                </field>
                <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                </field>
                <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Price</label>
                    <validate>validate-number validate-zero-or-greater</validate>
                </field>
                <field id="handling_type" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Calculate Handling Fee</label>
                    <source_model>Magento\Shipping\Model\Source\HandlingType</source_model>
                </field>
                <field id="handling_fee" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Handling Fee</label>
                    <validate>validate-number validate-zero-or-greater</validate>
                </field>
                <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Sort Order</label>
                </field>
                <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Ship to Applicable Countries</label>
                    <frontend_class>shipping-applicable-country</frontend_class>
                    <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model>
                </field>
                <field id="specificcountry" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Ship to Specific Countries</label>
                    <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                    <can_be_empty>1</can_be_empty>
                </field>
                <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Show Method if Not Applicable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Displayed Error Message</label>
                </field>
            </group>
        </section>
    </system>
</config>

settings

  • it doesn't work – Yomna Mansour Feb 27 at 14:14
  • Did you copy the code from my answer? copy again and check the screenshot. – Sohel Rana Feb 27 at 14:18
  • Also remove line between <?xml version="1.0"?> and <config xmlns:xsi=" that may be cause for you – Sohel Rana Feb 27 at 14:21
  • Yes, that what I did – Yomna Mansour Feb 27 at 14:23
  • you definitely made a mistake then, check carefully – Sohel Rana Feb 27 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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