2016-05-13 51 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