Question

my custom order action get customer by ID in magento and check condition order available or not

and how to get dynamically load get customer by ID in magento my custom order
action

so how to achieve this.

/app/code/local/Modulename/Connect/controllers/CustomerController.php

    $customerId = 23;

    $customer = Mage::getModel('customer/customer')->load($customerId);

    if($customer != '') {
         $cust_id=$customer->getId();
    }
Was it helpful?

Solution

public function getMyOrdersAction()
{

    //$customerId = 21;

    $customerId = $this->getRequest()->customerId;

    $customer = Mage::getModel('customer/customer')->load($customerId);

    if($customer != '') {
         $cust_id=$customer->getId();

         $basecurrencycode = Mage::app()->getStore($store)->getBaseCurrencyCode();
         $res = array();
           $totorders = Mage::getResourceModel('sales/order_collection')
                 ->addFieldToSelect('*')
                 ->addFieldToFilter('customer_id', $cust_id);
         $res["total"] = count($totorders);
       $orders = Mage::getResourceModel('sales/order_collection')
                 ->addFieldToSelect('*')
                 ->addFieldToFilter('customer_id', $cust_id)
                 ->setOrder('created_at', 'desc')
                   ->setPage($curr_page,$page_size);
                //$this->setOrders($orders); 
        # start order  loop
        if(count($orders) > 0)
        {
           foreach ($orders as $order) {

                $shippingAddress = $order->getShippingAddress();
                if(is_object($shippingAddress)) {
                    $shippadd = array();
                    $flag = 0;
                    if(count($orderData)>0)
                    $flag = 1;
                    $shippadd = array(
                         "firstname" => $shippingAddress->getFirstname(),
                         "lastname" => $shippingAddress->getLastname(),
                         "company" => $shippingAddress->getCompany(),
                         "street" => $shippingAddress->getStreetFull(),
                         "region" => $shippingAddress->getRegion(),
                         "city" => $shippingAddress->getCity(),
                         "pincode" => $shippingAddress->getPostcode(),
                         "countryid" => $shippingAddress->getCountry_id(),
                         "contactno" => $shippingAddress->getTelephone(),
                         "shipmyid" => $flag
                    ); 
                }
                $billingAddress = $order->getBillingAddress();
                if(is_object($billingAddress)) {
                    $billadd = array();
                    $billadd = array(
                         "firstname" => $billingAddress->getFirstname(),
                         "lastname" => $billingAddress->getLastname(),
                         "company" => $billingAddress->getCompany(),
                         "street" => $billingAddress->getStreetFull(),
                         "region" => $billingAddress->getRegion(),
                         "city" => $billingAddress->getCity(),
                         "pincode" => $billingAddress->getPostcode(),
                         "countryid" => $billingAddress->getCountry_id(),
                         "contactno" => $billingAddress->getTelephone()
                    );
                }
                $payment = array();
                $payment = $order->getPayment();



            try {
                $payment_result = array (
                          "payment_method_title" => $payment->getMethodInstance()->getTitle(),
                          "payment_method_code" => $payment->getMethodInstance()->getCode(),
                );
                if($payment->getMethodInstance()->getCode()=="banktransfer") {

                $payment_result["payment_method_description"] = $payment->getMethodInstance()->getInstructions();
                }
                    }
            catch(Exception $ex2) {

                            }

                $items = $order->getAllVisibleItems(); 
                $itemcount=count($items);
                $name=array();
                $unitPrice=array();
                $sku=array();
                $ids=array();
                $qty=array();
                $images = array();
                 $test_p  = array();
                 $itemsExcludingConfigurables = array();
                 $productlist = array();
                   foreach ($items as $itemId => $item) {
                     $name= $item->getName();
                     //echo $item->getName();
                     if($item->getOriginalPrice() > 0) {
                        $unitPrice =  number_format($item->getOriginalPrice(), 2, '.', '');
                     }
                     else {
                        $unitPrice =   number_format($item->getPrice(), 2, '.', '');
                     }

                     $sku=$item->getSku();
                     $ids=$item->getProductId();
                     //$qty[]=$item->getQtyToInvoice();
                     $qty= (int)$item->getQtyOrdered();
                     $products = Mage::getModel('catalog/product')->load($item->getProductId());
                     $images= Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'/media/catalog/product'.$products->getThumbnail();

                     $productlist[] = array (
                             "name" => $name,
                             "sku" => $sku,
                             "id" => $ids,
                             "quantity" =>(int)$qty,
                             "unitprice" => $unitPrice,
                             "image" => $images,
                             "total_item_count" => $itemcount,
                             "price_org" =>  $test_p,
                             "price_based_curr" => 1,
                        );

                }  # item foreach close


                $order_date = $order->getCreatedAtStoreDate().'';
                $orderData = array(
                     "id" => $order->getId(),
                     "order_id" => $order->getRealOrderId(),
                     "status" => $order->getStatus(),
                     "order_date" => $order_date,
                     "grand_total" => number_format($order->getGrandTotal(), 2, '.', ''),
                     "shipping_address" => $shippadd,
                     "billing_address" => $billadd,
                     "shipping_message" => $order->getShippingDescription(),
                     "shipping_amount" => number_format($order->getShippingAmount(), 2, '.', ''),
                     "payment_method" => $payment_result,
                     "tax_amount" => number_format($order->getTaxAmount(), 2, '.', ''),
                     "products" => $productlist,
                     "order_currency" => $order->getOrderCurrencyCode(),
                     "order_currency_symbol" => Mage::app()->getLocale()->currency($order->getOrderCurrencyCode())->getSymbol(),
                     "currency" => $this->currency,
                     "couponUsed" => 0
                );
                $couponCode = $order->getCouponCode();
                if($couponCode!="") {                   
                    $orderData["couponUsed"] =  1;
                    $orderData["couponCode"] =  $couponCode;
                    $orderData["discount_amount"] =  floatval(number_format($this->convert_currency(floatval($order->getDiscountAmount()),$basecurrencycode,$this->currency), 2, '.', ''))*-1;
                }

                $res["orderList"][] = $orderData;
                $result = array('result'=>$res,'status'=>"success",'message'=>"successfully");

           } # end foreach
        }
        else
        {
            $result = array('status'=>"error",'message'=>"Data Not Found");
        }

        echo json_encode($result);





    } 
    else{

            echo json_encode(array('status'=>'error','message'=> $this->__('Please Login to see the Orders')));

    }

} # end my orders
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top