Is it possible to automatically copy the value of a Customer's custom field to an Order's custom field when this customer places the order?
Should it be done using any plugin/extension or thru customized coding behind the scenes?
This custom field does not need to be displayed on customer order view. We just need it to distinguish whether the order was placed by Consumer or Wholesale when we get it thru API.
I'm totally new in this system, i did a lot of research but couldn't find any direction for this.
Any advice/suggestion would be very appreciated.
You can use woocommerce_thankyou hook to add this user data to the order meta data:
add_action( 'woocommerce_thankyou', 'orders_from_processing_to_pending', 10, 1 );
function orders_from_processing_to_pending( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
$user_id = get_current_user_id();
//Set HERE the meta key of your custom user field
$user_meta_key = 'some_meta_key';
// Get here the user custom field (meta data) value
$user_meta_value = get_user_meta($user_id, $user_meta_key, true);
if ( ! empty($user_meta_value) )
update_post_meta($order_id, $user_meta_key, $user_meta_value);
else
return;
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
This code is tested and works.
After, if you want to display that value on admin edit order backend or in frontend customer view order and emails notifications, you will have to use some more code and some other hooks…
Related
I need to create a function that looks for all the new orders entered in woocommerce and then just update this order without changing anything. (like if you clicked on the order and then clicked update with everything remaining the same)
The reason for this is that I'm bringing orders from another source into woocommerce but the plugin to export them to another platform isn't listening for these external orders until they are updated even if nothing changes.
I know I have to use the woocommerce_new_order hook to listen for the new orders coming in but how can I just update it without changing anything?
function custom_woocommerce_order($order_id) {
if (!$order_id) {
return;
}
//Will this work for what I need?
$order_id = $order->save();
//Or should I do this instead?
$order = wc_get_order($order_id);
do_action( 'woocommerce_update_order', $order );
}
add_action('woocommerce_new_order', 'custom_woocommerce_order');
My customer wants to enable for some specific customers to pay afterwards via invoice.
So i've added an extra field in the user profiles, where he can select yes/no.
This field works fine and saves properly.
Depending on the choice:
no = default webshop behavior and customer needs to pay direct.
yes = customer can order items without paying, he'll get an invoice later on.
Also tried different payment methods, but they are available for everybody, i only want them for specific users.
Now i've tried based on that field in the functions.php to add a conditional filter like so:
if (esc_attr( get_the_author_meta( 'directbetalen', $user->ID ) ) == 'no') {
add_filter('woocommerce_cart_needs_payment', '__return_false');
}
But it doesn't seem to work?
I want payment to be skipped when the field is set to no.
Else proceed as normal and customer needs to pay.
Using WordPress get_user_meta() function, try the following :
add_filter( 'woocommerce_cart_needs_payment', 'disable_payment_for_specific_users' );
function show_specific_payment_method_for_specific_users( $needs_payment ) {
if ( get_user_meta( get_current_user_id(), 'directbetalen', true ) === 'no' ) {
$needs_payment = false;
}
return $needs_payment;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.
I'm currently using a Woocommerce session to save information that the user inputs on the cart page which affects a fee added to the transaction.
I need to be able to access this information right after the order has been completed to make necessary updates to the user's account.
I figured woocommerce_thankyou would be a good hook to use, but unfortunately the session only seems to be available half of the time.
Are there any better hooks to use where I could confirm that the purchase had been completed and the session information would be available?
You need to save that session data as custom order meta data, to be able to use it afterwards (replace my_key, in the code below, with the correct session key):
// Add custom order meta data with temporary data from WC_Session
add_action( 'woocommerce_checkout_create_order', 'add_session_data_as_custom_order_meta_data', 10, 2 );
function add_session_data_as_custom_order_meta_data( $order, $data ) {
if ( $session_data = WC()->session->get('my_key') ) {
$order->update_meta_data( '_session_data', $session_data );
}
}
Code goes on function.php file of your active child theme (or theme). Tested and works.
Then to access the data you will use th WC_Data method get_meta() on the WC_Order Object:
$session_data = $order->get_meta('_session_data');
Or also using get_post_meta() function from a defined order Id:
$session_data = get_post_meta( $order_id, '_session_data', true );
I've made a Woocommerce giftshop for a client.
Their customers are other business that let their employees select their gift through the webshop. Each business have 1 login that all employees use.
As of right now, every user is only allowed 1 item in their cart.
If another product is selected, it will overwrite the previous.
Today I was informed that they wish to expand so it will be possible for select users/user roles to have more than 1 product in their cart and "purchase" them.
Money transactions are not handled directly on the webshop, so purchasing the products sends a list to my client and they take it from there
The current code I use to impose this limit is the following:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_only_one_in_cart', 99, 2 );
function custom_only_one_in_cart( $passed, $added_product_id ) {
// empty cart first: new item will replace previous
wc_empty_cart();
// display a message if you like
wc_add_notice( 'Max number of items in cart reached!', 'notice' );
return $passed;
}
So I'm looking for ideas on how to implement this on specific users or user roles, so the end result will be that most users can only pick one, while a few select user can pick more.
I've already been looking around a lot for a suitable solution, but I haven't been able to find one as of yet.
The solution doesn't have to incorporate the code I provided, either in its current state or in a variation of it, all suitable solutions are welcome.
Any help is appreciated.
In the following code will restrict add to cart to only one item based on defined allowed user roles:
add_filter( 'woocommerce_add_to_cart_validation', 'user_roles_only_one_in_cart', 50, 3 );
function user_roles_only_one_in_cart( $passed, $product_id, $quantity ) {
// HERE define the User roles that are allowed to buy multiple items:
$allowed_user_roles = array('special_customer','administrator', 'shop_manager');
$user = wp_get_current_user();
if( array_intersect( $allowed_user_roles, $user->roles ) )
return $passed;
// Check if cart is empty
if( ! WC()->cart->is_empty() ){
// display an error notice
wc_add_notice( __("Only one item in cart is allowed!", "woocommerce"), "error" );
// Avoid add to cart
$passed = false;
}
return $passed;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
For user roles creation and management you can use User Role Editor plugin (for example).
I am currently working on a WordPress project using WooCommerce and I need a really specific feature (not included in WooCommerce):
How to increase stock when an order is complete instead of decreasing it ?
What I have found so far is that I might need to use Woocommerce API in order to accomplish that WC_AJAX::increase_order_item_stock();. Nevertheless I am not really comfortable using complex PHP...
Do you have some lines of thinking to accomplish this?
Maybe using a plugin (which I did not find)? Or with raw code?
To sum up everything: I want to build a website for a restaurant with an inventory management and the possibility for cooks to order goods from different suppliers. So when a cook order something from the woocommerce shop page, purchased items's inventory have to increase and not decrease.
I have tried different things like 'WC Vendors' or 'Marketplace' but without success…
Thanks.
You could try This custom function hooked in woocommerce_order_status_completed action hook, that will increase back each product stock with the items quantity of this order when status is set to completed:
add_action( 'woocommerce_order_status_completed', 'action_on_order_completed' , 10, 1 );
function action_on_order_completed( $order_id )
{
// Get an instance of the order object
$order = wc_get_order( $order_id );
// Iterating though each order items
foreach ( $order->get_items() as $item_id => $item_values ) {
// Item quantity
$item_qty = $item_values['qty'];
// getting the product ID (Simple and variable products)
$product_id = $item_values['variation_id'];
if( $product_id == 0 || empty($product_id) ) $product_id = $item_values['product_id'];
// Get an instance of the product object
$product = wc_get_product( $product_id );
// Get the stock quantity of the product
$product_stock = $product->get_stock_quantity();
// Increase back the stock quantity
wc_update_product_stock( $product, $item_qty, 'increase' );
}
}
The code works with simple or variables products that have their own stock management enabled. So may be you might need to make some changes on it, depending on your WooCommerce settings. This is just an example that gives you an idea, a way…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code don't throw errors on WooCommerce version 2.6.x, and should work.