Terawallet credit function, Auto credit when purchasing - woocommerce

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

Related

Assign a tag programatically for stock status when product is saved in WooCommerce backend

I have created 3 tags 1.
Backorder (id 111)
In stock (id 112),
Out of stock (id 113)
I would like these tags to be auto-assigned based on availability.
If I am able to get this working, I'll be using the woocommerce_admin_process_product_object, woocommerce_product_quick_edit_save, woocommerce_product_set_stock, woocommerce_variation_set_stock to capture the possible scenarios for stock status changes.
Below is what I have tried but it gives me a fatal error. If I do not use the if condition, then the tags get assigned but it does not meet my objective. Any advice?
add_action( 'woocommerce_admin_process_product_object', 'mycode_woocommerce_backorder_tag', 10, 2 );
function mycode_woocommerce_backorder_tag ($wc_get_product, $product) {
if ($product->managing_stock() && $product->is_on_backorder(1)) {
$wc_get_product->set_tag_ids(array(111));
$wc_get_product->save();
//wp_set_object_terms ($post_id, 'onbackorder', 'product_tag');
}
}
woocommerce_admin_process_product_object hook has only 1 argument, which is $product
$product->save(); is also not necessary, because this happens automatically
Copied from admin/meta-boxes/class-wc-meta-box-product-data.php
/**
* Set props before save.
*
* #since 3.0.0
*/
do_action( 'woocommerce_admin_process_product_object', $product );
$product->save();
So to answer your question, you get:
// When product is saved in WooCommerce backend
function action_woocommerce_admin_process_product_object( $product ) {
// managing_stock() - returns whether or not the product is stock managed.
// on_backorder() – check if a product is on backorder.
if ( $product->managing_stock() && $product->is_on_backorder(1) ) {
// Product set tag ids
$product->set_tag_ids( array( 111 ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

Temporarily Remove Cancel Button if Subscription Active less than 3 months

I am in need of some help temporarily removing the cancel button in the "My Subscription" page within "My Account". I would like to hide the cancel button until it has been at least 3 months (or 90days) since the user has subscribed. Where after they have been subscribed for 3 months, the cancel button will appear again.
Using: Woocommerce together with Woo-subscriptions and Woo-memberships
I have found another question where this has been answered, but I cannot seem to get the code to work no matter how much I edit it (Disable Cancel Subscription for single subscription in WooCommerce). The first snippet of code below is from the link.
function sv_edit_my_memberships_actions( $actions ) {
// Get the current active user
$user_id = wp_get_current_user();
if(!$user_id) // No valid user, abort
return $actions;
// Only query active subscriptions
$memberships_info = wc_memberships_get_user_active_memberships($user_id, array(
'status' => array( 'active' ),
));
// Loop through each active subscription
foreach ($memberships_info as $membership) {
$subscription_start_date = date("Y/m/d", strtotime($membership->get_start_date()));
//$subscription_end_date = date("Y/m/d", strtotime($membership->get_end_date()));
//$subscription_name = $membership->get_plan()->get_name();
//$subscription_id = $membership->get_plan()->get_id();
if($subscription_id == 'YOUR_ID') { // Active subscription
// Compare the starting date of the subscription with the current date
$datetime1 = date_create($subscription_start_date);
$datetime2 = date_create(date(time()));
$interval = date_diff($datetime1, $datetime2);
if($interval->format('%m') <= 11) {
// remove the "Cancel" action for members
unset( $actions['cancel'] );
}
}
}
return $actions;
}
I have been able to hide the cancel button with my code below, however it hides it indefinitely:
function remove_cancel_button( $actions, $subscription ) {
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'cancel': // Remove the cancel button
unset( $actions[ $action_key ] );
break;
default:
error_log( '-- $action = ' . print_r( $action, true ) );
break;
}
}
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);
I've read the related developer documentation found here and altered your code so that it uses the current sites date and compares it to the subscription date.
If there is less then a 3 months difference the cancel button will stay hidden until there is a at least a 3 month difference.
Note that I used 'last_payment' date to compare with, other possible options to use are 'start', 'trial_end', 'next_payment', 'last_payment' or 'end'.
Read more about it here.
/**
* Remove cancel button ( When last payment was less then 3 months ago )
*
* #param array $actions, action array.
* #param int $subscription_id, the id of the current subscription.
* #return array $actions, the altered action array.
*/
function remove_cancel_button( $actions, $subscription_id ) {
// Gets the subscription object on subscription id
$subscription = new WC_Subscription( $subscription_id );
// Get last payment date from subscription object, uses the sites timezone setting
$last_payment_date = $subscription->get_date( 'last_payment', 'site' );
$last_payment_date = new DateTime( $last_payment_date );
// The current date/time, uses the sites timezone setting
$today = new DateTime( current_time('mysql') );
// Get the difference in date
$interval = $today->diff( $last_payment_date );
// Check if interval is less then 3 months
if( $interval->m < 3 ){
unset( $actions['cancel'] );
}
// Return the actions
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);
Hopefully this helps you out, let me know if anything is unclear.

Group Woocommerce users by Coupon

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');
*
*
*/
}
}
}

Woocommerce add to cart monly or yearly payment

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

Woocommerce - Extra fee based on product quantity

I found this answer in a earlier post here (below), but I want to know of there is any way to add an extra fee based on quantity if the product quantity changes?
Lets say there is 100 items in one package. (the problem is also that there is not they same amount of item in all packages, some can be 100, some can be 150, 200, or 500)
Example:
1-99 = 1$.
100 = no fee.
101 - 199 1$
200 = no fee
201 - 299 = 1$ and so on..
Total will always be 1$ per product but the total can be more if they order several products that have these breaks. The total can be 4$ if there is 4 products with break-cost.
(Also, not sure where to put the code)
Thank you!
The code I found here: Add additional costs based on quantity in Woocommerce
// Hook before adding fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
/**
* Add custom fee on article specifics
* #param WC_Cart $cart
*/
function add_custom_fees( WC_Cart $cart ){
$fees = 0;
foreach( $cart->get_cart() as $item ){
// Check if odds and if it's the right item
if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], 'custom_fee_for_supplier_name', true) ){
// You can also put a custom price in each produt with get_post_meta
$fees += 10;
}
}
if( $fees != 0 ){
// You can customize the descriptions here
$cart->add_fee( 'Custom fee (odds paquets)', $fees);
}
}
The woocommerce_after_cart_item_quantity_update fires right after a quantity is updated. If you modify your function a little bit (to use WC()->cart to access the cart object) you can run the same function on both hooks. I thought it might keep adding additional fees, but in my testing it seems to just recalculate the right cost for the same fee.
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
add_action( 'woocommerce_after_cart_item_quantity_update', 'add_custom_fees' );
/**
* Add custom fee on article specifics
* #param WC_Cart $cart
*/
function add_custom_fees(){
$fees = 0;
foreach( WC()->cart->get_cart() as $item ){
// Check if odds and if it's the right item
if( $item[ 'quantity' ] % 2 == 1 && get_post_meta( $item[ 'product_id' ], '_custom_fee_for_odds', true ) ){
// You can also put a custom price in each produt with get_post_meta
$fees += 10;
}
}
if( $fees > 0 ){
// You can customize the descriptions here
WC()->cart->add_fee( 'Custom fee (odds paquets)', $fees);
}
}

Resources