How to get all order collection in Customer account My Orders - php

I want to add Order search functionality in customer account My Orders section. As if customer have many orders so they dont need to navigate instead of they can search by orders like this -
http://demo.dckap.com/m2/sales/order/history/
I tried to get customer session in Magento_Sales/templates/order/history.phtml page but its not working.
Is there any way we can pass input search box value to order object ?
Or Load customer orders collection ?

you need to override few files. you can try with below code, it should work.
for view access, override canView() function defined in Magento\Sales\Controller\AbstractController\OrderViewAuthorization.php
//overide getOrders() method in history.php block file as below
// to get customer id from customer session
if (!($customerId = $this->_customerSession->getCustomerId())) {
return false;
}
// to load orders for customer
$this->orders = $this->_orderCollectionFactory->create()->addFieldToFilter('customer_id', array('eq' => $customerId));
//to add filter for search
if (!empty(<yoursearchterm>)) {
$this->orders->addFieldToFilter(
'entity_id', ['like' => '%' . <yoursearchterm> . '%']
);
}
and finally add below line
return $this->orders;
Next you will need to override history.phtml and add your search boxes inside form. you can set action like below
action="<?php echo $block->getUrl('sales/order/history'); ?>"
Hope this helps!!

This is my solution to load logged In customer orders collection -
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
$customer_id = $customerSession->getCustomer()->getId();
$order_collection = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('customer_id', $customer_id)->addAttributeToFilter('increment_id', array('eq' => $orderId));
}
Then after that I have replace order collection with my order collection $order_collection to get desired search order value from text box.

Related

How to get order ID from shipment ID or from observer?

I'm using sales_order_shipment_save_after observer to collect tracking numbers. I have tried $event->getOrder but it will not load any order object. I suppose it's the shipment observer I use as opposed to order observer. Usually, getId or getIncrementId on Order would ok. Right now I was able to get tracking number and shipment ID just fine. Is there a way to get order ID from shipment ID in Magento?
Here is what I got
$shipment = $event->getShipment();
$tracks = $shipment->getAllTracks();
foreach ($tracks as $track) {
$orderTracking = $track->getTrackNumber();
}
need to try below code
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$shippingMethod = $order->getShippingMethod();
I am try to get order details form shipment object
Your function , should like this
public function you_function_name(Varien_Event_Observer $observer)

Get Group id of product in Order in a Hook

In my website i have created a custom hook. here is my code :
<?php function myActionHookFunctionvars($vars) {
$orderid = $vars['orderid'];
$ordernumber = $vars['ordernumber'];
$amount = $vars['amount'];
//here i need the group id of the product in which it lies.
}
add_hook("AfterShoppingCartCheckout",1,"myActionHookFunctionvars");?>
So, in this I need to get the information of the group(gid) in which this product which we are ordering lies. As I wants to apply this hook for only products of a specific group, not for all the groups.
I have found answer to the above mentioned question question.
Put the code below amount variable in function :
$gidsql = "SELECT gid FROM `tblproducts` WHERE id=(SELECT packageid FROM `tblhosting` WHERE orderid=$orderid)";
$gidresult = mysql_query($gidsql);
while($row = mysql_fetch_array($gidresult)){
$mygid = $row['gid'];
}
It may useful to anyone.

CodeIgniter DataMapper order of related items

I'm writing an invoicing application in CodeIgniter/DataMapper.
Each customer has many invoices.
In the view, I want to be able to display customer data and then a table of the invoices for that customer.
At the moment, I load the customer and their related invoices in the controller and send them to the view.
public function view($id) {
$c = new Customer($id);
$c->invoice->get_iterated();
$data['customer'] = $c;
}
But when I iterate over the invoices to display them in the view, I really want them to be in descending order by date, but of course they're not.
Is there any way to specify the order of related items or to sort them before displaying them, or do I need to load the invoices separately, something like in the following the controller?
public function view($id) {
$c = new Customer($id);
$i = new Invoice();
$i->order_by('invoice_date', 'desc');
$i->get_where(array('customer_id' => $id));
$data['customer'] = $c;
$data['invoices'] = $i;
}
You can simply add an ->order_by() call before issuing the get_iterated(). So in your examples it would look something like:
$c->invoice->order_by('invoice_date', 'desc')->get_iterated();

How do I check if a product is in comparison list magento

