虽然使用 BigCommerce webhook 可以确保执行客户组分配应用程序的最高成功率,但它需要在 BigCommerce 上进行大量设置(创建草稿应用程序、获取 oAuth 密钥、跳跃插孔等),并且可能有点过分满足您的要求。
这是一种更简单的方法,在我{大部分}的拙见中,它利用了您在原始问题中包含的大部分内容。尽管如此,任何解决方案都需要外部服务器来通过 BigCommerce API 处理客户组分配。
在 BigCommerce 控制面板中,将额外字段添加到您提到的用户注册表单中。

如您所见,这个新的输入字段已本地添加到默认注册页面:

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

好的,这很好,但是我们如何自动化呢?
为此,我们必须让我们的外部应用程序知道客户刚刚注册。此外,我们的外部应用程序需要对这个新创建的客户进行某种引用,以便它知道要为哪个客户更新客户组。通常 BigCommerce webhook 会通知我们所有这些,但由于我们没有使用 BigCommerce webhook,所以这里是触发外部脚本的替代方法。
- 我们将通过 BigCommerce 注册确认页面触发我们的外部应用程序 -
createaccount_thanks.html。此页面在客户创建帐户后立即加载,因此它是插入触发脚本的理想场所。
- 此外,现在客户已登录,我们可以通过 BigCommerce Global 系统变量访问客户的电子邮件地址 -
%%GLOBAL_CurrentCustomerEmail%%。
- 我们应该从这个页面向我们的外部应用程序发出一个 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 脚本,因为上面的质量确实与功能性伪代码类似,但更多的是为了展示总体思路。高温高压