雖然使用的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