What I have done:
I have few custom calculations to be done after placing an order for the customer in magento admin panel. I have hooked on to sales_order_save_after event inside tab inside my module's config.xml .
The Problem:
I need to get the customer_id of the actual customer for whom the order is been placed on the backend. How can this be done?
$_customer = Mage::getSingleton('customer/session')->getCustomer();
$customer_id=$_customer->getId();
The above will give me the customer_id in case of front end, I need a way to get the customer's id when ordering from backend.
In case, the event "adminhtml_sales_order_create_process_data", is what I need to hook on, do let me know. because I am also kind of confused about which event to hook on.
Help me out.
Please use this event sales_order_place_after.
And in your code, you might get the customer id like this:
public function hookSalesOrderPlaceAfter(Varien_Event_Observer $observer) {
$order = $observer->getEvent()->getOrder();
$customer_email = $order->getCustomerEmail();
$website_id = Mage::app()->getStore()->getWebsiteId();
$customer = Mage::getModel('customer/customer')->setWebsiteId($website_id)->loadByEmail($customer_email);
...
}
Hope it will help you.
This code below works for me (Thanks to Makwana Ketan shared this code)
$sessionquoteId = Mage::getSingleton('adminhtml/session_quote')->getQuote()->getId();
$sessionCustomerId = Mage::getModel('sales/quote')->loadByIdWithoutStore($sessionquoteId)->getCustomerId();
Related
Given that I have multiple websites in my Magento instance, how do I identify the website where a particular event happened? For example, observing the checkout_cart_add_product_complete event lets me catch all Add to Cart events. Let's say I wanted to get the website Id of the website where this Add to Cart event happened, how do you I do that?
public function addToCart(Varien_Event_Observer $observer) {
$product = $observer->getEvent()->getProduct();
$websiteId = $observer->getEvent()->get ??? ();
}
I know that I can get the websiteIds of the product that was added to the cart, by doing the following
$websiteIds = $observer->getEvent()->getProduct()->getWebsiteIds();
But that is not what I want, because if the product belongs to more than one website, it will give me all the websites and not the one where the Add to Cart event happened.
Thanks
Have you tried:
Mage::app()->getStore()->getId()
within your observer? That should give you the current store id..
You can directly get the StoreId from Mage, not from the observer object.
StoreId:
Mage::app()->getStore()->getStoreId();
WebsiteId:
Mage::app()->getStore()->getWebsiteId();
I'm building a website for a client, he wants to update his stock from his Presta website to his CRM. In order to achieve that it's very simple, I only have to call an URL like this :
http://crm.com/client1/bin/majstock.php?mode=stock&pdt=REF~-1;REF2~-1
Where REF is obviously the Ref of the product, and the number after ~ is the quantity to update, so in this example the customer bought 2 products, one is REF and the other one is REF2.
The problem is that I don't know where I should call this URL, and where I can get the parameters
Thanks for your help !
You can use the hook actionOrderStatusUpdates like that:
public function hookActionOrderStatusUpdate($params)
{
$OrderState = $params['newOrderStatus']; // an OrderState object
// $OrderState->id // order status ID
// $params['id_order'] // order ID
$Order = new Order((int)$params['id_order']);
$products = $Order->getProductsDetail();
// or
$products = $Order->getProducts();
}
You could create a new module with a hook on actionOrderStatusUpdate and call the CRM when the desired status is set on the order.
I am developing an OpenCart module and struggling with getting the order_id by using $this->session->data['order_id'].
My module is working fine if I comment out the part which unsets the order_id session variable in ControllerCheckoutSuccess class.
How can I ensure that I get the order_id before ControllerCheckoutSuccess unsets it?
Edit1: I need the order id to extract details like customer name, total amount, subtotal etc for that particular order. Is there some other method by which I can get the order id?
Edit2: Added this code in 'store/catalog/controller/module/myModule.php' in index() function. Using it to store order_id in temp session variable and use it on order success -
if (isset($this->session->data['temp_order_id'])
&& isset($this->request->get['route'])
&& $this->request->get['route'] == 'checkout/success')
{
// perform custom action
unset($this->session->data['temp_order_id']);
}
elseif (isset($this->session->data['order_id']))
{
$this->session->data['temp_order_id'] = $this->session->data['order_id'];
}
Please help!
In catalog/controller/checkout/success.php file, add:
$this->session->data['temp_order_id'] = $this->session->data['order_id'];
After:
if (isset($this->session->data['order_id'])) {
Then call your custom module or call your custom module before checkout success page controller is called.
Couldn't find any solution for this so sorted it by dynamically adding code (as mentioned by Sankar V) using vQmod. However, it adds a dependency that vQmod should be installed already.
Hope it helps someone facing similar problem.
Does anyone know how to implement a custom "module" that is triggered when an order is paid or complete?
And how am I able to call the order data from that observer?
I am also working with the "Serial Codes" plugin, and I want to send
an email to the person who bought this product, containing the serial
code.
Is there anybody who is able to help me out?
You can write an observer for the sales_order_save_before event. In the observer method you are able to get the order by $observer->getEvent()->getOrder(). Then you can check for the order status/state and add your code when the order is completed. This is the safest way, with the small downside, that the Observer function will always be triggered when the order is saved. Example Code:
public function onCompleteOrder(Varien_Event_Observer $observer)
{
/** #var $order Mage_Sales_Model_Order */
$order = $observer->getEvent()->getOrder();
if ($order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE) {
// do something
}
return $this;
}
By the way: A Magento order is usually becoming completed when
An invoice has been created AND
a shipment has been created
I have a two part question about customizing my Magento store.
When someone buys a downloadable product, I want to generate a licence code and include it in the invoice.
I have added a product attribute called ‘license_code’ to my product’s default attribute set and I want to set its value with php when a customer checks out.
What is the event to observe that will let me access the products in the cart just after they are purchased but before the invoice is created?
I also need to know what script to use to set a product’s attribute value during that event.
Thank you for your help!
Possible events are sales_order_place_before or sales_convert_quote_*.
You cannot save your 'license_code' attribute because that will affect all products, a product does not store it's values when ordered. Instead a better idea would be to manipulate the options of an order item.
function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
$orderItem = $observer->getOrderItem();
$options = $orderItem->getProductOptions();
$options['licence_code'] = YOUR-DOWNLOADABLE-CODE-HERE;
$orderItem->setProductOptions($options);
}
Retrieving the code later is essentially the same process with getProductOptions(), the order item objects are already used on the order view pages so are easy to find and use in your theme.
Ok I think I got it figured out.
I set up my event observers as follows:
<events>
<sales_order_item_save_before>
<observers>
<downloadable_observer>
<class>Licensing_Catalog_Model_Observer</class>
<method>generate_licenses</method>
</downloadable_observer>
</observers>
</sales_order_item_save_before>
</events>
and then my observing function as:
public function generate_licenses($observer)
{
$orderItem = $observer->getEvent()->getItem();
$options = $orderItem->getProductOptions();
$options['licence_code'] = 'YOUR-DOWNLOADABLE-CODE-HERE';
$orderItem->setProductOptions($options);
return $this;
}
Thank you so much for the help, clockworkgeek!