问题描述
我正在经营woocommerce wordpress商店,并且集成了一个名为“ terawallet aka woo-wallet”的虚拟钱包附加组件。该插件使网站上的每个用户都可以选择充值他们的虚拟钱包,提取资金或在结帐时将其用作付款方式。
我想创建一个功能/挂钩,当购买产品时,该功能/挂钩会自动为产品作者钱包(张贴产品信息的用户)充值。我正在使用一个市场插件,该插件可为每个卖方/卖方设置佣金,购买后,管理员将获得佣金,卖方获得剩余金额(总计_卖方_金额)
例如 USER 1 =卖方,user 2 =买方 当买方购买产品时,卖方虚拟帐户应自动充值,并在完成产品订单(处理)后进行管理员委托后剩余的金额。因此,如果购买者购买了属于4个不同产品作者的4种产品,则应将每个作者相应地记入与他们的用户ID关联的产品的贷方。
woo-wallet类称为“ woo_wallet_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 );
}
我尝试了以下代码:
$wallet = woo_wallet_wallet ();
$wallet ->credit($product_user_id,(total_seller_amount),‘credit’,)
解决方法
您将必须执行以下操作:
/**
* 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 );
测试并工作
WC版本:v4.5.2
WordPress版本:v5.5.1
TeraWallet版本:v1.3.16
此代码应放在主题的function.php文件中