虽然使用的Bigcommerce网络挂接将确保执行你的客户群分配应用的成功率最高,它需要的Bigcommerce 颇有几分设置的(创建草稿应用程序,获取oAuth密钥,跳跃插孔等),并可能会对您的要求有点矫枉过正。
在我的{大部分}卑微的意见中,这是一个更简单的方法,它利用了您原来问题中包含的大部分内容。 尽管如此,任何解决方案都需要外部服务器通过BigCommerce API处理客户组分配。
内的Bigcommerce控制面板,添加一些额外的领域给用户像你提到的报名表格。
因此,大家可以看到,这个新的输入字段已本地添加到默认的注册页面:
所以现在,当用户在网站上创建一个帐户,该可以通过该客户帐户的API直接访问Signup Code
(创建的自定义字段)的值。看看这是什么JSON数据是这样的:
好了,所以这是很好的和所有,但是我们如何实现自动化吗?
为此,我们必须让我们的外部应用程序知道刚刚注册的客户。此外,我们的外部应用程序需要对此新创建的客户提供某种参考,以便它知道要更新客户组的哪个客户。通常BigCommerce webhook会通知我们所有这些,但由于我们没有使用BigCommerce webhook,因此触发外部脚本的替代方法如下。
- 我们将通过BigCommerce Registration Confirmation页面 -
createaccount_thanks.html
来触发我们的外部应用程序。该页面在客户创建账户后立即加载,因此它是插入我们的触发器脚本的理想场所。
- 此外,现在客户已登录,我们可以通过BigCommerce Global系统变量 -
%%GLOBAL_CurrentCustomerEmail%%
访问客户的电子邮件地址。
- 我们应该从这个页面发送一个HTTP请求到我们的外部应用程序以及客户的电子邮件地址。具体来说,我们可以通过JavaScript创建XMLHttpRequest,或者现代化,我们将通过jQuery使用Ajax。该脚本应在
createaccount_thanks.html
关闭</body>
标记之前插入。 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甚至可以使用offers several SDK's来节省百万开发时间。请记住,您需要将其托管在某个位置,然后将其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脚本,因为质量上面的脚本实际上是沿着函数伪代码的一些东西,但更多的是表示总体思路。 HTH
需要说明的是,当支持告诉你一些事情不能完成时,这意味着没有设置。这可以通过定制完成。它需要您自己的Web服务器,该服务器配置为侦听WebHooks并根据您自己的业务/代码逻辑分配创建的客户。 – Alyss
谢谢Alyssa,你能告诉我这是多么复杂的一个想法,或者可以稍微简单一点的解释它吗?我不是一个完整的小白,但这种类型的项目对我来说有点陌生。注 - BCs服务器设置是否有用? – Tron
有一个错字。我的名字是Alyss,而不是Alyssa :)。 BC服务器设置不被使用。你需要有一个外部服务器或类似heroku的东西。 JavaScript可能会不足。 – Alyss