Get Group id of product in Order in a Hook - php

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.

Related

change order state on hook with condition

I'm developing a module for my Prestashop website and I'm stuck for hours now on a thing. I use Prestashop 1.7.5.1.
Here is the use case :
Some products are set in the default category "preorder" who have the ID 21. When a customer buy an article who is in this category, I would like to automatically change the order state ID to the preorder ID. The order state ID for the preorder is 18.
Here is the code :
public function hookDisplayOrderConfirmation($params)
{
$objOrder = $params['order'];
$products = $objOrder->getProducts();
foreach ($products as $product)
{
$cat = (int)$product->id_category_default;
if($cat == 21)
{
$history = new OrderHistory();
$history->id_order = $objOrder->id;
 $history->changeIdOrderState(18, $objOrder->id);
break;
}
}
}
By the way, where I can find all the class and method of Prestashop ? for example, where I can find all the variable of the $objOrder above ?

Thanks a lot for your support :) and have a nice day !
You can find all the variable of Order object inside [prestashop]/classes/order/Order.php class. Here you can find all the variable and function/ methods related to the Order object.

Get order id from cart id module prestashop 1.6

controller/front/validation.php
<?php
class Paytr_CheckoutValidationModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
$this->display_column_left = false;
$this->display_column_right = false;
$cart = $this->context->cart;
$total = $cart->getOrderTotal;
$currency = $this->context->currency;
$customer = new Customer( $cart->id_customer );
if ( !Validate::isLoadedObject($customer) )
Tools::redirect('index.php?controller=order&step=1');
//$this->context->cart->delete();
//$isOrderX = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT * FROM '._DB_PREFIX_.'orders WHERE id_cart = '.$cart->id);
//var_dump($_POST);
//Tools::redirect('index.php?controller=history');
//Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.(int)$this->module->currentOrder.'&key='.$customer->secure_key);
echo 'index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.(int)$this->module->currentOrder.'&key='.$customer->secure_key;
}
}
I need to find order id from cart id. These are I tried. I couldnt get id_order. I was hopeful for Db query. But it doesnt work I think.
You can get it with this code: $id_order = Order::getOrderByCartId($id_cart);
this is the problem cause deletes cookie after validate order. So It doesnt show id_cart. So I figure out like this.
$isOrderX = Db::getInstance()->getRow(' SELECT * FROM '._DB_PREFIX_.'orders WHERE id_customer = '.$cart->id_customer.' ORDER BY id_order DESC ');
Tools::redirect('index.php?controller=order-confirmation&id_cart='.$isOrderX['id_cart'].'&id_module='.$this->module->id.'&id_order='.$isOrderX['id_order'].'&key='.$customer->secure_key);
As long as the order is not registered, you can not get "id_order".
If the order is registered and you are sure that there is no better way, you can use the following code to find the latest cart that has been ordered:
$this->context->customer->getLastCart(true);

How to get all order collection in Customer account My Orders

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.

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)

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