2016-05-13 34 views
1

BC支持人员告诉我这是不可能的,但如果真的没有办法,我会感到惊讶。在注册时自动为特定客户群分配客户--Bigcommerce

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

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

我会那么值与预先定义的字符串,如果有比赛,我会分配一个客户groupX(与8组ID):

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

是否有可能分配客户在注册时如何(或以其他方式)注册特定组?

任何帮助,非常感谢。

+0

需要说明的是,当支持告诉你一些事情不能完成时,这意味着没有设置。这可以通过定制完成。它需要您自己的Web服务器,该服务器配置为侦听WebHooks并根据您自己的业务/代码逻辑分配创建的客户。 – Alyss

+0

谢谢Alyssa,你能告诉我这是多么复杂的一个想法,或者可以稍微简单一点的解释它吗?我不是一个完整的小白,但这种类型的项目对我来说有点陌生。注 - BCs服务器设置是否有用? – Tron

+0

有一个错字。我的名字是Alyss,而不是Alyssa :)。 BC服务器设置不被使用。你需要有一个外部服务器或类似heroku的东西。 JavaScript可能会不足。 – Alyss

回答

1

你需要设置一个服务器来监听webhooks,除非你想做一个cron作业。我们有关于developer portal的一些基本信息,但我在下面列出了更多资源。从那里开始,您需要选择您选择的服务器语言,以便在创建完成后监听webhook,正确响应(如果收到响应,则返回200),根据此信息执行代码,然后针对BC API采取措施。

所以,如果你正在寻找一个代码,你需要听取store/customer/created webhook,并让你的代码寻找一个包含代码的自定义字段。如果它存在,然后采取行动。否则,什么也不做。

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

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

How do I receive Github Webhooks in Python

+0

对不起Alyss!并非常感谢这一点。我有一个很难理解,我试图找出什么听一个webhook意味着什么,但我会看看并阅读这些链接,看看我不能学习一些东西。我很欣赏时间 – Tron

2

虽然使用的Bigcommerce网络挂接将确保执行你的客户群分配应用的成功率最高,它需要的Bigcommerce 颇有几分设置的(创建草稿应用程序,获取oAuth密钥,跳跃插孔等),并可能会对您的要求有点矫枉过正。

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

  1. 内的Bigcommerce控制面板,添加一些额外的领域给用户像你提到的报名表格。 enter image description here

  2. 因此,大家可以看到,这个新的输入字段已本地添加到默认的注册页面: enter image description here

所以现在,当用户在网站上创建一个帐户,该可以通过该客户帐户的API直接访问Signup Code(创建的自定义字段)的值。看看这是什么JSON数据是这样的: enter image description here


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

  1. 我们将通过BigCommerce Registration Confirmation页面 - createaccount_thanks.html来触发我们的外部应用程序。该页面在客户创建账户后立即加载,因此它是插入我们的触发器脚本的理想场所。
  2. 此外,现在客户已登录,我们可以通过BigCommerce Global系统变量 - %%GLOBAL_CurrentCustomerEmail%%访问客户的电子邮件地址。
  3. 我们应该从这个页面发送一个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