Get Coupon code applied to the product in woocommerce [duplicate] - woocommerce

This question already has answers here:
WooCommerce Access orders that had discounts
(1 answer)
Get coupon data from WooCommerce orders
(2 answers)
Closed 3 years ago.
I am trying to retrieve the shop coupon code applied to a particular product. I used the below code but it did not work.
$WC_Cart = new WC_Cart();
$var = $WC_Cart->get_applied_coupons();
Can anyone please help me to get the solution. Thanks in advance!

I think this will solve your problem. I tested the code and it worked as:
echo order no
echo used coupons for this order no
run step 1 and step 2 for all orders
You may need to modify it.
//function to get orders - completed, pending and processing
function lets_get_all_orders()
{
$customer_orders = wc_get_orders( array(
'limit' => -1,
'status' => array('completed','pending','processing')
) );
return $customer_orders;
}
//function to get all used coupons in the orders
function lets_get_all_used()
{
$orders = lets_get_all_orders();
//traverse all users and echo coupon codes
foreach($orders as $order)
{
$order_discount = $order->discount_total;
$order_used_coupons = $order->get_used_coupons();
$order_id = $order->ID;
//check if any coupon is used in this order
if($order_discount>0)
{
echo "Order No: $order_id <br> Used Coupons:";
//display coupon code(s)
foreach($order_used_coupons as $order_used_coupon)
{
echo " - $order_used_coupon";
echo "<br>";
}
}
}
}
Please inform me if you need any help. Have a good day.

Related

WooCommerce function that disables an admin user to reduce the product stock quantity

I would like to add a function that is triggered every time that the stock quantity of a product will be changed in the admin product page, such that this function will not allow any reduce of the stock value - but only increase.
This is to prevent an admin user to reduce the stock quantity of the products.
Of course, this function should not be triggered if a product will be in an order, since then of course I would like the stock quantity to be reduced.
I tried the following function in the functions.php but unfortunately did not work.
Since I'm new to woocommerce and php, any ideas that could provide a solid solution to the problem?
// get old and new product stock quantity
function get_old_and_new_product_quantity_stock( $sql, $product_id_with_stock, $new_stock, $operation ) {
$product = wc_get_product( $product_id_with_stock );
$old_stock_quantity = $product->get_stock_quantity();
$new_stock_quantity = $new_stock;
echo $old_stock_quantity, $new_stock_quantity;
if ($new_stock_quantity < $old_stock_quantity) {
$new_stock = $old_stock_quantity;
$new_stock_quantity = $old_stock_quantity;
}
return $sql;
}
add_filter( 'woocommerce_update_product_stock_query', 'get_old_and_new_product_quantity_stock', 10, 4 );
You can use the update_post_meta action hook to check if the new value is less than the previous value and display error message.
This will work for quick edit and for product edit page. But the wp_die on product page will look bad so use the javascript to prevent submitting on product edit page (there was another question about it yesterday)
Be sure to test this snippet and create some orders that will reduce the stock automatically. I added is_admin() check but please do a good test.
add_action( 'update_post_meta', 'prevent_reducing_stock_metadata', 10, 4 );
function prevent_reducing_stock_metadata( $meta_id, $post_id, $meta_key, $meta_value ) {
// Check if the meta key is _stock and the new value is less than the previous value
if ( '_stock' == $meta_key && $meta_value < get_post_meta( $post_id, '_stock', true ) ) {
// Check if this is an update from the WordPress admin area
if ( is_admin() ) {
wp_die( __( 'Error: You cannot reduce the stock level for this product.' ), 'error' );
}
}
}

Auto complete order woocommerce [duplicate]

