I have my own website in Magento2 and also use my shipping method I have created below function in Model\Service\AbstractService.php

     * Convert weight to kilogram
     * @param $weight
     * @param $weightUnit
     * @param int $roundPrecision
     * @return float
    public function convertWeightToKg($weight, $weightUnit, $roundPrecision = 3)
        return round(

Now I have created new function like Convert weight to grams

so what to do for this function. give me solution for this.

  • 1
    what you want? Question is NOT clear.Please explain what you want to achieve? – Amit Bera Apr 2 at 13:15
  • I want created new function like Convert weight to grams above is the example ok – MehulKanjariya Apr 2 at 13:50

public function convertWeightToKg($weight, $weightUnit, $roundPrecision = 3)


switch ($weightUnit) {

case 'LBS':
    return $finalWeight;

case 'KILOGRAM':
    return $finalWeight;

    return $weight;

} }

  • let me know if any issues – mohith Apr 2 at 17:21
  • This is basic structure you can modifiy as per your need – mohith Apr 2 at 17:23
  • what is case for gram weight – MehulKanjariya Apr 4 at 20:22
  • default will work – mohith Apr 5 at 4:18
  • kindly check this and update if any issues – mohith Apr 5 at 4:19

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.