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

时间:2016-05-13 04:10:57

标签: javascript bigcommerce customer

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

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

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

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

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

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

尽管使用BigCommerce webhooks可以确保执行客户组分配应用程序的最高成功率,但它需要在BigCommerce 上进行相当多的设置(创建草稿应用程序,获取oAuth密钥,跳跃插孔等)< / em>,可能对您的要求有点过分。

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

  1. 在BigCommerce控制面板中,将额外字段添加到用户注册表单中,就像您提到的那样。 enter image description here

  2. 正如您所看到的,此新输入字段已本地添加到默认注册页面: enter image description here

  3. 现在,当用户在您的网站上创建帐户时,Signup Code(创建的自定义字段)的值可以通过该客户帐户的API直接访问。看一下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。应在</body>上的结束createaccount_thanks.html标记之前插入此脚本。
    4. 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来保存 mega 开发时间。请记住,您需要在线安装它,然后将其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

答案 1 :(得分:1)

除非你想做一个cron工作,否则你需要设置一个服务器来监听webhook。我们有关于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