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 / Attributes / ProductHistory.php

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

239 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Product Attributes
4 *
5 * @package Disco
6 * @subpackage \App\Attributes
7 */
8
9 namespace Disco\App\Attributes;
10
11 /**
12 * Class Product
13 *
14 * This class provides methods for retrieving various attributes of a WooCommerce product.
15 *
16 * @package Disco
17 * @subpackage \App\Attributes
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
21 * @category Attributes
22 */
23 class ProductHistory {
24
25 /**
26 * Product ID
27 *
28 * @var int
29 */
30 private $id;
31
32 /**
33 * Product Ids
34 *
35 * @var array
36 */
37 private $ids;
38
39 /**
40 * Product constructor.
41 *
42 * @param array $ids Product IDs.
43 * @param \WC_Product | null $product Product object.
44 */
45 public function __construct( $ids, $product = null ) {
46 if ( is_object( $ids ) ) {
47 $ids = array( $ids );
48 }
49
50 $this->id = 0;
51
52 if ( $product instanceof \WC_Product ) {
53 $this->id = $product->get_id();
54 }
55
56 $this->ids = $ids;
57 }
58
59 /**
60 * Get Total Quantity Sold for specific product
61 *
62 * @return int
63 * @since 1.2.59
64 */
65 public function total_quantity_sold() {
66 $product = wc_get_product( $this->id );
67
68 if ( ! ( $product instanceof \WC_Product ) ) {
69 return 0;
70 }
71
72 $total_sold = $product->get_total_sales();
73
74 if ( ! is_numeric( $total_sold ) ) {
75 return 0;
76 }
77
78 return (int) $total_sold;
79 }
80
81 /**
82 * Get Net Revenue for specific product.
83 *
84 * @return float|int
85 * @since 1.2.59
86 */
87 public function total_amount_sold() {
88 return $this->net_revenue( $this->id );
89 }
90
91 /**
92 * Get Total Order Made for specific product.
93 *
94 * @return float|int
95 * @since 1.2.59
96 */
97 public function total_order_made() {
98 return $this->order_quantity( $this->id );
99 }
100
101 /**
102 * Get Net Revenue for specific products.
103 *
104 * @return int
105 */
106 public function total_quantity_sold_by_ids() {
107 $total_sold = 0;
108
109 if ( is_array( $this->ids ) && ! empty( $this->ids ) ) {
110 $quantities = array();
111
112 foreach ( $this->ids as $id ) {
113 $product = wc_get_product( $id );
114
115 if ( ! ( $product instanceof \WC_Product ) ) {
116 continue;
117 }
118
119 $quantities[] = $product->get_total_sales();
120 }
121
122 $total_sold = array_sum( $quantities );
123 }
124
125 return $total_sold;
126 }
127
128 /**
129 * Get specific products net revenue.
130 *
131 * @return float|int
132 */
133 public function total_amount_sold_by_ids() {
134 $total_sold = 0;
135
136 if ( is_array( $this->ids ) && ! empty( $this->ids ) ) {
137 $quantities = array();
138
139 foreach ( $this->ids as $id ) {
140 $quantities[] = $this->net_revenue( $id );
141 }
142
143 $total_sold = array_sum( $quantities );
144 }
145
146 return $total_sold;
147 }
148
149 /**
150 * Get products sum of total order quantity.
151 *
152 * @return float|int
153 */
154 public function total_order_made_by_ids() {
155 $total_sold = 0;
156
157 if ( ! empty( $this->ids ) ) {
158 $quantities = array();
159
160 foreach ( $this->ids as $id ) {
161 $quantities[] = $this->order_quantity( $id );
162 }
163
164 $total_sold = array_sum( $quantities );
165 }
166
167 return $total_sold;
168 }
169
170 /**
171 * Get Product Last Order Date.
172 *
173 * @return string
174 */
175 public function last_order_date() {
176 global $wpdb;
177
178 $date = $wpdb->get_var(// phpcs:ignore
179 $wpdb->prepare(
180 "
181 SELECT date_created
182 FROM {$wpdb->prefix}wc_order_product_lookup
183 WHERE product_id = %d ORDER BY order_id DESC LIMIT 1
184 ",
185 $this->id
186 )
187 );
188
189 if ( $date ) {
190 return gmdate( 'Y-m-d', strtotime( $date ) );
191 }
192
193 return '';
194 }
195
196 /**
197 * Get Single product total order quantity.
198 *
199 * @param int $product_id Product ID.
200 * @return float
201 */
202 private function order_quantity( $product_id ) {
203 global $wpdb;
204
205 return (int) $wpdb->get_var(// phpcs:ignore
206 $wpdb->prepare(
207 "
208 SELECT COUNT(DISTINCT(order_id))
209 FROM {$wpdb->prefix}wc_order_product_lookup
210 WHERE product_id = %d
211 ",
212 $product_id
213 )
214 );
215 }
216
217 /**
218 * Get Single product net revenue.
219 *
220 * @param int $product_id Product ID.
221 * @return float
222 */
223 private function net_revenue( $product_id ) {
224 global $wpdb;
225
226 return (float) $wpdb->get_var(// phpcs:ignore
227 $wpdb->prepare(
228 "
229 SELECT SUM(product_net_revenue)
230 FROM {$wpdb->prefix}wc_order_product_lookup
231 WHERE product_id = %d
232 ",
233 $product_id
234 )
235 );
236 }
237
238 }
239