Merge pull request #2966 from Haendlerbund/add_tests_for_cart_rules
Add tests for cart rules
This commit is contained in:
commit
7f3e564c14
|
|
@ -134,11 +134,14 @@ class CartRule
|
|||
*/
|
||||
public function getCartRules()
|
||||
{
|
||||
static $cartRules;
|
||||
|
||||
if ($cartRules) {
|
||||
return $cartRules;
|
||||
$staticCartRules = new class() {
|
||||
public static $cartRules;
|
||||
public static $cartID;
|
||||
};
|
||||
if ($staticCartRules::$cartID === cart()->getCart()->id && $staticCartRules::$cartRules) {
|
||||
return $staticCartRules::$cartRules;
|
||||
}
|
||||
$staticCartRules::$cartID = cart()->getCart()->id;
|
||||
|
||||
$customerGroupId = null;
|
||||
|
||||
|
|
@ -166,6 +169,7 @@ class CartRule
|
|||
->orderBy('sort_order', 'asc');
|
||||
})->findWhere(['status' => 1]);
|
||||
|
||||
$staticCartRules::$cartRules = $cartRules;
|
||||
return $cartRules;
|
||||
}
|
||||
|
||||
|
|
@ -246,6 +250,10 @@ class CartRule
|
|||
continue;
|
||||
}
|
||||
|
||||
if ($rule->coupon_code) {
|
||||
$item->coupon_code = $rule->coupon_code;
|
||||
}
|
||||
|
||||
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
|
||||
|
||||
$discountAmount = $baseDiscountAmount = 0;
|
||||
|
|
@ -254,9 +262,9 @@ class CartRule
|
|||
case 'by_percent':
|
||||
$rulePercent = min(100, $rule->discount_amount);
|
||||
|
||||
$discountAmount = ($quantity * $item->price - $item->discount_amount) * ($rulePercent / 100);
|
||||
$discountAmount = ($quantity * $item->price + $item->tax_amount - $item->discount_amount) * ($rulePercent / 100);
|
||||
|
||||
$baseDiscountAmount = ($quantity * $item->base_price - $item->base_discount_amount) * ($rulePercent / 100);
|
||||
$baseDiscountAmount = ($quantity * $item->base_price + $item->base_tax_amount - $item->base_discount_amount) * ($rulePercent / 100);
|
||||
|
||||
if (! $rule->discount_quantity || $rule->discount_quantity > $quantity) {
|
||||
$discountPercent = min(100, $item->discount_percent + $rulePercent);
|
||||
|
|
@ -316,8 +324,16 @@ class CartRule
|
|||
break;
|
||||
}
|
||||
|
||||
$item->discount_amount = min($item->discount_amount + $discountAmount, $item->price * $quantity + $item->tax_amount);
|
||||
$item->base_discount_amount = min($item->base_discount_amount + $baseDiscountAmount, $item->base_price * $quantity + $item->base_tax_amount);
|
||||
$item->discount_amount = round(
|
||||
min(
|
||||
$item->discount_amount + $discountAmount,
|
||||
$item->price * $quantity + $item->tax_amount
|
||||
),2);
|
||||
$item->base_discount_amount = round(
|
||||
min(
|
||||
$item->base_discount_amount + $baseDiscountAmount,
|
||||
$item->base_price * $quantity + $item->base_tax_amount
|
||||
), 2);
|
||||
|
||||
$appliedRuleIds[$rule->id] = $rule->id;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,14 +19,14 @@ class Order
|
|||
/**
|
||||
* CartRuleCustomerRepository object
|
||||
*
|
||||
* @var Webkul\CartRule\Repositories\\CartRuleCustomerRepository
|
||||
* @var \Webkul\CartRule\Repositories\CartRuleCustomerRepository
|
||||
*/
|
||||
protected $cartRuleCustomerRepository;
|
||||
|
||||
/**
|
||||
* CartRuleCouponRepository object
|
||||
*
|
||||
* @var Webkul\CartRule\Repositories\\CartRuleCouponRepository
|
||||
* @var \Webkul\CartRule\Repositories\CartRuleCouponRepository
|
||||
*/
|
||||
protected $cartRuleCouponRepository;
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ class Order
|
|||
|
||||
/**
|
||||
* Save cart rule and cart rule coupon properties after place order
|
||||
*
|
||||
*
|
||||
* @param \Webkul\Sales\Contracts\Order $order
|
||||
* @return void
|
||||
*/
|
||||
|
|
@ -112,7 +112,7 @@ class Order
|
|||
}
|
||||
|
||||
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $order->coupon_code);
|
||||
|
||||
|
||||
if ($coupon) {
|
||||
$this->cartRuleCouponRepository->update(['times_used' => $coupon->times_used + 1], $coupon->id);
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class CartRuleRepository extends Repository
|
|||
/**
|
||||
* CartRuleCouponRepository object
|
||||
*
|
||||
* @var Webkul\CartRule\Repositories\CartRuleCouponRepository
|
||||
* @var \Webkul\CartRule\Repositories\CartRuleCouponRepository
|
||||
*/
|
||||
protected $cartRuleCouponRepository;
|
||||
|
||||
|
|
|
|||
|
|
@ -436,20 +436,19 @@ class Cart
|
|||
*
|
||||
* @return \Webkul\Checkout\Contracts\Cart|null
|
||||
*/
|
||||
public function getCart()
|
||||
public function getCart(): ?\Webkul\Checkout\Contracts\Cart
|
||||
{
|
||||
$cart = null;
|
||||
|
||||
if ($this->getCurrentCustomer()->check()) {
|
||||
$cart = $this->cartRepository->findOneWhere([
|
||||
return $this->cartRepository->findOneWhere([
|
||||
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
} elseif (session()->has('cart')) {
|
||||
$cart = $this->cartRepository->find(session()->get('cart')->id);
|
||||
return $this->cartRepository->find(session()->get('cart')->id);
|
||||
}
|
||||
|
||||
return $cart && $cart->is_active ? $cart : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -483,7 +482,8 @@ class Cart
|
|||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return void is the cart valid
|
||||
* @return bool
|
||||
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
||||
*/
|
||||
public function saveCustomerAddress($data): bool
|
||||
{
|
||||
|
|
@ -633,6 +633,7 @@ class Cart
|
|||
} else {
|
||||
foreach ($cart->items as $item) {
|
||||
$response = $item->product->getTypeInstance()->validateCartItem($item);
|
||||
// ToDo: refactoring of all validateCartItem functions, at the moment they return nothing
|
||||
|
||||
if ($response) {
|
||||
return;
|
||||
|
|
@ -741,7 +742,7 @@ class Cart
|
|||
* @param \Webkul\Checkout\Contracts\CartItem $item
|
||||
* @return \Webkul\Checkout\Contracts\CartItem
|
||||
*/
|
||||
protected function setItemTaxToZero(CartItem $item): CartItem
|
||||
protected function setItemTaxToZero(\Webkul\Checkout\Contracts\CartItem $item): \Webkul\Checkout\Contracts\CartItem
|
||||
{
|
||||
$item->tax_percent = 0;
|
||||
$item->tax_amount = 0;
|
||||
|
|
@ -755,7 +756,7 @@ class Cart
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasError()
|
||||
public function hasError(): bool
|
||||
{
|
||||
if (! $this->getCart()) {
|
||||
return true;
|
||||
|
|
@ -773,7 +774,7 @@ class Cart
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isItemsHaveSufficientQuantity()
|
||||
public function isItemsHaveSufficientQuantity(): bool
|
||||
{
|
||||
foreach ($this->getCart()->items as $item) {
|
||||
if (! $this->isItemHaveQuantity($item)) {
|
||||
|
|
@ -790,7 +791,7 @@ class Cart
|
|||
* @param \Webkul\Checkout\Contracts\CartItem $item
|
||||
* @return bool
|
||||
*/
|
||||
public function isItemHaveQuantity($item)
|
||||
public function isItemHaveQuantity($item): bool
|
||||
{
|
||||
return $item->product->getTypeInstance()->isItemHaveQuantity($item);
|
||||
}
|
||||
|
|
@ -800,7 +801,7 @@ class Cart
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deActivateCart()
|
||||
public function deActivateCart(): void
|
||||
{
|
||||
if ($cart = $this->getCart()) {
|
||||
$this->cartRepository->update(['is_active' => false], $cart->id);
|
||||
|
|
@ -816,7 +817,7 @@ class Cart
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepareDataForOrder()
|
||||
public function prepareDataForOrder(): array
|
||||
{
|
||||
$data = $this->toArray();
|
||||
|
||||
|
|
@ -875,7 +876,7 @@ class Cart
|
|||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function prepareDataForOrderItem($data)
|
||||
public function prepareDataForOrderItem($data): array
|
||||
{
|
||||
$finalData = [
|
||||
'product' => $this->productRepository->find($data['product_id']),
|
||||
|
|
@ -1033,9 +1034,9 @@ class Cart
|
|||
* When logged in as guest or the customer profile is not complete, we use the
|
||||
* billing address to fill the order customer_ data.
|
||||
*
|
||||
* @param \Webkul\Checkout\Models\Cart $cart
|
||||
* @param \Webkul\Checkout\Contracts\Cart $cart
|
||||
*/
|
||||
private function assignCustomerFields(\Webkul\Checkout\Models\Cart $cart): void
|
||||
private function assignCustomerFields(\Webkul\Checkout\Contracts\Cart $cart): void
|
||||
{
|
||||
if ($this->getCurrentCustomer()->check()
|
||||
&& ($user = $this->getCurrentCustomer()->user())
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
use Webkul\CartRule\Models\CartRule;
|
||||
|
||||
$factory->define(CartRule::class, function (Faker $faker) {
|
||||
return [
|
||||
'name' => $faker->uuid,
|
||||
'description' => $faker->sentence(),
|
||||
'starts_from' => null,
|
||||
'ends_till' => null,
|
||||
'coupon_type' => '1',
|
||||
'use_auto_generation' => '0',
|
||||
'usage_per_customer' => '100',
|
||||
'uses_per_coupon' => '100',
|
||||
'times_used' => '0',
|
||||
'condition_type' => '2',
|
||||
'end_other_rules' => '0',
|
||||
'uses_attribute_conditions' => '0',
|
||||
'discount_quantity' => '0',
|
||||
'discount_step' => '0',
|
||||
'apply_to_shipping' => '0',
|
||||
'free_shipping' => '0',
|
||||
'sort_order' => '0',
|
||||
'status' => '1',
|
||||
'conditions' => null,
|
||||
];
|
||||
});
|
||||
|
|
@ -12,15 +12,17 @@ class Tax
|
|||
|
||||
/**
|
||||
* Returns an array with tax rates and tax amount
|
||||
* @param object $that
|
||||
* @param bool $asBase
|
||||
*
|
||||
* @param \Webkul\Checkout\Contracts\Cart $cart
|
||||
* @param bool $asBase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getTaxRatesWithAmount(object $that, bool $asBase = false): array
|
||||
public static function getTaxRatesWithAmount(\Webkul\Checkout\Contracts\Cart $cart, bool $asBase = false): array
|
||||
{
|
||||
$taxes = [];
|
||||
|
||||
foreach ($that->items as $item) {
|
||||
foreach ($cart->items as $item) {
|
||||
$taxRate = (string) round((float) $item->tax_percent, self::TAX_RATE_PRECISION);
|
||||
|
||||
if (! array_key_exists($taxRate, $taxes)) {
|
||||
|
|
@ -40,13 +42,15 @@ class Tax
|
|||
|
||||
/**
|
||||
* Returns the total tax amount
|
||||
* @param object $that
|
||||
* @param bool $asBase
|
||||
*
|
||||
* @param \Webkul\Checkout\Contracts\Cart $cart
|
||||
* @param bool $asBase
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public static function getTaxTotal(object $that, bool $asBase = false): float
|
||||
public static function getTaxTotal(\Webkul\Checkout\Contracts\Cart $cart, bool $asBase = false): float
|
||||
{
|
||||
$taxes = self::getTaxRatesWithAmount($that, $asBase);
|
||||
$taxes = self::getTaxRatesWithAmount($cart, $asBase);
|
||||
|
||||
$result = 0;
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue