在注册时自动将客户分配给特定客户群 - Bigcommerce

Tro*_*ron 1 javascript bigcommerce customer

不列颠哥伦比亚大学的支持告诉我,这是不可能的,但如果真的没办法,我会感到惊讶.

我需要能够在创建帐户时自动将客户分配给特定客户组.我的想法:

  • 我会在注册表单中添加一个额外的字段
  • 为用户提供代码(字符串或数字)
  • 用户在创建新帐户时输入代码
  • 用户点击提交
  • 在表单提交上,我会获取额外字段的值:
var codeInput = document.getElementById('code-input').value;
Run Code Online (Sandbox Code Playgroud)

然后我会将该值与预定义的字符串进行比较,如果匹配,我会将该客户分配给groupX(组ID为8):

 if ( codeInput === "codeIGaveToTheUser" ) {
    currentUserGroupID = 8;
 }
Run Code Online (Sandbox Code Playgroud)

可以像这样(或任何其他方式)在注册时将客户分配给特定的组吗?

任何帮助深表感谢.

小智 5

虽然使用BigCommerce webhooks可以确保执行客户组分配应用程序的最高成功率,但它需要在BigCommerce上进行相当多的设置(创建草稿应用程序,获取oAuth密钥,跳跃插孔等),并且可能有点根据您的要求矫枉过正.

在我的{大多数}谦虚的观点中,这是一种更简单的方法,它利用了原始问题中包含的大部分内容.但是,任何解决方案都需要外部服务器来通过BigCommerce API处理客户组分配.

  1. 在BigCommerce控制面板中,将额外字段添加到用户注册表单中,就像您提到的那样. 在此输入图像描述

  2. 如您所见,此新输入字段已本地添加到默认注册页面: 在此输入图像描述

现在,当用户在您的网站上创建帐户时,Signup Code可以通过该客户帐户的API直接访问(创建的自定义字段)的值.看一下JSON数据的样子: 在此输入图像描述


好的,所以这很好,但我们如何实现自动化呢?
为此,我们必须让我们的外部应用程序知道刚刚注册的客户.此外,我们的外部应用程序需要对这个新创建的客户进行某种引用,以便它知道更新客户组的客户.通常,BigCommerce webhook会通知我们所有这些,但由于我们没有使用BigCommerce webhook,这里是触发外部脚本的替代方法.

  1. 我们将通过BigCommerce Registration Confirmation页面触发我们的外部应用程序 - createaccount_thanks.html.客户创建帐户后立即加载此页面,因此它是插入触发器脚本的理想位置.
  2. 此外,现在客户已登录,我们可以通过BigCommerce Global系统变量访问客户的电子邮件地址 - %%GLOBAL_CurrentCustomerEmail%%.
  3. 我们应该从此页面向我们的外部应用程序发出HTTP请求以及客户的电子邮件地址.具体来说,我们可以通过JavaScript创建XMLHttpRequest,或者是现代的,我们将通过jQuery使用Ajax.应该在结束</body>标记之前插入此脚本createaccount_thanks.html.

POST请求的示例(尽管GET也足够):

<script>
  $(function() {
    $('.TitleHeading').text('One moment, we are finalizing your account. Please wait.').next().hide(); // Let the customer know they should wait a second before leaving this page.
    //** Configure and Execute the HTTP POST Request! **//
    $.ajax({
      url:         'the_url_to_your_script.com/script.php',
      type:        'POST',
      contentType: 'application/json',
      data:         JSON.stringify({email:"%%GLOBAL_CurrentCustomerEmail%%"}),
      success: function() {
        // If the customer group assignment goes well, display page and proceed normally. This callback is only called if your script returns a 200 status code.
        $('.TitleHeading').text('%%LNG_CreateAccountThanks%%').next().show();
      },
      error:   function() {
        // If the customer group assignment failed, you might want to tell your customer to contact you. This callback is called if your script returns any status except 200.
        $('.TitleHeading').text('There was a problem creating your account').after('Please contact us at +1-123-456-7890 so that we can look into the matter. Please feel free to continue shopping in the meantime.');            
      }                      
    });
  });
</script>
Run Code Online (Sandbox Code Playgroud)

最后,您只需要创建负责处理上述请求的服务器端应用程序,并更新客户的客户组.您可以使用任何你想要的语言,甚至的Bigcommerce 提供了几个SDK的你可以用它来保存大型的开发时间.请记住,您需要在线处理它,然后将其URL插入上面的JS脚本.

PHP示例(快速和脏):

git clone https://github.com/bigcommerce/bigcommerce-api-php.git
curl -sS https://getcomposer.org/installer | php && php composer.phar install

<?php
/**
 * StackOverflow/BigCommerce :: Set Customer Group Example
 * http://stackoverflow.com/questions/37201106/
 * 
 * Automatically assigning a customer group.
 */

//--------------MAIN------------------------//

// Load Dependencies:
require ('bigcommerce-api-php/vendor/autoload.php');
use Bigcommerce\Api\Client as bc;

// Define BigCommerce API Credentials:
define('BC_PATH', 'https://store-abc123.mybigcommerce.com');
define('BC_USER', 'user');
define('BC_PASS', 'token');

// Load & Parse the Email From the Request Body;
$email = json_decode(file_get_contents('php://input'))->email;

// Execute Script if API Connection Good & Email Set:
if ($email && setConnection()) {
    $customer = bc::getCollection('/customers?email=' .$email)[0];        //Load customer by email
    $cgid     = determineCustomerGroup($customer->form_fields[0]->value); //Determine the relevant customer group ID, via your own set string comparisons. 
    bc::updateCustomer($customer->id, array('customer_group_id' => $cgid)) ? http_send_status(200) : http_send_status(500); //Update the customer group. 
} else {
    http_send_status(500);
    exit;
}

//-------------------------------------------------//

/**
 * Sets & tests the API connection.
 * @return bool true if the connection successful.
 */
function setConnection() {
    try {
        bc::configure(array(
        'store_url' => BC_PATH,
        'username'  => BC_USER,
        'api_key'   => BC_PASS
        ));
    } catch (Exception $e) {
        return false;
    }
    return bc::getResource('/time') ? true : false; //Test Connection
}

/**
 * Hard define the customer group & signup code associations here.
 * @param string The code user used at signup.
 * @return int   The associated customergroup ID. 
 */
function determineCustomerGroup($signupCode) {
    switch ($signupCode) {
        case 'test123':
            return 1;
        case 'codeIGaveToTheUser':
            return 8;
        default:
            return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么您可以直接在服务器端程序中进行客户组字符串比较.我建议你重写自己的BC API脚本,因为上面提到的质量实际上是功能性伪代码的一部分,但更多的是用来表示一般的想法.HTH