Question

I am working on order collection query outside magento directory.It is working fine and also providing results. But suddenly fetching records time is more as compare to previous time. Now it takes more time for fetching same record database. Don't know what happens and what to do?

    <?php

$order_collections = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('increment_id')
->addAttributeToSelect('entity_id')
->addAttributeToFilter('store_id', array('in' => array('finset' => $storeId)));
$order_collections->setOrder('created_at', 'DESC');

foreach($order_collections as $_order)
{ 

    $incrment_id =  $_order->getIncrementId();//die;
    $ordered_items = Mage::getModel('sales/order')->loadByIncrementId($incrment_id);

    if(count($ordered_items)!=0)
    {
        foreach($ordered_items->getAllVisibleItems() as $item)
        {
            $allproductids[] = $item->getProductId();
        }
    }

    $collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', array('in' => $allproductids));

    $collection->addAttributeToSelect
    (
        array
        (
            'entity_id',
            'name',
            'description',
            'short_description',
            'price',
            'special_price',
            'image_label',
            'small_image_label',
            'thumbnail_label',
            'barcode'
        )
    )->setStoreId($store_id);

    //pr($collection->getData());
                foreach ($collection as $product_details)
                {
                    $product = Mage::getModel('catalog/product');
                    $pId = $product_details->getId();
                    $productDetailsImage = $product->load($product_details->getId())->getThumbnail();
                    $loaddata[$product_details->getId()]['bar_code'] = $product_details->getBarcode();
                    $product_image_url = $productDetailsImage;
                    $x = explode('/',$product_image_url);
                    $loaddata[$product_details->getId()]['image_name'] = end($x);

                    if($product_details->getTypeId() == "configurable")
                    {
                        $config = $product_details->getTypeInstance(true);
                        $attribute_details = $config->getConfigurableAttributesAsArray($product_details);
                        $all_child_product_details = array();
                        foreach($attribute_details as $key=>$attribute_value)
                        {
                            $option_title = $attribute_value['label'];
                            $product_values = $attribute_value['values']; 
                            $op_id = $attribute_value['id'];
                            $code = $attribute_value['attribute_code'];

                            $child_product_details = array();
                            $allChildProductDetails = array();
                            foreach ($product_values as $product_value) 
                            {   
                                $child_product_details = $product_value;
                                $option_type_id = $product_value['value_index']; 
                                $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product_details);
                                 $simple_collection = $conf->getUsedProductCollection()
                                ->addAttributeToSelect('*')
                                ->addAttributeToFilter( $code, array('eq' => $option_type_id))
                                ->addFilterByRequiredOptions();

                                $conf_pro_id = array();
                                foreach($simple_collection as $simple_product)
                                {
                                    $simple_pro =  $simple_product->getData();
                                    if ($simple_pro['is_in_stock'] > 0) 
                                    {
                                        $conf_pro_id[] = $simple_product->getId();
                                    }
                                } 
                                $allChildProductDetails[] = array('new_data'=>$child_product_details,'products'=>$conf_pro_id);
                            }   
                            $all_child_product_details[] = array('type'=>$option_title,'data'=>$allChildProductDetails);
                        }
                        $loaddata[$product_details->getId()]['attribute_details'] = $all_child_product_details;
                    }

                }

    foreach($ordered_items as $item)
    {
        $id = $storeId.$website_id.$_order->getIncrementId().$item->getData()['product_id'];//entityid
        $storeId = $storeId;
        $website_id = $website_id;
        $local_id = '';
        $order_id = $_order->getIncrementId();
        $product_id = $item->getData()['product_id']; //product id
        $parent_product_id = ''; /* Blank this time */
        $product_name = escape_sqlite_value($item->getName());
        $product_sku = escape_sqlite_value($item->getSku());
        $product_barcode = escape_sqlite_value($loaddata[$item->getData()['product_id']]['bar_code']);
        $product_weight = $item->getData()['weight'];
        $product_price = str_replace(',','',$item->getData()['price']);
        $product_quantity = $item->getQtyOrdered();
        $product_type = $item->getData()['product_type'];
        $product_attribute_details = escape_sqlite_value(json_encode($loaddata[$item->getData()['product_id']]['attribute_details']));
        $product_assets_type = 1; /* 1 - Image */
        $product_assets_value = escape_sqlite_value($loaddata[$item->getData()['product_id']]['image_name']);
        $product_tax_amount = '';
        $product_is_discount = '';
        $product_percentage = '';
        $product_discount_amount = '';
        $product_is_gift_wrap = '';
        $product_gift_wrap_qty = '';
        $product_gift_wrap_amount = '';
        $product_item_refunded_qty = '';
    }
}
Was it helpful?

Solution

load product here and skip other product just load product data

foreach($ordered_items->getAllVisibleItems() as $item)
        {

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

                    $productDetailsImage = $product->load($product_details->getId())->getThumbnail();
                    $loaddata[$product_details->getId()]['bar_code'] = $product_details->getBarcode();
                    $product_image_url = $productDetailsImage;
                    $x = explode('/',$product_image_url);
                    $loaddata[$product_details->getId()]['image_name'] = end($x);

                    if($product_details->getTypeId() == "configurable")
                    {
                        $config = $product_details->getTypeInstance(true);
                        $attribute_details = $config->getConfigurableAttributesAsArray($product_details);
                        $all_child_product_details = array();
                        foreach($attribute_details as $key=>$attribute_value)
                        {
                            $option_title = $attribute_value['label'];
                            $product_values = $attribute_value['values']; 
                            $op_id = $attribute_value['id'];
                            $code = $attribute_value['attribute_code'];

                            $child_product_details = array();
                            $allChildProductDetails = array();
                            foreach ($product_values as $product_value) 
                            {   
                                $child_product_details = $product_value;
                                $option_type_id = $product_value['value_index']; 
                                $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product_details);
                                 $simple_collection = $conf->getUsedProductCollection()
                                ->addAttributeToSelect('*')
                                ->addAttributeToFilter( $code, array('eq' => $option_type_id))
                                ->addFilterByRequiredOptions();

                                $conf_pro_id = array();
                                foreach($simple_collection as $simple_product)
                                {
                                    $simple_pro =  $simple_product->getData();
                                    if ($simple_pro['is_in_stock'] > 0) 
                                    {
                                        $conf_pro_id[] = $simple_product->getId();
                                    }
                                } 
                                $allChildProductDetails[] = array('new_data'=>$child_product_details,'products'=>$conf_pro_id);
                            }   
                            $all_child_product_details[] = array('type'=>$option_title,'data'=>$allChildProductDetails);
                        }
                        $loaddata[$product_details->getId()]['attribute_details'] = $all_child_product_details;
                    }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top