Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #101 from classyllama/feature/AVS-421_74-expand-cu…
Browse files Browse the repository at this point in the history
…stomer-attributes-for-customer-code

Feature/avs 421 74 expand customer attributes for customer code
  • Loading branch information
rsisco authored Dec 29, 2017
2 parents 55007cf + 0255996 commit 1ac5106
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
25 changes: 23 additions & 2 deletions Framework/Interaction/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,16 @@ public function getTaxService(
*/
protected function getCustomerCode($data)
{
switch ($this->config->getCustomerCodeFormat($data->getStoreId())) {
// Retrieve the customer code configuration value
$customerCode = $this->config->getCustomerCodeFormat($data->getStoreId());
switch ($customerCode) {
case Config::CUSTOMER_FORMAT_OPTION_EMAIL:
// Use email address
$email = $data->getCustomerEmail();
return $email ?: Config::CUSTOMER_MISSING_EMAIL;
break;
case Config::CUSTOMER_FORMAT_OPTION_NAME_ID:
// Use name and ID
$customer = $this->getCustomerById($data->getCustomerId());
if ($customer && $customer->getId()) {
$name = $customer->getFirstname() . ' ' . $customer->getLastname();
Expand All @@ -349,9 +353,26 @@ protected function getCustomerCode($data)
return sprintf(Config::CUSTOMER_FORMAT_NAME_ID, $name, $id);
break;
case Config::CUSTOMER_FORMAT_OPTION_ID:
default:
// Use customer ID
return $data->getCustomerId() ?: strtolower(Config::CUSTOMER_GUEST_ID) . '-' . $data->getId();
break;
default:
// Use custom customer attribute
if (!$data->getCustomerId()) {
// This is a guest so no attribute value exists and neither does a customer ID
return strtolower(Config::CUSTOMER_GUEST_ID) . '-' . $data->getId();
}
// Retrieve customer by ID
$customer = $this->getCustomerById($data->getCustomerId());
// Retrieve attribute using provided attribute code
$attribute = $customer->getCustomAttribute($customerCode);
if (!is_null($attribute)) {
// Customer has value defined for provided attribute code
return $attribute->getValue();
}
// No value set for provided attribute code, but this is not a guest so use customer ID
return $data->getCustomerId();
break;
}
}

Expand Down
47 changes: 43 additions & 4 deletions Model/Config/Source/CustomerCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,57 @@
namespace ClassyLlama\AvaTax\Model\Config\Source;

use ClassyLlama\AvaTax\Helper\Config;
use Magento\Customer\Model\Customer;

class CustomerCode implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var Customer
*/
protected $customer;

/**
* CustomerCode constructor.
*
* @param Customer $customer
*/
public function __construct(Customer $customer)
{
$this->customer = $customer;
}

/**
* @return array
*/
public function toOptionArray()
{
return [
['value' => Config::CUSTOMER_FORMAT_OPTION_ID, 'label' => __('ID')],
['value' => Config::CUSTOMER_FORMAT_OPTION_EMAIL, 'label' => __('Email')],
['value' => Config::CUSTOMER_FORMAT_OPTION_NAME_ID, 'label' => __('Name (ID)')],
// Define attributes array with default config values to include
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_ID] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_ID, 'label' => __('ID')
];
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_EMAIL] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_EMAIL, 'label' => __('Email')
];
$attributesArray[Config::CUSTOMER_FORMAT_OPTION_NAME_ID] = [
'value' => Config::CUSTOMER_FORMAT_OPTION_NAME_ID, 'label' => __('Name (ID)')
];

// Retrieve all customer attributes
$customerAttributes = $this->customer->getAttributes();
foreach ($customerAttributes as $attribute){
$label = $attribute->getDefaultFrontendLabel();
if (!is_null($label) && $attribute->getIsUserDefined()) {
// Only add custom attribute codes that have a frontend label value defined
$attributesArray[$attribute->getAttributeCode()] = [
'value' => $attribute->getAttributeCode(),
'label' => __($label)
];
}
}

// Sort alphabetically for ease of use
sort($attributesArray);

return $attributesArray;
}
}
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<field id="customer_code_format" translate="label comment" type="select" sortOrder="3010" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Customer Code Format</label>
<source_model>ClassyLlama\AvaTax\Model\Config\Source\CustomerCode</source_model>
<comment>Select a format for customers to be identified in AvaTax. Once the integration is live, this setting should not be changed.</comment>
<comment><![CDATA[Select a format for customers to be identified in AvaTax. Available options will include any <em>custom</em> customer attributes that have been created in Magento. If the customer doesn't have a value defined for the selected attribute when their transaction is processed and sent to Avalara, their customer (or guest) ID will be used instead. Once the integration is live, this setting should not be changed.]]></comment>
<depends>
<field id="enabled">1</field>
</depends>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<queue_failed_lifetime>180</queue_failed_lifetime>
<queue_admin_notification_enabled>1</queue_admin_notification_enabled>
<queue_failure_notification_enabled>1</queue_failure_notification_enabled>
<customer_code_format>id</customer_code_format>
</avatax>
</tax>
</default>
Expand Down

0 comments on commit 1ac5106

Please sign in to comment.