Hopefully some legend can point me in the right direction.
I am wanting to group users who use certain coupon codes in woocommerce when they purchase. I realise there are plugins such as woocommerce groups which allow you to restrict coupons to user groups but I need this the other way.
Eg: someone uses a coupon code "cricket" when they purchase and this will then add the user to the group "cricket-users".
Thanks V much!
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'cricket' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id()
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
}
}
}
Related
I tried this code, and it works. But it automatically adds the credit to the person who sells the product. So, is it possible to add automatic credit to the person who buys the product, not the person who sells it? I tried to change this code but failed. It may seem easy to you, but my attempts were unsuccessful. I'm a bit new to this field, please excuse me.
/**
* Authomatically Top-up Multi vendor Woocommerce Seller on Complete order
* #param int $order_id
*/
function izzycart_auto_topup_user_on_product_purchase( $order_id ) {
$order = new WC_Order($order_id);
/**
* You may need order total and customer_id to compose
* Transaction message for top, which is why it is added
* below as optional (if needed)
*/
$total = $order->get_total(); // <=== Total order Price (if needed)
$customer_id = $order->get_customer_id(); // <=== Customer ID (if needed)
$order_items = $order->get_items(); // <=== Grab all items in order
$item_details = [];
foreach ( $order_items as $item ) {
// Get the ID of each item in Order
$product_id = $item->get_product_id();
$item_details[] = [
'product_name' => $item->get_name(),
'total' => $item->get_total(),
'author' => get_post($product_id)->post_author // <=== The product/item author ID
];
}
//Loop through all product/items author and add topup their wallet
foreach ($item_details as $item_detail) {
$wallet = new Woo_Wallet_Wallet();
// Get Administrator's percentage
$adminsCut = 0.2 * $item_detail['total']; // <=== Admin Takes 20%
// Get Author's Percentage
$authorsCut = $item_detail['total'] - (0.2 * $item_detail['total']); // <=== Author Takes 80%
//Top-Up Admin
if( $item_detail['author'] != 1 ) {
/**
* By Default Administrator has an ID of 1
* if Author of product is not Admin
*/
$topUpAdmin = $wallet->credit(1, $adminsCut, "Top-Up cut from {$item_detail['product_name']} sales");
//Top-Up Author of Product
$topUpAuthor = $wallet->credit($item_detail['author'], $authorsCut, "Top-Up from {$item_detail['product_name']} purchase");
}else {
// Author of product is Admin. Give admin all the money
$topUpAdmin = $wallet->credit(1, $item_detail['total'], "Top-Up from {$item_detail['product_name']} sales");
}
}
}
add_action( 'woocommerce_order_status_completed', 'izzycart_auto_topup_user_on_product_purchase', 10, 1 );
the code from
I am developing stock status log feature to collect data of all products stock changes. I am using "woocommerce_product_set_stock" hook to write changes to custom database table. One big issue is that I cannot find out how to get related order to stock change when someone has placed a new order. When stock change is based on order cancel/refund I can get the order id from $_SERVER info but when placed a new order via checkout, there's nothing related to order id.
Customer requires order id information on stock change log so any hints, please?
When order cancelled or pending WooCommerce called this wc_reduce_stock_levels and wc_increase_stock_levels function to handle stock quantity.
You can use woocommerce_reduce_order_stock and woocommerce_restore_order_stock to write changes to your custom database table
function update_in_custom_table( $order ){
$order_id = $order->get_id();
$order = wc_get_order( $order_id );
// We need an order, and a store with stock management to continue.
if ( ! $order || 'yes' !== get_option( 'woocommerce_manage_stock' ) ) {
return;
}
// Loop over all items.
foreach ( $order->get_items() as $item ) {
if ( ! $item->is_type( 'line_item' ) ) {
continue;
}
$product = $item->get_product();
if ( ! $product || ! $product->managing_stock() ) {
continue;
}
$stock_quantity = $product->get_stock_quantity();
}
}
add_action( 'woocommerce_reduce_order_stock', 'update_in_custom_table', 10, 1 );
add_action( 'woocommerce_restore_order_stock', 'update_in_custom_table', 10, 1 );
I need something like this :
It is possible to set that when we have last 5 pcs on stock , some user role (wholesale) cant order that product ?
For example if we set quantity to 6 pcs than wholesale customer can will order only 1 pcs to stay minimum there 5 pcs and if is lower than 5 pcs than wholesale customer cant order than product ?
I don't know if you requirement is feasible or not. But i have a logic (which may or mayn't be the proper way)
get stock quantity and user role on clicking add to cart button
Check if user role is wholesale customer and stock quantity is the required one
if yes then block product from being added to cart
This is the basic implementation of the above logic. I have just checked the code and found that product is not added to cart. You can build on the code to meet your requirements.
function stock_quantity_validation($valid, $product_id, $quantity){
$qty = 7; // Your required stock quantity
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if ( in_array( 'wholesale_customer', $roles ) ) {
$stock = get_post_meta( $product_id, '_stock', true ); // Getting stock quantity of product
if($stock < $qty){
$valid = false;
}
}
}
return $valid;
}
add_filter('woocommerce_add_to_cart_validation','stock_quantity_validation', 10, 3);
I would like to display all customer orders under my account queries to certain role. Tried using the following but only sorting out orders under the email.
How can I do that?
add_filter( 'woocommerce_my_account_my_orders_query',
'custom_my_orders_query' );
function custom_my_orders_query($args){
$user_id = get_current_user_id();
$args['post_status'] = array('wc-processing');
$args['meta_key'] = 'assignee';
$args['meta_value'] = $user_id;
return $args;
}
I new in subscription payments need help regarding to subscription payments. I am using
WC()->cart->add_to_cart( $parent_id );
for adding product to the cart.
How can i add product to cart for monthly or yearly periodically payment.
what parameter should i send ?
WC()->cart->add_to_cart( $parent_id, 'yearly' );
or what ?
The subscription interval is set in the product page.
If you want a subscription to have different intervals and want to choose them grammatically you must create a variable subscription and change the period in each variation.
Then when you add the product to cart you choose what variation are you adding
add_to_cart( $product_id, $quantity, $variation_id);
This is all available parameters of add_to_cart function:
/**
* Add a product to the cart.
*
* #param int $product_id contains the id of the product to add to the cart
* #param int $quantity contains the quantity of the item to add
* #param int $variation_id
* #param array $variation attribute values
* #param array $cart_item_data extra cart item data we want to pass into the item
* #return string|bool $cart_item_key
*/
public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {
[...]
}
See full Docs in the Woocommerce Subscriptions FAQ