| 1 |
<?php |
| 2 |
|
| 3 |
namespace Disco\App\Calc; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class CalcPercent |
| 7 |
* |
| 8 |
* @package Disco |
| 9 |
* @subpackage Disco\App\Calc |
| 10 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 11 |
* @link https://webappick.com |
| 12 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 13 |
* @category Plugin |
| 14 |
*/ |
| 15 |
class CalcFixed extends CalcAbstract { |
| 16 |
|
| 17 |
/** |
| 18 |
* @var object $cart Cart. |
| 19 |
* @phpstan-ignore-next-line |
| 20 |
*/ |
| 21 |
private $cart; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var float |
| 25 |
*/ |
| 26 |
private $discounted_quantities; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var array $rule Campaign Rule. |
| 30 |
*/ |
| 31 |
private $rule; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var array $item Cart Item. |
| 35 |
*/ |
| 36 |
private $item; |
| 37 |
|
| 38 |
/** |
| 39 |
* CalcFixed constructor. |
| 40 |
* |
| 41 |
* @param array $rule Campaign Rule. |
| 42 |
* @param array $item Cart Item. |
| 43 |
* @param \WC_Cart $cart Cart. |
| 44 |
* @param object $campaign Campaign. |
| 45 |
*/ |
| 46 |
public function __construct( $rule, $item, $cart, $campaign ) { |
| 47 |
$this->item = $item; |
| 48 |
$this->rule = $rule; |
| 49 |
$this->cart = $cart; |
| 50 |
// @phpstan-ignore-next-line |
| 51 |
$this->discount_intent = $campaign->get_discount_intent(); |
| 52 |
} |
| 53 |
|
| 54 |
public function calculate(): array { |
| 55 |
$discount = $this->calculate_discount(); |
| 56 |
|
| 57 |
return array( |
| 58 |
'price' => $this->calculate_price( $discount ), |
| 59 |
'discount' => $discount, |
| 60 |
'line_subtotal' => $this->calculate_line_subtotal( $discount ), |
| 61 |
'discounted_quantities' => $this->discounted_quantities, |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Calculate Discount. |
| 67 |
* |
| 68 |
* @return float Discount Amount. |
| 69 |
*/ |
| 70 |
public function calculate_discount(): float { // phpcs:ignore |
| 71 |
$discount_amount = 0; |
| 72 |
$fixed_discount = $this->rule['discount_value']; |
| 73 |
$quantity = (int) $this->item['quantity']; |
| 74 |
$min = $this->rule['min'] ? (int) $this->rule['min'] : 0; // phpcs:ignore |
| 75 |
$max = $this->rule['max'] ? (int) $this->rule['max'] : 0; // phpcs:ignore |
| 76 |
$recursive = isset( $this->rule['recursive'] ) && 'yes' === $this->rule['recursive']; // phpcs:ignore |
| 77 |
|
| 78 |
// Default discount for quantities. |
| 79 |
$discount_for_quantities = $min; |
| 80 |
|
| 81 |
// Apply for the Bulk Discount when max is set. |
| 82 |
if ( $max && $quantity >= $max ) { |
| 83 |
$discount_for_quantities = $max; |
| 84 |
} |
| 85 |
|
| 86 |
// If quantity between min and max then apply discount. |
| 87 |
if ( $quantity >= $min && $quantity <= $max ) { |
| 88 |
$discount_for_quantities = $quantity; |
| 89 |
} |
| 90 |
|
| 91 |
// Apply for the Bundle & BOGO Discount when recursive is set. |
| 92 |
$multiplier = 1; |
| 93 |
|
| 94 |
if ( $recursive ) { |
| 95 |
if ( $quantity % $min === 0 || $quantity > $discount_for_quantities ) { |
| 96 |
$multiplier = floor( $quantity / $discount_for_quantities ); |
| 97 |
$discount_for_quantities *= $multiplier; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Limit discount to Min or Max quantity. |
| 102 |
if ( $quantity >= $discount_for_quantities ) { |
| 103 |
$discount_amount = $fixed_discount * $multiplier; |
| 104 |
} |
| 105 |
|
| 106 |
$this->discounted_quantities = $discount_for_quantities; |
| 107 |
|
| 108 |
return apply_filters( 'disco_final_discounted_amount', $discount_amount, 'fixed' ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Calculate Price. |
| 113 |
* |
| 114 |
* @param float $discount Discount Amount. |
| 115 |
* @return float Price. |
| 116 |
*/ |
| 117 |
public function calculate_price( $discount ): float { |
| 118 |
$discounted_subtotal = $this->calculate_line_subtotal( $discount ); |
| 119 |
|
| 120 |
return $discounted_subtotal / $this->item['quantity']; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Calculate Line Subtotal. |
| 125 |
* |
| 126 |
* @param float $discount Discount Amount. |
| 127 |
* @return float Line Subtotal. |
| 128 |
*/ |
| 129 |
public function calculate_line_subtotal( $discount ): float { |
| 130 |
return $this->item['line_subtotal'] - $discount; |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|