使用GravityForms和WooCommerce产品供应商自动创建供应商

时间:2015-02-06 16:37:24

标签: php wordpress woocommerce

我的网站使用以下插件运行WordPress:

我需要做的是每当有人通过特定的GravityForm注册时(有两个 - 不需要付款而另一个需要付款),创建一个用户并且该用户被赋予“供应商”状态他们可以发布和管理自己的产品。使用GravityForms用户注册插件创建用户很简单,但由于插件期望每个供应商都是手动创建的,因此产品供应商的东西会分崩离析...供应商插件不会以传统方式使用角色或功能,所以它很棘手提出解决方案。

通过一些研究,我发现以下代码片段声称它可以创建用户创建的新产品供应商:

**
* Create a vendor on account registration.
*
* @param int $customer_id
* @param array $new_customer_data
* @return boid
*/
function wc_create_vendor_on_registration( $customer_id, $new_customer_data ) {
$username = $new_customer_data['user_login'];
$email = $new_customer_data['user_email'];
// Ensure vendor name is unique
if ( term_exists( $username, 'shop_vendor' ) ) {
$append = 1;
$o_username = $username;
while ( term_exists( $username, 'shop_vendor' ) ) {
$username = $o_username . $append;
$append ++;
}
}
// Create the new vendor
$return = wp_insert_term(
$username,
'shop_vendor',
array(
'description' => sprintf( __( 'The vendor %s', 'localization-domain' ), $username ),
'slug' => sanitize_title( $username )
)
);
if ( is_wp_error( $return ) ) {
wc_add_notice( __( '<strong>ERROR</strong>: Unable to create the vendor account for this user. Please contact the administrator to register your account.', 'localization-domain' ), 'error' );
} else {
// Update vendor data
$vendor_data['paypal_email'] = $email; // The email used for the account will be used for the payments
$vendor_data['commission'] = '50'; // The commission is 50% for each order
$vendor_data['admins'][] = $customer_id; // The registered account is also the admin of the vendor
update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
}
}
add_action( 'woocommerce_created_customer', 'wc_create_vendor_on_registration', 10, 2 ); 

这可以单独使用,但我不希望每次用户注册时都会触发它,只有当我的GravityForm完成时才会触发。

我需要做的是将它与GravityForm的gform_after_submission钩子组合并改变它,以便从提交的表单中提取值: http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/actions/gform_after_submission/

这是我提出的问题,但它不起作用,我不确定原因:

add_action("gform_after_submission_2", "wc_create_vendor_on_registration", 10, 2);
function wc_create_vendor_on_registration( $customer_id, $new_customer_data ) {
    $username = $entry["12"];
    $email    = $entry["8.1"]; 

    // Ensure vendor name is unique
    if ( term_exists( $username, 'shop_vendor' ) ) {
        $append     = 1;
        $o_username = $username;

        while ( term_exists( $username, 'shop_vendor' ) ) {
            $username = $o_username . $append;
            $append ++;
        }
    }

    // Create the new vendor
    $return = wp_insert_term(
        $username,
        'shop_vendor',
        array(
            'description' => sprintf( __( 'The vendor %s', 'localization-domain' ), $username ),
            'slug'        => sanitize_title( $username )
        )
    );

    if ( is_wp_error( $return ) ) {
        wc_add_notice( __( '<strong>ERROR</strong>: Unable to create the vendor account for this user. Please contact the administrator to register your account.', 'localization-domain' ), 'error' );
    } else {
        // Update vendor data
        $vendor_data['paypal_email'] = $entry["9.1"];  // The email used for the account will be used for the payments
        $vendor_data['commission']   = '50'; // The commission is 50% for each order
        $vendor_data['admins'][]     = $customer_id; // The registered account is also the admin of the vendor

        update_option( 'shop_vendor_' . $return['term_id'], $vendor_data );
    }
}
add_action( 'woocommerce_created_customer', 'wc_create_vendor_on_registration', 10, 2 );
}

有什么想法吗?

0 个答案:

没有答案