Skip to main content
Tweeted twitter.com/StackMagento/status/1184665347351298050
Became Hot Network Question
Typo correction
Source Link
Sumit
  • 5k
  • 2
  • 20
  • 35

I am new to magentoMagento development and iI am using a custom mysqlMySQL query in some places like in observer/plugin etc.

These codes are repeating for all these files. so iI want to make a new re usablereusable function/class and want to call this method instead of writing the same thing in all files.

I don't know even to create controller and all, could you please help me to create this?

                $modelMaterials = $objectManager->create('Vendor\Module\Model\Grid');
                $dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
                if($dataMaterials->getSize()){
                     $materialsarr = $dataMaterials->getFirstItem();
                     $weight = $subject->getWeight();
                     $fineWeight = $data->getFineWeight();
                     $metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $metalPrice += (double)$data->getSurcharge();
                     $metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $price = (double)$materialsarr->getAmount() * (double)$weight;
                     $tx_rates = (double)$materialsarr->getTax() / 100;
                     $tax = $tx_rates * (double)$metalPrice;
                     $specialPrice = $subject->getSpecialPrice();
                }      

I am new to magento development and i am using a custom mysql query in some places like in observer/plugin etc.

These codes are repeating for all these files. so i want to make a new re usable function/class and want to call this method instead of writing the same thing in all files.

I don't know even to create controller and all, could you please help me to create this?

                $modelMaterials = $objectManager->create('Vendor\Module\Model\Grid');
                $dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
                if($dataMaterials->getSize()){
                     $materialsarr = $dataMaterials->getFirstItem();
                     $weight = $subject->getWeight();
                     $fineWeight = $data->getFineWeight();
                     $metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $metalPrice += (double)$data->getSurcharge();
                     $metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $price = (double)$materialsarr->getAmount() * (double)$weight;
                     $tx_rates = (double)$materialsarr->getTax() / 100;
                     $tax = $tx_rates * (double)$metalPrice;
                     $specialPrice = $subject->getSpecialPrice();
                }      

I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.

These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.

I don't know even to create controller and all, could you please help me to create this?

                $modelMaterials = $objectManager->create('Vendor\Module\Model\Grid');
                $dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
                if($dataMaterials->getSize()){
                     $materialsarr = $dataMaterials->getFirstItem();
                     $weight = $subject->getWeight();
                     $fineWeight = $data->getFineWeight();
                     $metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $metalPrice += (double)$data->getSurcharge();
                     $metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
                     $price = (double)$materialsarr->getAmount() * (double)$weight;
                     $tx_rates = (double)$materialsarr->getTax() / 100;
                     $tax = $tx_rates * (double)$metalPrice;
                     $specialPrice = $subject->getSpecialPrice();
                }      
update corrections
Link
anonymous
  • 3.8k
  • 4
  • 25
  • 67

Magneto 2 How to call Helper function call in observer file

edited tags; edited title
Link
Rakesh Donga
  • 5.4k
  • 2
  • 25
  • 57

Magneto 2 How to create a commonHelper function for my magento module?call in observer file

Source Link
Sanjun Dev
  • 389
  • 2
  • 13
Loading