I'm installing some tracking code into the checkout_success.php page. I need to be able to grab the coupon code/discount code name from the order, if one was used so that I can echo it out in my tracking script.
I was wondering if anyone knows how to do this?
I'm using this contribution of discount coupons; ot_discount_coupons.php, August 4, 2006, author: Kristen G. Thorson, ot_discount_coupon_codes version 3.0
It seems that the coupon code is not actually stored in the order_totals, but in a seperate discount_coupons_to_orders table. is there a query i can do on this table to find the matching coupon code used for this order? i tried the following but it return nothing;
$coupon_query = tep_db_query("select coupons_id from discount_coupons_to_orders where orders_id = '".(int)$orders['orders_id']."' ORDER BY orders_id DESC LIMIT 1"); $coupon_id = tep_db_fetch_array($coupon_query); $couponid = $coupon_id['coupon_id'];
Thank you.
Instead of:
$couponid = $coupon_id['coupon_id'];
Try:
$couponid = $coupon_id['coupons_id'];
My solution is probably a bit more than you're looking for but worked well for my work. I do a query at around line 76 in this php file that queries out the order information and the coupon code.
$orders_query = tep_db_query("select orders.orders_id from " . TABLE_ORDERS . " left join discount_coupons_to_orders dco on orders.orders_id=dco.orders_id where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
$orders = tep_db_fetch_array($orders_query);
The purpose of this is so that way a customer can get info on their order. You can reference your coupon code in here like I did showing that the discount was applied.
echo '<br /><br /><span style="color:red"><b>Your order number is #'.$orders['orders_id'].(!empty($orders['coupons_id']) ? ' Discount Code: '.$orders['coupons_id'] : "").' you can now view your receipt</b>.</span>';
We found customers immediately want to see a "receipt", so we link directly back to account history. But the key here is that if you use a join to the main order information you can access the order info and the coupon code in one shot.
If I remember correctly you can pass on the coupon code through the page, and have access to it. If not, hack into the coupon entry page, and on the "if coupon is ok" part, save it in a session variable. On the success page you`d just use something like $_SESSION['couponcode'], no use in having more queries. This is probably 2 lines of modification on the coupon entry page.
Related
back-office side, I'm looking (and I can not find 😅) the name of the variable php where there is the total amount TTC of the order (in order's table) ...
I can look in cart.php, AdminOrdersController.php, I can not find ...
It all depends where you are, but otherwise with a command id you can find it by doing :
$order = New Order($id_order);
echo $order->total_paid;
Regards,
Regarding OpenCart store selling digital downloads.
I am trying to add a script to the product page to warn the customer if that specific product (download) is already purchased and exists in Account>Downloads.
The reason is to avoid customers purchasing the same product twice.
Appreciate your help.
Edit:
I have tried getting the product IDs of all orders by a customer using a SQL query and that works fine externally but inside OpenCart I am facing issues.
A query such as:
SELECT product_id
FROM ocuk_order_product
WHERE order_id IN (
SELECT order_id
FROM ocuk_order
WHERE customer_id = 'xxxx'
)
My main problem is not being sure how to get similar results in OpenCart Product page. (Which pages and path exactly and where inside the files)
Tried this post as well: Opencart get product downloads
But it is not exactly working on product page (product.php)
Opencart Version 3.0.2.0
Ok I'll take a stab at this but let's clarify a few things:
1) DB_PREFIX is simply a php constant that's declared in your config.php file. Based on the query you provided it's probably equal to ocuk. The point is, you write queries using this constant and they become portable across installations regardless of the users' chosen database prefix.
2) Opencart is based on the MCVL (an extension of MVC which includes language files) structure. The place to execute queries is in the model. The place to call model methods is in the controller. The controller passes output to the view and often uses language variables from language files.
So what I would do is write function and put it in your product model – in this case that's catalog/model/catalog/product.php. This function is called a method since it's now part of your model class. The function will output the rows in your table that a logged in customer has purchased the given product. It's also important to check (a) that the customer is logged in and (b) that the orders you are querying against are real orders - with an order_status_id > 0. We can go into that but suffice to say that you always need to check the order_status_id if you want to know about actual completed orders. Such a method could look something like this:
public function getOrderProductHistory($product_id) {
$result = [];
if ($customer_id = $this->customer->getId()) {
$sql = "
SELECT *
FROM " . DB_PREFIX . "order o
JOIN " . DB_PREFIX . "order_product op USING (order_id)
WHERE o.order_status_id > 0
AND o.customer_id = '" . (int)$customer_id . "'
";
$query = $this->db->query($sql);
$result = $query->rows;
}
return $result;
}
Now in your controller (on a product page that's catalog/controller/product/product.php) you can call this method to get results like this:
$order_product_history = $this->model_catalog_product->getOrderProductHistory($product_id);
And then do something based on the output:
if ($order_product_history) {
$data['has_been_ordered'] = true;
} else {
$data['has_been_ordered'] = false;
}
Now $has_been_ordered is a boolean that you can access in your view to display a message to the customer. There's still a bit more code for you to write but hopefully this helps get you off to a good start.
I have a bit of an odd situation that I am trying to fix. Magento v1.9.2.4
I have only 2 different attribute sets. A and B.
I want to display the stock quantity/availability for set B, but not
set A.
To make things a bit more complex, I have 14 Customer Groups, I only want 6 of those groups to ever see any quantities/availability.
Here's what I have done so far to arrange this:
$customerSession = Mage::getSingleton('customer/session');
if($customerSession->isLoggedIn()){
$groupId = $customerSession->getCustomerGroupId();
$group = Mage::getModel('customer/group')->load($groupId);
if ('custgroup_1' == $group->getCode()){
$qty = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo 'Quantity Available: ' . $qty;
}
}
The above snippet is repeated 5 times with the [if ('custgroup_1' ...] changed to accommodate the group I need this to show up for. That part is working just fine.
I only need to specify somehow that I only want the availability to show up for the attribute set B. Then Regardless of the customer group never show the qty/availability for attribute set A.
I have tried playing with the inventory options on the product page. (disabled stock management = qty still shows | enabled stock management, set qty to 0 and my custom options disappear | ect.) Nothing within the magento backend seems to work.
I'm a newbie to this whole Magento/Dev thing. So I apologize if this is considered a silly question.
Thank you for any/all help!
So, Immediately after posting this I realized I was thinking about the problem all wrong.
Because any item that isn't in attribute set B isn't having stock managed, I was able to write a condition that enables output only for products that have an inventory level greater than 0.
Here is the code for anyone who might happen to need to show a quantity for products based on a customer group, and hide availability for any item that has inventory management set to no, but is set to "In Stock".
$customerSession = Mage::getSingleton('customer/session');
if($customerSession->isLoggedIn()){
$groupId = $customerSession->getCustomerGroupId();
$group = Mage::getModel('customer/group')->load($groupId);
if ('custgroup_1' == $group->getCode()){
$__manStock = $_product->getStockItem()->getManageStock();
$__invAmt = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($__manStock > 0)
echo $this->__("Available Qty: $__invAmt");
}
}
So I have to send variables to an affiliate website and they need the order id, price, 2-letter state abbriviation, and the country abbreviation. I have already got the price and country abbr. but I still need the order id and the 2-letter state abbreviation. The code I have right now is as follows:
$order = Mage::getSingleton('checkout/session')->getLastRealOrderId();//doesnt work
$amount = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();//works
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');//Gives full name of State
$countryId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id');//works
echo " display: $order $amount $stateId $countryId";//prints out the variables
I have been looking on all the code on here for the order id but nothing has returned anything. So I'm wondering what I am doing wrong with that/why it is not printing out anything.
The second thing is that I am wondering if there is an easy way to get the 2-leter state abbreviation? I have also tried 'region_id" instead of 'region' but that just gives me the number code (not the letter code).
Answers to either of these problems would be greatly appreciated.
Once you have the order loaded you can call 'region_code' or getRegionCode(). I'm using magento 1.9. Not sure if it is available in previous versions.
The best way is to send this information through the success.phtml file as only in that file you will get that info and avoid sending order ids for uncompleted orders (failed paypal transactions etc.)
1) depending on your checkout method, you may or may not have the state code in your data.
So if you get the name of states by
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');
then add this code afterwards:
$states = Mage::getModel('directory/country')->load('US')->getRegions();//get list of states
foreach($states as $state){
if($state[default_name] == $stateId){ //check if the names match
echo $state[code]; //get code
}
}
2) On success page
$orderId = $this->getOrderId();
should work.
Banging my head for the last two days but unable to achieve this. Help!!
Whenever a shipment email is communicated I want to trigger a code which will send a SMS to the customer informing him/her that his order has been dispatched and also communicate the tracking number.
The code will be something like this:
<?php
$url = "http://www.abcde.in/binapi/pushsms.php?usr=xyz&pwd=abc&sndr=MEGYTR&ph=8888829554&text=This is test. MegaYtr&rpt=1";
$result = file_get_contents($url);
?>
Questions:
1) From where do I run this code?
2) How do I get additional info like Order Number, Customer Name, Grand Total & Tracking Number. I had done something similar for sending SMS when customer places order at that I used this code:
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$shipping_address_data = $order_details->getShippingAddress();
$first_name = $shipping_address_data['firstname'];
$telephone = $shipping_address_data['telephone'];
$amount_paid = $order_details->total_paid;
$message = 'Dear '.$first_name.' thank you for shopping at abc.com. Your order'.$this->getOrderId().' amounting to Rs.'.$amount_paid.'is being processed.';
echo '<b>'.$message.' Please check your email for further details.</b>';
I am using Magento Community 1.7.0.1.
try this
i would like to give you an simple solution without need of modifying core files.
for that you need to create an observer for SuccessAction below is the event that will trigger your code when an order is successfull
checkout_onepage_controller_success_action
this will help you in creating observer for the above event create observer using this
one more thing i would like to add is in the controller at location Mage/Checkout/OnepageController search for successAction, this is the Action which is processed on the succes of order. Here on line 240 if you comment $session->clear(); than you would not require to place order again and again, just by refreshing the page you can check your changes.
And lastly FYI above event will dispatch orderId by using that you can load the order object, for doing that the below is the code
//load order object
$_order = Mage::getModel('sales/order')->loadByIncrementId($order_id_from_observer);