Improved cart rule package code style
This commit is contained in:
parent
a72401dbba
commit
9c78f3b4db
|
|
@ -107,9 +107,10 @@ class CartRule
|
|||
foreach ($cart->items()->get() as $item) {
|
||||
$this->process($item);
|
||||
|
||||
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated())
|
||||
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated()) {
|
||||
$this->devideDiscount($item);
|
||||
}
|
||||
}
|
||||
|
||||
$this->processShippingDiscount($cart);
|
||||
|
||||
|
|
@ -127,17 +128,19 @@ class CartRule
|
|||
{
|
||||
static $cartRules;
|
||||
|
||||
if ($cartRules)
|
||||
if ($cartRules) {
|
||||
return $cartRules;
|
||||
}
|
||||
|
||||
$customerGroupId = null;
|
||||
|
||||
if (Cart::getCurrentCustomer()->check()) {
|
||||
$customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id;
|
||||
} else {
|
||||
if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest'))
|
||||
if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest')) {
|
||||
$customerGroupId = $customerGuestGroup->id;
|
||||
}
|
||||
}
|
||||
|
||||
$cartRules = $this->cartRuleRepository->scopeQuery(function($query) use ($customerGroupId) {
|
||||
return $query->leftJoin('cart_rule_customer_groups', 'cart_rules.id', '=', 'cart_rule_customer_groups.cart_rule_id')
|
||||
|
|
@ -174,8 +177,9 @@ class CartRule
|
|||
]);
|
||||
|
||||
if ($coupon) {
|
||||
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit)
|
||||
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cart->customer_id && $coupon->usage_per_customer) {
|
||||
$couponUsage = $this->cartRuleCouponUsageRepository->findOneWhere([
|
||||
|
|
@ -183,9 +187,10 @@ class CartRule
|
|||
'customer_id' => $cart->customer_id
|
||||
]);
|
||||
|
||||
if ($couponUsage && $couponUsage->times_used >= $coupon->usage_per_customer)
|
||||
if ($couponUsage && $couponUsage->times_used >= $coupon->usage_per_customer) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -200,9 +205,10 @@ class CartRule
|
|||
'customer_id' => $cart->customer_id,
|
||||
]);
|
||||
|
||||
if ($ruleCustomer && $ruleCustomer->times_used >= $rule->usage_per_customer)
|
||||
if ($ruleCustomer && $ruleCustomer->times_used >= $rule->usage_per_customer) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -226,11 +232,13 @@ class CartRule
|
|||
$appliedRuleIds = [];
|
||||
|
||||
foreach ($this->getCartRules() as $rule) {
|
||||
if (! $this->canProcessRule($rule))
|
||||
if (! $this->canProcessRule($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->validator->validate($rule, $item))
|
||||
if (! $this->validator->validate($rule, $item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
|
||||
|
||||
|
|
@ -279,8 +287,9 @@ class CartRule
|
|||
break;
|
||||
|
||||
case 'buy_x_get_y':
|
||||
if (! $rule->discount_step || $rule->discount_amount > $rule->discount_step)
|
||||
if (! $rule->discount_step || $rule->discount_amount > $rule->discount_step) {
|
||||
break;
|
||||
}
|
||||
|
||||
$buyAndDiscountQty = $rule->discount_step + $rule->discount_amount;
|
||||
|
||||
|
|
@ -290,8 +299,9 @@ class CartRule
|
|||
|
||||
$discountQty = $qtyPeriod * $rule->discount_amount;
|
||||
|
||||
if ($freeQty > $rule->discount_step)
|
||||
if ($freeQty > $rule->discount_step) {
|
||||
$discountQty += $freeQty - $rule->discount_step;
|
||||
}
|
||||
|
||||
$discountAmount = $discountQty * $item->price;
|
||||
|
||||
|
|
@ -305,9 +315,10 @@ class CartRule
|
|||
|
||||
$appliedRuleIds[$rule->id] = $rule->id;
|
||||
|
||||
if ($rule->end_other_rules)
|
||||
if ($rule->end_other_rules) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$item->applied_cart_rule_ids = join(',', $appliedRuleIds);
|
||||
|
||||
|
|
@ -332,8 +343,9 @@ class CartRule
|
|||
*/
|
||||
public function processShippingDiscount($cart)
|
||||
{
|
||||
if (! $selectedShipping = $cart->selected_shipping_rate)
|
||||
if (! $selectedShipping = $cart->selected_shipping_rate) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedShipping->discount_amount = 0;
|
||||
$selectedShipping->base_discount_amount = 0;
|
||||
|
|
@ -341,14 +353,17 @@ class CartRule
|
|||
$appliedRuleIds = [];
|
||||
|
||||
foreach ($this->getCartRules() as $rule) {
|
||||
if (! $this->canProcessRule($rule))
|
||||
if (! $this->canProcessRule($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->validator->validate($rule, $cart))
|
||||
if (! $this->validator->validate($rule, $cart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $rule || ! $rule->apply_to_shipping)
|
||||
if (! $rule || ! $rule->apply_to_shipping) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$discountAmount = $baseDiscountAmount = 0;
|
||||
|
||||
|
|
@ -381,9 +396,10 @@ class CartRule
|
|||
|
||||
$appliedRuleIds[$rule->id] = $rule->id;
|
||||
|
||||
if ($rule->end_other_rules)
|
||||
if ($rule->end_other_rules) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$selectedShipping->save();
|
||||
|
||||
|
|
@ -408,8 +424,9 @@ class CartRule
|
|||
*/
|
||||
public function processFreeShippingDiscount($cart)
|
||||
{
|
||||
if (! $selectedShipping = $cart->selected_shipping_rate)
|
||||
if (! $selectedShipping = $cart->selected_shipping_rate) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedShipping->discount_amount = 0;
|
||||
|
||||
|
|
@ -418,14 +435,17 @@ class CartRule
|
|||
$appliedRuleIds = [];
|
||||
|
||||
foreach ($this->getCartRules() as $rule) {
|
||||
if (! $this->canProcessRule($rule))
|
||||
if (! $this->canProcessRule($rule)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $this->validator->validate($rule, $cart))
|
||||
if (! $this->validator->validate($rule, $cart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $rule || ! $rule->free_shipping)
|
||||
if (! $rule || ! $rule->free_shipping) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$selectedShipping->price = 0;
|
||||
|
||||
|
|
@ -435,9 +455,10 @@ class CartRule
|
|||
|
||||
$appliedRuleIds[$rule->id] = $rule->id;
|
||||
|
||||
if ($rule->end_other_rules)
|
||||
if ($rule->end_other_rules) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$cartAppliedCartRuleIds = array_merge(explode(',', $cart->applied_cart_rule_ids), $appliedRuleIds);
|
||||
|
||||
|
|
@ -463,8 +484,9 @@ class CartRule
|
|||
$totalPrice = $totalBasePrice = $validCount = 0;
|
||||
|
||||
foreach ($items as $item) {
|
||||
if (! $this->canProcessRule($rule, $item))
|
||||
if (! $this->canProcessRule($rule, $item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
|
||||
|
||||
|
|
@ -490,14 +512,16 @@ class CartRule
|
|||
{
|
||||
$cart = Cart::getCart();
|
||||
|
||||
if (! $cart->coupon_code)
|
||||
if (! $cart->coupon_code) {
|
||||
return;
|
||||
}
|
||||
|
||||
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $cart->coupon_code);
|
||||
|
||||
if (! $coupon || ! in_array($coupon->cart_rule_id, explode(',', $cart->applied_cart_rule_ids)))
|
||||
if (! $coupon || ! in_array($coupon->cart_rule_id, explode(',', $cart->applied_cart_rule_ids))) {
|
||||
Cart::removeCouponCode();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Devide discount amount to children
|
||||
|
|
@ -511,8 +535,9 @@ class CartRule
|
|||
$ratio = $item->base_total != 0 ? $child->base_total / $item->base_total : 0;
|
||||
|
||||
foreach (['discount_amount', 'base_discount_amount'] as $column) {
|
||||
if (! $item->{$column})
|
||||
if (! $item->{$column}) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$child->{$column} = round(($item->{$column} * $ratio), 4);
|
||||
|
||||
|
|
|
|||
|
|
@ -196,8 +196,9 @@ class CartRuleController extends Controller
|
|||
'code_format' => 'required'
|
||||
]);
|
||||
|
||||
if (! request('id'))
|
||||
if (! request('id')) {
|
||||
return response()->json(['message' => trans('admin::app.promotions.cart-rules.cart-rule-not-defind-error')], 400);
|
||||
}
|
||||
|
||||
$this->cartRuleCouponRepository->generateCoupons(request()->all(), request('id'));
|
||||
|
||||
|
|
|
|||
|
|
@ -44,9 +44,10 @@ class CartRuleCouponController extends Controller
|
|||
foreach ($couponIds as $couponId) {
|
||||
$coupon = $this->cartRuleCouponRepository->find($couponId);
|
||||
|
||||
if ($coupon)
|
||||
if ($coupon) {
|
||||
$this->cartRuleCouponRepository->delete($couponId);
|
||||
}
|
||||
}
|
||||
|
||||
session()->flash('success', trans('admin::app.promotions.cart-rules.mass-delete-success'));
|
||||
|
||||
|
|
|
|||
|
|
@ -76,8 +76,9 @@ class Order
|
|||
*/
|
||||
public function manageCartRule($order)
|
||||
{
|
||||
if (! $order->discount_amount)
|
||||
if (! $order->discount_amount) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cartRuleIds = explode(',', $order->applied_cart_rule_ids);
|
||||
|
||||
|
|
@ -86,13 +87,15 @@ class Order
|
|||
foreach ($cartRuleIds as $ruleId) {
|
||||
$rule = $this->cartRuleRepository->find($ruleId);
|
||||
|
||||
if (! $rule)
|
||||
if (! $rule) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rule->update(['times_used' => $rule->times_used + 1]);
|
||||
|
||||
if (! $order->customer_id)
|
||||
if (! $order->customer_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ruleCustomer = $this->cartRuleCustomerRepository->findOneWhere([
|
||||
'customer_id' => $order->customer_id,
|
||||
|
|
@ -110,8 +113,9 @@ class Order
|
|||
}
|
||||
}
|
||||
|
||||
if (! $order->coupon_code)
|
||||
if (! $order->coupon_code) {
|
||||
return;
|
||||
}
|
||||
|
||||
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $order->coupon_code);
|
||||
|
||||
|
|
|
|||
|
|
@ -58,8 +58,9 @@ class CartRule extends Model implements CartRuleContract
|
|||
{
|
||||
$coupon = $this->coupon_code()->first();
|
||||
|
||||
if (! $coupon)
|
||||
if (! $coupon) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $coupon->code;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -318,11 +318,13 @@ class CartRuleRepository extends Repository
|
|||
$options = $attribute->options;
|
||||
}
|
||||
|
||||
if ($attribute->validation == 'decimal')
|
||||
if ($attribute->validation == 'decimal') {
|
||||
$attributeType = 'decimal';
|
||||
}
|
||||
|
||||
if ($attribute->validation == 'numeric')
|
||||
if ($attribute->validation == 'numeric') {
|
||||
$attributeType = 'integer';
|
||||
}
|
||||
|
||||
$attributes[2]['children'][] = [
|
||||
'key' => 'product|' . $attribute->code,
|
||||
|
|
@ -463,8 +465,9 @@ class CartRuleRepository extends Repository
|
|||
['code', 'default_name as admin_name']
|
||||
)->toArray();
|
||||
|
||||
if (! count($countryStates))
|
||||
if (! count($countryStates)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$collection[] = [
|
||||
'id' => $country->code,
|
||||
|
|
|
|||
Loading…
Reference in New Issue