You can add product to compare. I have to show the link "Add to Compare" if the product is not added already otherwise show "Compare". I have to check if the product is in comparison list.
I have list.phtml file.
I tried this but this gives all the products added in comparison list.
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
I can loop through the returned products and can check if the product is in this collection but I am looking for a single call which take the product id or sku and return true or false accordingly.
I also added the filter like this but does not work
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()
->addAttributeToFilter('sku', $item->getSku());
Try to use
Mage_Catalog_Model_Product_Compare_List
and its method:
getItemCollection
Like this:
$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection();
$collection->.....Additional filters go here.
Why helper didn't worked? Because collection is already loaded there:
v 1.6
public function getItemCollection()
{
if (!$this->_itemCollection) {
$this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
} elseif ($this->_customerId) {
$this->_itemCollection->setCustomerId($this->_customerId);
} else {
$this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
Mage::getSingleton('catalog/product_visibility')
->addVisibleInSiteFilterToCollection($this->_itemCollection);
/* Price data is added to consider item stock status using price index */
$this->_itemCollection->addPriceData();
$this->_itemCollection->addAttributeToSelect('name')
->addUrlRewrite()
->load();
/* update compare items count */
$this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection));
}
return $this->_itemCollection;
}
So you can load collection by model and filter itself in template or in your own custom helper - model.

How to get Shipping/Billing Address ID of an order outside Magento core API?

I want to get a shipping/billing address id from a just complete order out of Magento.
I have tried the following code but it's not worked:
Mage::getModel('sales/order')->load($array_data["order_id"])->getShippingAddressId()
Does someone have any ideas?
The following might help someone looking for a similar solution:
// Get the id of the last order for the current user(session)
$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
// If an order actually exists
if ($orderId) {
//Get the order details based on the order id ($orderId)
$order = Mage::getModel('sales/order')->load($orderId);
// Get the id of the orders shipping address
$shippingId = $order->getShippingAddress()->getId();
// Get shipping address data using the id
$address = Mage::getModel('sales/order_address')->load($shippingId);
// Display the shipping address data array on screen
var_dump($address);
}
Note: Obviously this solution requires the user to have a current session
Good luck.
First, break apart your chained call to make make sure you're actually loading an order with
$order = Mage::getModel('sales/order')->load($array_data["order_id"]);
var_dump($order->getData());
Assuming you've loaded the order, look at the values dumped above. There's no shipping_address_id. That, combined with there being no method getShippingAddressId on a Mage_Sales_Model_Order is why your code isn't working.
Try
$order = Mage::getModel('sales/order')->load($array_data["order_id"]);
$id = $order->getShippingAddress()->getId();
The getShippingAddress address method will return an address object, which you can inspect for its id. If you look at the Mage_Sales_Model_Order class definition, you can see the method definitions
//magento 1.4
public function getShippingAddress()
{
foreach ($this->getAddressesCollection() as $address) {
if ($address->getAddressType()=='shipping' && !$address->isDeleted()) {
return $address;
}
}
return false;
}
public function getAddressesCollection()
{
if (is_null($this->_addresses)) {
$this->_addresses = Mage::getResourceModel('sales/order_address_collection')
->addAttributeToSelect('*')
->setOrderFilter($this->getId());
if ($this->getId()) {
foreach ($this->_addresses as $address) {
$address->setOrder($this);
}
}
}
return $this->_addresses;
}
The TL;DR for the code above is, address IDs aren't stored with the orders model. The addresses for all orders are stored as a sales/order_address or Mage_Sales_Model_Order_Address object.
$order = Mage::getModel('sales/order')->load($orderId);
//Get shipping address Id
$order->getShippingAddress()->getId();
//format output
echo $order->getShippingAddress()->format('html');
//Get shipping address data
print_r($order->getShippingAddress()->getData());
After uncountable debugging and googling, I got it solved:
For incremental order address id based on order,
$order_id=Mage::getSingleton('checkout/session')->getLastRealOrderId();
$sales_order=Mage::getModel('sales/order')->load($order_id);
$billing_address_id=$sales_order->billing_address_id;
$shipping_address_id=$sales_order->shipping_address_id;
For address entity id of the order based on customer,
$quote = Mage::getSingleton('checkout/session')->getQuote();
$billing_address_id=$quote->getBillingAddress()->customer_address_id;
$shipping_address_id=$quote->getShippingAddress()->customer_address_id;
Try This, you can get the shipping_address_id and billing_address_id
$orderCollection = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('main_table.customer_id',$session->getId());
echo "<pre>";
foreach ($orderCollection as $order){
$shippingAddress = Mage::getModel('sales/order_address')->load($order->getShippingAddressId());
$billingAddress = Mage::getModel('sales/order_address')->load($order->getBillingAddressId());
print_r($order->getData());
print_r($shippingAddress->getData());
print_r($billingAddress->getData());
}

Categories