This question already has answers here:
WooCommerce Order Status (Autocomplete orders)
(1 answer)
WooCommerce: Auto complete paid orders
(5 answers)
Closed 11 months ago.
I have a WordPress site that sells courses, and I want to make the woocommerce change the order status for the processing to complete.
I tried this
https://woocommerce.com/document/automatically-complete-orders/
But when the payment failed, it became complete
I want to make it complete only for the paid courses.
Thank you
The reference you picked is ok
https://woocommerce.com/document/automatically-complete-orders/
Just needed a small adjustment:
/**
* Auto Complete Processing WooCommerce orders on Thankyou Page
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ( $order->has_status('processing') ) {
$order->update_status( 'completed' );
}
}
What we are doing with this adjustment is checking if the order has status processing before updating it to completed. This will avoid failed orders to be turned into completed.
Hope it works

Rearranging woocommerce checkout price order [duplicate]

In WooCommerce, I understand well that woocommerce_get_order_item_totals filter kook is used to customize order total rows like reordering them.
add_filter( 'woocommerce_get_order_item_totals', 'custom_order_of_from_order_table', 10, 2 );
function woocommerce_get_order_item_totals( $total_rows, $order ) {
// code here
return $total_rows;
}
I have tried to reorder the subtotal over the total, and the payment method below the total without success on WooCommerce ThankYou page. My PHP knowledge is very limited and I appreciate any help.
How to customize total rows from order table, reordering them on WooCommerce thankyou page?
The following will reorder items totals as desired on Woocommerce thankyou (order received) page only:
add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display = '' ){
// Only on "order received" thankyou page
if ( ! is_wc_endpoint_url('order-received') )
return $total_rows;
$sorted_items_end = array('cart_subtotal', 'order_total', 'payment_method');
$sorted_total_rows = array(); // Initializing
// Loop through sorted totals item keys
foreach( $sorted_items_end as $item_key ) {
if( isset($total_rows[$item_key]) ) {
$sorted_total_rows[$item_key] = $total_rows[$item_key]; // Save sorted data in a new array
unset($total_rows[$item_key]); // Remove sorted data from default array
}
}
return array_merge( $total_rows, $sorted_total_rows); // merge arrays
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
To make that work everywhere for customer orders and email notifications, just remove:
// Only on "order received" thankyou page
if ( ! is_wc_endpoint_url('order-received') )
return $total_rows;

woocommerce: how to access an order purchase in database [duplicate]

This question already has answers here:
How to get WooCommerce order details
(6 answers)
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
Closed 3 years ago.
I'm trying to access users purchase information from my wordpress admin plugin so that I can summarise orders e.g:
Product name
Custom data (in my case text associated with the
product)
When it was bought
How much they paid
It's mostly the information you can find on the "Account" > "Orders" page when the user is logged in.
I've looked through the woocommerce tables but can't find this information.
Can you suggest which tables I can query to pull together the information I'm looking for above?
The orders are stored at the wp_posts table with a 'shop_order' custom post type.
You can simply use the woocommerce function like this.
$woo_orders = wc_get_orders( array('numberposts' => -1) );
/* Loop each WC_Order object */
foreach( $woo_orders $order ){
/* Get the ID */
echo $order->get_id();
/* Get the status */
echo $order->get_status(); // The status
}
Or use the normal wordpress loop:
$loop = new WP_Query( array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'wc-ywraq-new' //will get the new order
) );
// Your post loop
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
// The ID
$order_id = $loop->post->ID;
// The object from WC_Order find the reference in woocommerce docs
$order = wc_get_order($loop->post->ID);
endwhile;
wp_reset_postdata(); // always
endif;
Here's the reference from github: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query

Woocommerce: Which function create an order?

I'm working on an android app based on a 3 Tier architecture. I have set up a products page and the Pay Now button integrated with PayPal. I also receive Success message after successful payment.
I'm now stuck at the order function. Which function of Woocommerce makes the order?
I'm simply trying to pass the price, product_id and other details to that particular function which will take care of the rest of the process.
Any help would be much appreciated. Thank you.
Here is some code that will generate an order. You'll have to substitute in your payment gateway class
// create a new checkout instance and order id
global $current_user;
$product_id = $_POST['productId'];
$product = wc_get_product($product_id);
$order = wc_create_order(array(
'customer_id' => $current_user->ID,
));
$order->add_product( $product, 1 );
$order->calculate_totals();
$my_gateway = new WC_Gateway_WhateverGatewayYouAreUsing();
$payment = $my_gateway->process_payment($order->id);
if($payment["result"] == "success") {
$order->update_status('completed');
wc_add_notice("Thank you!");
}
else {
$order->update_status("cancelled");
wc_add_notice("oh no! plz send me da moneez", 'notice');
}

Resources