1

BC 支持告诉我这是不可能的,但如果真的没有办法,我会感到惊讶。

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

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

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

 if ( codeInput === "codeIGaveToTheUser" ) {
    currentUserGroupID = 8;
 }

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

任何帮助深表感谢。

4

2 回答 2

5

虽然使用 BigCommerce webhook 可以确保执行客户组分配应用程序的最高成功率,但它需要在 BigCommerce 上进行大量设置(创建草稿应用程序、获取 oAuth 密钥、跳跃插孔等),并且可能有点过分满足您的要求。

这是一种更简单的方法,在我{大部分}的拙见中,它利用了您在原始问题中包含的大部分内容。尽管如此,任何解决方案都需要外部服务器来通过 BigCommerce API 处理客户组分配。

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

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

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


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

  1. 我们将通过 BigCommerce 注册确认页面触发我们的外部应用程序 - 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>

现在最后,您只需要创建负责处理上述请求并更新客户的客户组的服务器端应用程序。您可以使用任何您想要的语言,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;
    }
}

因此,您将直接在服务器端程序中进行客户组字符串比较。我建议您重写您自己的 BC API 脚本,因为上面的质量确实与功能性伪代码类似,但更多的是为了展示总体思路。高温高压

于 2016-05-15T14:57:09.970 回答
2

除非你想做一个 cron 工作,否则你需要设置一个服务器来监听 webhook。我们在开发者门户上有一些基本信息,但我在下面提供了更多资源。从那里,您需要选择您选择的服务器语言,以便在创建 webhook 后侦听它们,正确响应(如果收到 200 响应),根据此信息执行代码,然后对 BC API 采取行动。

因此,如果您要查找代码,则需要侦听store/customer/createdwebhook,并让您的代码查找包含该代码的自定义字段。如果存在,则采取行动。否则,什么也不做。

https://developer.github.com/webhooks/configuring/

http://coconut.co/how-to-create-webhooks

如何在 Python 中接收 Github Webhooks

于 2016-05-13T05:23:00.443 回答