Get all invoices in Magento

2019-01-25 00:11发布

问题:

How do I get all the invoices collection?

I know that to get all the orders is:

 $orders = Mage::getModel(sales/order)->getCollection();

However I can't seem to find the equivalent with invoice (it's not sales/invoice). Sorry if it's a dumb question.

回答1:

You need to use :

$orders = Mage::getModel("sales/order_invoice")->getCollection();

For the explanation :

When you want to access i block/model/resource/helper/etc in magento :

  • first, you choose the right method to access it : Mage::getModel for a model

  • second, you must tell magento "which" module you want to access. You do that with the first part of the parameter string :

Mage::getModel("sales/order_invoice")

This string refers to the XML node of the type or resource you want to access. In your case, for accessing a model of the Mage_Sales module : take a look at the Mage_Sales's config.xml, you'll see the node to use (used for models and resourceModels) :

[...]
<models>
    <sales><!-- <----- THIS NODE -->
        <class>Mage_Sales_Model</class>
         <resourceModel>sales_resource</resourceModel>
     </sales>
     [...]
  • last part, you need to add the full access to the file you want, with folder if needed. In your case, in the Model folder of the Mage_Sales module (above xml config tells us that this folder is Mage_Sales_Model), you'll see a file Invoice.php in the Order folder : then you path is "order_invoice" and the full commande to access your model is :

Mage::getModel("sales/order_invoice")