|
|
@@ -44,41 +44,100 @@ class RewardActiveRule extends Model
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
- * 获取客户群组积分设置
|
|
|
+ * 获取特定客户群组的积分值
|
|
|
*/
|
|
|
- public function getCustomerGroupPointsAttribute()
|
|
|
+ public function getRewardPointForCustomerGroup($customerGroupId)
|
|
|
{
|
|
|
if ($this->enable_different_points_by_group) {
|
|
|
- $groupPoints = json_decode($this->customer_group_ids, true);
|
|
|
- return is_array($groupPoints) ? $groupPoints : [];
|
|
|
+ $groupPoints = $this->getCustomerGroupPointsAttribute();
|
|
|
+ return isset($groupPoints[$customerGroupId]) ? (int)$groupPoints[$customerGroupId] : 0;
|
|
|
}
|
|
|
- return [];
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 设置客户群组积分设置
|
|
|
- */
|
|
|
- public function setCustomerGroupPointsAttribute($value)
|
|
|
- {
|
|
|
- if ($this->enable_different_points_by_group) {
|
|
|
- $this->attributes['customer_group_ids'] = json_encode($value);
|
|
|
+ // 如果不启用不同群组不同积分,则检查 customer_group_ids 是否为空或包含当前群组
|
|
|
+ // 如果 customer_group_ids 为空,表示适用于所有群组
|
|
|
+ if (empty($this->customer_group_ids)) {
|
|
|
+ return (int)$this->reward_point;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查当前群组是否在允许的群组列表中
|
|
|
+ $allowedGroups = explode(',', $this->customer_group_ids);
|
|
|
+ if (in_array((string)$customerGroupId, $allowedGroups)) {
|
|
|
+ return (int)$this->reward_point;
|
|
|
}
|
|
|
+
|
|
|
+ // 如果群组不在允许列表中,返回 0(不赠送积分)
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取特定客户群组的积分值
|
|
|
+ * 检查规则是否适用于指定客户
|
|
|
*/
|
|
|
- public function getRewardPointForCustomerGroup($customerGroupId)
|
|
|
+ public function isApplicableToCustomer($customer)
|
|
|
{
|
|
|
+ // 检查 store_view 是否匹配
|
|
|
+ if ($this->store_view && $this->store_view !== '0') {
|
|
|
+ $storeViews = explode(',', $this->store_view);
|
|
|
+ $currentChannelCode = core()->getCurrentChannelCode();
|
|
|
+ if (!in_array($currentChannelCode, $storeViews)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果不启用不同群组不同积分,检查 customer_group_ids
|
|
|
+ if (!$this->enable_different_points_by_group) {
|
|
|
+ // 如果 customer_group_ids 为空或空字符串,表示适用于所有群组
|
|
|
+ if (empty($this->customer_group_ids) || trim($this->customer_group_ids) === '') {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查客户群组是否在允许列表中
|
|
|
+ $allowedGroups = array_map('trim', explode(',', $this->customer_group_ids));
|
|
|
+
|
|
|
+ // 获取客户群组ID
|
|
|
+ if ($customer instanceof \Webkul\Customer\Models\Customer) {
|
|
|
+ $customerGroupId = $customer->customer_group_id;
|
|
|
+ } elseif (is_object($customer) && property_exists($customer, 'customer_group_id')) {
|
|
|
+ $customerGroupId = $customer->customer_group_id;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($customerGroupId === null || $customerGroupId === '') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return in_array((string)$customerGroupId, $allowedGroups);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果启用不同群组不同积分,检查是否有为该群组配置积分
|
|
|
if ($this->enable_different_points_by_group) {
|
|
|
$groupPoints = $this->getCustomerGroupPointsAttribute();
|
|
|
- return isset($groupPoints[$customerGroupId]) ? (int)$groupPoints[$customerGroupId] : 0;
|
|
|
+
|
|
|
+ if (empty($groupPoints)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取客户群组ID
|
|
|
+ if ($customer instanceof \Webkul\Customer\Models\Customer) {
|
|
|
+ $customerGroupId = $customer->customer_group_id;
|
|
|
+ } elseif (is_object($customer) && property_exists($customer, 'customer_group_id')) {
|
|
|
+ $customerGroupId = $customer->customer_group_id;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($customerGroupId === null || $customerGroupId === '') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否为该群组配置了积分(即使为 0 也算配置了)
|
|
|
+ return isset($groupPoints[(string)$customerGroupId]) || isset($groupPoints[$customerGroupId]);
|
|
|
}
|
|
|
|
|
|
- // 如果不启用不同群组不同积分,则返回统一的积分值
|
|
|
- return (int)$this->reward_point;
|
|
|
+ return true;
|
|
|
}
|
|
|
- public function getTransactionTypeTextAttribute()
|
|
|
+
|
|
|
+ public function getTransactionTypeTextAttribute()
|
|
|
{
|
|
|
$types = [
|
|
|
self::TYPE_ORDER => 'Order',
|
|
|
@@ -93,4 +152,5 @@ class RewardActiveRule extends Model
|
|
|
|
|
|
return $types[$this->type_of_transaction] ?? 'Unknown';
|
|
|
}
|
|
|
+
|
|
|
}
|