PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.49
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.49
1.4.15 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 All 178 releases
disco / app / Calc / CalcFactory.php

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

172 lines 4.3 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 // Get cart subtotal from cart if disco pro is active.
105 if ( Disco::is_pro() ) {
106 return $cart->get_subtotal();
107 }
108
109 // Get cart subtotal from meta if disco pro is not active.
110 $subtotal = 0;
111
112 foreach ( $cart->get_cart() as $item ) {
113 $subtotal += Settings::get_price_from_meta( $item['product_id'] ) * $item['quantity'];
114 }
115
116 return $subtotal;
117 }
118
119 /**
120 * Get free quantity.
121 *
122 * @param array $rule Campaign Rule.
123 * @param array $item Cart Item.
124 * @return float
125 */
126 public static function get_free_quantity( array $rule, array $item ) {
127 $cart_qty = $item['quantity'];
128
129 if ( $rule['recursive'] === 'no' && $rule['min'] > 0 ) {
130 return $rule['get_quantity'];
131 }
132
133 return floor( $cart_qty / $rule['min'] ) * $rule['get_quantity'];
134 }
135
136 /**
137 * Get Product Price.
138 *
139 * @param \WC_Product $product Product.
140 * @return float Product Price.
141 */
142 public static function get_product_price( \WC_Product $product ): float {
143 // Get price from product if disco pro is active.
144 if ( Disco::is_pro() ) {
145 $product = wc_get_product( $product->get_id() );
146
147 // @phpstan-ignore-next-line
148 return Settings::get_price( $product );
149 }
150
151 // Get price from meta if disco pro is not active.
152 return Settings::get_price_from_meta( $product->get_id() );
153 }
154
155 /**
156 * Get cart items ids
157 *
158 * @return array Cart Items IDs.
159 */
160 public static function get_cart_items_ids(): array {
161 $cart = WC()->cart;
162 $cart_items_ids = array();
163
164 foreach ( $cart->get_cart() as $item ) {
165 $cart_items_ids[] = $item['product_id'];
166 }
167
168 return $cart_items_ids;
169 }
170
171 }
172