PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Calc / CalcFactory.php

CalcFactory.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Calc/CalcFactory.php

176 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Disco\App\Calc;
4
5 use Disco\App\Disco;
6 use Disco\App\Utility\Config;
7 use Disco\App\Utility\Settings;
8 use WC_Cart;
9
10 /**
11 * Class CalcFactory
12 *
13 * @package Disco
14 * @subpackage Disco\App\Calc
15 * @author Ohidul Islam <wahid0003@gmail.com>
16 * @link https://webappick.com
17 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
18 * @category Plugin
19 */
20 class CalcFactory {
21
22 /**
23 * Get Discount.
24 *
25 * @param array $rule Campaign Rule.
26 * @param array $item Cart Item.
27 * @param \WC_Cart $cart Cart.
28 * @param \Disco\App\Utility\Config $campaign Campaign Config.
29 */
30 public static function get_discount( array $rule, array $item, WC_Cart $cart, Config $campaign ): array {
31 $discount_type = $rule['discount_type'];
32 $class_name = 'Calc' . ucwords( str_replace( '_', ' ', $discount_type ) );
33 $class_name = 'Disco\App\Calc\\' . str_replace( ' ', '', $class_name );
34
35 if ( class_exists( $class_name ) ) {
36 $type = new $class_name( $rule, $item, $cart, $campaign );
37
38 // @phpstan-ignore-next-line
39 return ( new Calc )->get_discount( $type );
40 }
41
42 return array(
43 'price' => $item['data']->get_price(),
44 'discount' => 0,
45 'line_subtotal' => $item['line_subtotal'],
46 );
47 }
48
49 /**
50 * Get Discount Applies To.
51 *
52 * @param array $rule Campaign Rule.
53 * @param \Disco\App\Utility\Config $campaign Campaign.
54 */
55 public static function discount_applies_to( array $rule, Config $campaign ): string {
56 if (
57 $rule['discount_type'] === 'percent'
58 || $rule['discount_type'] === 'fixed'
59 || $rule['discount_type'] === 'fixed_price'
60 ) {
61 $applies_to = 'line_item_subtotal';
62 } elseif ( $rule['discount_type'] === 'percent_per_product' || $rule['discount_type'] === 'fixed_per_product' ) {
63 $applies_to = 'line_item';
64 } elseif ( 'Product' === $campaign->get_discount_intent() ) {
65 $applies_to = 'product';
66 } else {
67 $applies_to = 'cart';
68 }
69
70 return $applies_to;
71 }
72
73 /**
74 * Get Price.
75 *
76 * @param array $item Cart Item.
77 */
78 public static function get_price( array $item ): float {
79 $id = $item['product_id'];
80
81 if ( $item['variation_id'] > 0 ) {
82 $id = $item['variation_id'];
83 }
84
85 // Get price from product if disco pro is active.
86 if ( Disco::is_pro() ) {
87 $product = wc_get_product( $id );
88
89 // @phpstan-ignore-next-line
90 return Settings::get_price( $product );
91 }
92
93 // Get price from meta if disco pro is not active.
94 return Settings::get_price_from_meta( $id );
95 }
96
97 /**
98 * Get Cart Subtotal.
99 *
100 * @param \WC_Cart $cart Cart.
101 * @return float Cart Subtotal.
102 */
103 public static function get_cart_subtotal( WC_Cart $cart ): float {
104 /**
105 * Get cart subtotal from cart if disco pro is active,
106 * if disco pro is not active, get cart subtotal from meta.
107 * It's for support Multi-currency plugin in disco pro.
108 */
109 if ( Disco::is_pro() ) {
110 return $cart->get_subtotal();
111 }
112
113 // Get cart subtotal from meta if disco pro is not active.
114 $subtotal = 0;
115
116 foreach ( $cart->get_cart() as $item ) {
117 $subtotal += Settings::get_price_from_meta( $item['product_id'] ) * $item['quantity'];
118 }
119
120 return $subtotal;
121 }
122
123 /**
124 * Get free quantity.
125 *
126 * @param array $rule Campaign Rule.
127 * @param array $item Cart Item.
128 * @return float
129 */
130 public static function get_free_quantity( array $rule, array $item ) {
131 $cart_qty = $item['quantity'];
132
133 if ( $rule['recursive'] === 'no' && $rule['min'] > 0 ) {
134 return $rule['get_quantity'];
135 }
136
137 return floor( $cart_qty / $rule['min'] ) * $rule['get_quantity'];
138 }
139
140 /**
141 * Get Product Price.
142 *
143 * @param \WC_Product $product Product.
144 * @return float Product Price.
145 */
146 public static function get_product_price( \WC_Product $product ): float {
147 // Get price from product if disco pro is active.
148 if ( Disco::is_pro() ) {
149 $product = wc_get_product( $product->get_id() );
150
151 // @phpstan-ignore-next-line
152 return Settings::get_price( $product );
153 }
154
155 // Get price from meta if disco pro is not active.
156 return Settings::get_price_from_meta( $product->get_id() );
157 }
158
159 /**
160 * Get cart items ids
161 *
162 * @return array Cart Items IDs.
163 */
164 public static function get_cart_items_ids(): array {
165 $cart = WC()->cart;
166 $cart_items_ids = array();
167
168 foreach ( $cart->get_cart() as $item ) {
169 $cart_items_ids[] = $item['product_id'];
170 }
171
172 return $cart_items_ids;
173 }
174
175 }
176