如何在 woocommerce 付款成功后创建一个 terawallet product_user 信用函数挂钩

How to create a terawallet product_user credit function a hook after successful woocommerce payment

我是 运行 一家 woocommerce wordpress 商店,我集成了一个名为“terawallet aka woo-wallet”的虚拟钱包插件。此附加组件让网站上的每个用户都可以选择为他们的虚拟钱包充值、提取资金或在结帐时将其用作付款方式。

我想创建一个 function/hook 在购买产品时自动为产品作者钱包(post 编辑产品 post 的用户)充值。我使用的是为每个 vendor/seller 设置佣金的市场插件,购买后,管理员获得佣金,卖家获得剩余部分 (total_seller_amount)

例如 用户 1=卖家,用户 2=买家 当买家购买产品时,卖家虚拟账户应在产品订单完成(处理)后自动充值管理佣金后的剩余金额。因此,如果买家购买了属于 4 位不同产品作者的 4 件产品,则每个作者都应相应地记入与其用户 ID 相关联的产品。

woo-wallet class 被称为 '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 );

已经过测试并且可以正常工作

Admin Top-up 交易截图

产品作者Top-up交易截图

WC 版本:v4.5.2
WordPress 版本:v5.5.1
TeraWallet 版本:v1.3.16

此代码应放入您主题的 function.php 文件中