Reputation: 23
i'm running a woocommerce wordpress store and i've integrated a virtual wallet add-on called "terawallet aka woo-wallet". this add-on gives each user on the site the option to top-up their virtual wallet, withdraw funds or use it as a payment method during checkout.
i would like to create a function/hook that automatically tops-up the product author wallet(user who posted the product post) when a product is purchased. I'm using a marketplace plugin that sets commission for each vendor/seller and after purchase, admin gets a commision and seller gets the remainder (total_seller_amount)
so e.g USER 1=seller and user 2=buyer when buyer purchases a product, the seller virtual account should automatically be topped up with the amount remaining after admin commission when product order is completed(processed). so if the buyer purchases 4 products that belong to 4 different product authors, each author should be credited accordingly to the product linked to their user ID.
the woo-wallet class is called 'woo_wallet_wallet'
below is the function that credits users wallet:
/**
* Create wallet payment credit transaction
* @param int $user_id
* @param float $amount
* @param string $details
* @return int transaction id
*/
public function credit( $user_id = '', $amount = 0, $details = '' ) {
$this->set_user_id( $user_id );
return $this->recode_transaction( $amount, 'credit', $details );
}
I tried the following code:
$wallet = woo_wallet_wallet ();
$wallet ->credit($product_user_id,, (total_seller_amount), ‘credit’,)
Upvotes: 1
Views: 3013
Reputation: 1052
You will have to do something like this:
/**
* 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 );
TESTED AND WORKING
Admin Top-up Transaction screenshot
Author of product Top-up Transaction screenshot
WC Version: v4.5.2
Wordpress Version: v5.5.1
TeraWallet Version: v1.3.16
This code should go in your theme's function.php file
Upvotes: 2