Woocommerce: How do I generate unique IDs for each purchased item? - wordpress

I'm working in WordPress 4.3.1., with WooCommerce 2.4.7. For my employer's current store project, we need to have unique IDs generated for each item purchased, which can never be the same as any other purchase.
For example, if someone buys 3 shirts of the same design and size, each of these shirts, once in the cart, should each have a unique item ID displayed. Regardless of the number of orders we get, the unique item IDs cannot be duplicated.
As for the ID, I was considering mixing the product SKU with the date, and possibly the order ID to generate the date. How exactly would I go about this? Is there a plugin that can handle this, or should I deal in straight PHP?

I assume you want to use the unique id for work order tracking internally
use this hook (I am using processing rather then completed)
add_action( 'woocommerce_order_status_processing', 'add_unique_id' );
Then do this
function add_unique_id($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item_id => $product ) {
$gen_id = "generate id goes here";
wc_add_order_item_meta($item_id, 'unique_id', $gen_id);
}

You could use a WooCommerce hook like woocommerce_order_status_completed in order to run something after every purchase.
add_action( 'woocommerce_order_status_completed', 'custom_function' );
function custom_function($order_id) {
// completed order object (can be useful)
$order = new WC_Order( $order_id );
// run what you want here
}
I can suggest you to use a post meta or an user meta (based on your needs).

Related

Run a custom code after a product is sold in woocommerce

I need to know how to assign custom actions (or run custom code) when someone buys something in the website and the payment has been completed. I need this so our system can send order's data to an API.
i am using wordpress and woocommerce for the website.
how can i find the order dynamics variable to put it in my API's code ?
if there,s anyone who use wordpress and woocommerce, please tell me where can i find order's dynamic variables such as:
product id which the customer has been ordered
product count and etc.
You could write a function that connects to the woocommerce_order_status_processing hook. At this point, the payment has been accepted and WooCommerce is waiting for the store to fulfill the order.
add_action( 'woocommerce_order_status_processing', 'my_order_complete_function', 10, 1 );
function my_order_complete_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
// Add your API call here.
}
}

Send customer notification emails for merchandise orders, but not for FooEvents tickets

I am hosting a webshop selling both merchandise and event tickets, using WooCommerce in combination with FooEvents.
I am having issues with the 'customer order completed' emails:
I want to send them for Merchandise items (order ready to pick up)
but do not want to send them for tickets (since these are issued automatically by email)
I tried to conditionally send the email notification based on the product categories, based on the following code:
(taken from this post: Avoid customer email notification for a specific product category in Woocommerce)
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
function product_cat_avoid_processing_email_notification( $recipient, $order ) {
if( is_admin() ) return $recipient;
// HERE set your product categories (coma separated term Ids, slugs or names)
$cat_to_filter = 'tickets';
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get the correct product ID (for variations we take the parent product ID)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Check for product categories for this item
if( has_term( $cat_to_filter, 'product_cat', $product_id ) )
return ''; // If it's found, we return an empty recipient
}
return $recipient;
}
This code works fine for excluding orders with regular products, e.g. I can exclude order completed notifications for the product category 'merchandise'. However, for tickets it does not work and results in an error: a generic error message on checkout page, the order is created but tickets are not generated nor sent.
My hunch is, that the code does not work with FooEvents, either because:
the code deletes the customer email address FooEvents needs to send the tickets, or
the code interferes with FooEvents in the area of changing order statusses to 'completed'
Can you help me out and provide me a fix for this issue?
Thanks,
Jeroen

Display woocommerce 'payment method' in Order Notes section in the Admin Dashboard

I'd like to pass the payment method to the order notes section of a Woocommerce Order (when viewing the order in the backend)
This field gets imported into our ShipStation software. Right now, I have no way of knowing what payment method a customer used when viewing an order from ShipStation. (we give a free gift when a certain payment method is used.)
At first, viewing the payment method in Wordpress Admin is available. You just have to enable it in Screen Options.
If you want to add the Shipment method into the order note, you need to just add this to your functions.php:
add_action( ‘woocommerce_new_order’, ‘add_engraving_notes’, 1, 1 );
function add_engraving_notes( $order_id ) {
$order = new WC_Order( $order_id );
$note = $order->getShippingMethod();;
$order->add_order_note( $note );
$order->save();
}

Auto-complete Paid Orders only for specific product IDs on Woocommerce

Is there any way I can auto complete orders only for specific product IDs on Woocommerce?
I used the code on this thread to auto complete orders.
I also read this thread but it excludes product ids from auto complete. And I am not able to make it work the other way around.
Since I have 20+ products in my shop and I want to use auto complete on only 2 of them, it would be great if I can specify the order ids which I want to auto complete.
Here is a way to autocomplete paid orders for specific product IDS:
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
// Below the targeted product Ids
$targeted_ids = array(37, 53);
// Loop through order line items
foreach( $order->get_items() as $item ) {
if ( in_array( $item->get_product_id(), $targeted_ids ) || in_array( $item->get_variation_id(), $targeted_ids ) ) {
return 'completed';
}
}
return $status;
}
Code goes in functions.php file of the active child theme (or active theme).
WooCommerce: Auto complete paid orders
Exclude specific products on auto-complete orders process in Woocommerce

WordPress Woocommerce post id

In WooCommerce, on view-order page is there a way to get post id of product (or an other column) ?
I tried to display data of $_product and $item_meta but it's displaying the id of the product created in the admin of WordPress, not the id of the current product in the order.
The following code will get you all the details of the order:
<?php
global $woocommerce;
$order = new WC_Order( $order_id );
$order->get_items();
?>
After testing all methods of woocommerce, the only way to do that is to create a custom function that takes in the database the post_title (in wp_posts) that starts with the order id.

Resources