0

im trying to gett all products and also show the categories of each products from complete orders, but is giving me a strange error:

Fatal error: Call to a member function getId() on boolean in /home/site/public_html/csv/getAllOrders.php on line 28

Everything looks ok, i have no idea what is wrong, i already tried to get the categories by loading product id, or sku, but same error, cant figure out what is wrong. Here is my code:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));

    foreach ($orders as  $order) {

        foreach ($order->getAllItems() as $item) {

            echo $item->getName() ." - ";
            echo $item->getSku();

            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$item->getSku());

            if($product->getId()){
                echo implode(",", $product->getCategoryIds());
            }
            echo "<br>";
        }
        echo "<br>*********************<br>";


    }
1
  • can you get your answer??? Jan 18, 2018 at 3:11

3 Answers 3

0

Try this: $id = $item->getProductId(); $product = Mage::getModel('catalog/product')->load($id);

1
  • I found the problem, but cant find the solution, there are products that cant get the categoriesID, and this is because the products were deleted and also the categories dont exist, since at the time i think i deleted them and created.So i tried to find some kint of solution in the future to detect type of products in my order, since i cant recover this information of the categories, so i tried to get from the order items the attribute set of the product, but i think even this information is not save on the orders items table. So can someone tell me if there is some kind of solution for this?
    – Info E
    Jan 17, 2018 at 11:36
0

try below code it is working for me

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));

    foreach ($orders as  $order) {

        foreach ($order->getAllItems() as $item) {

            echo $item->getProductId() ." - ";
            echo $item->getSku();

            $product = Mage::getModel('catalog/product')->load($item->getProductId());

            if($product->getId()){
                echo implode(",", $product->getCategoryIds());
            }
            echo "<br>";
        }
        echo "<br>*********************<br>";


    }
0

Try below code:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));
if($orders->getSize()) {
  foreach ($orders as  $order) {
    foreach ($order->getAllItems() as $item) {
        echo $item->getName() ." - ";
        echo $item->getSku();
        $product = $item->getProduct();

        if($product->getId()){
                echo implode(",", $product->getCategoryIds());
        }
            echo "<br>";
        }
        echo "<br>*********************<br>";


  }
}

Your Answer

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

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