自定义插件WooCommerce


Custom Plugin for WooCommerce

我已经创建了一个插件来结合WooCommerce订阅&WooCommerce账户基金的目标是,当会员升级为"黄金会员"时,他们将获得40英镑的积分,并且只要他们的订阅有效,他们将每28天获得一次积分。

我的问题是,只要有人把一个标准的订单,他们得到记入£40!我不确定为什么会发生这种情况!而且我不能让它每28天再发生一次。这是我的代码。

<?php
/*
Plugin Name: WooCommerce Subscriptions Funds
Plugin URI: 
Description: Combined WooCommerce Subscriptions & WooCommerce Funds
Version: 1.0
Author: Brad Houston
Author URI: 
License: GPL
*/
/**
 * Required functions
 */
if ( ! function_exists( 'woothemes_queue_update' ) || ! function_exists( 'is_woocommerce_active' ) ) {
    require_once( 'woo-includes/woo-functions.php' );
}
/**
 * Check if WooCommerce is active, and if it isn't, disable Subscriptions.
 *
 * @since 1.0
 */
if ( ! is_woocommerce_active() || version_compare( get_option( 'woocommerce_db_version' ), '2.1', '<' ) ) {
    add_action( 'admin_notices', 'WC_Subscriptions::woocommerce_inactive_notice' );
    return;
}
class WC_Subscriptions_Funds
{
    /**
     * Bootstraps the class and hooks required actions & filters.
     *
     * @since 1.0
     */
    public static function init(){
        // Check if we want to create the order ourself (a renewal order)
        add_filter( 'woocommerce_create_order', __CLASS__ . '::filter_woocommerce_create_order', 10, 2 );
    }
    public static function filter_woocommerce_create_order( $order_id, $checkout_object ) {
        global $woocommerce;
        $customer_id = get_current_user_id();
        WC_Account_Funds::add_funds( $customer_id, 40 );
        //var_dump($woocommerce->cart->cart_contents);exit;
        return $order_id;
    }
}
WC_Subscriptions_Funds::init();
?>

woocommerce_create_order钩子并不意味着订单成功或支付,它只是被添加到数据库中。

你要做的是检查订单是否完成。

有一堆钩子用于订单状态更改(cf http://docs.woothemes.com/wc-apidocs/hook-docs.html和http://docs.woothemes.com/wc-apidocs/source-class-WC_Abstract_Order.html#2138)

因此,查看这些文档,并检查已完成的状态钩子,您可能想要响应woocommerce_order_status_completed

另外,get_current_user_id();是获得订单客户的错误方式。如果您使用的是银行转账支付,"当前用户id"可能是将订单标记为已完成的管理员,而不是客户。我认为 WC_Order有get_user_id()get_user()方法来做到这一点-检查文档或源代码。