| 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 Ids |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $ids; |
| 31 |
|
| 32 |
/** |
| 33 |
* Product constructor. |
| 34 |
* |
| 35 |
* @param array $ids Product IDs. |
| 36 |
*/ |
| 37 |
public function __construct( $ids = array() ) { |
| 38 |
if ( is_object( $ids ) ) { |
| 39 |
$ids = array( $ids ); |
| 40 |
} |
| 41 |
|
| 42 |
$this->ids = $ids; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get Net Revenue for specific products. |
| 47 |
* |
| 48 |
* @return int |
| 49 |
*/ |
| 50 |
public function total_quantity_sold() { |
| 51 |
$total_sold = 0; |
| 52 |
|
| 53 |
if ( is_array( $this->ids ) && ! empty( $this->ids ) ) { |
| 54 |
$quantities = array(); |
| 55 |
|
| 56 |
foreach ( $this->ids as $id ) { |
| 57 |
$product = wc_get_product( $id ); |
| 58 |
|
| 59 |
if ( ! ( $product instanceof \WC_Product ) ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$quantities[] = $product->get_total_sales(); |
| 64 |
} |
| 65 |
|
| 66 |
$total_sold = array_sum( $quantities ); |
| 67 |
} |
| 68 |
|
| 69 |
return $total_sold; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get specific products net revenue. |
| 74 |
* |
| 75 |
* @return float|int |
| 76 |
*/ |
| 77 |
public function total_amount_sold() { |
| 78 |
$total_sold = 0; |
| 79 |
|
| 80 |
if ( is_array( $this->ids ) && ! empty( $this->ids ) ) { |
| 81 |
$quantities = array(); |
| 82 |
|
| 83 |
foreach ( $this->ids as $id ) { |
| 84 |
$quantities[] = $this->net_revenue( $id ); |
| 85 |
} |
| 86 |
|
| 87 |
$total_sold = array_sum( $quantities ); |
| 88 |
} |
| 89 |
|
| 90 |
return $total_sold; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get products sum of total order quantity. |
| 95 |
* |
| 96 |
* @return float|int |
| 97 |
*/ |
| 98 |
public function total_order_made() { |
| 99 |
$total_sold = 0; |
| 100 |
|
| 101 |
if ( ! empty( $this->ids ) ) { |
| 102 |
$quantities = array(); |
| 103 |
|
| 104 |
foreach ( $this->ids as $id ) { |
| 105 |
$quantities[] = $this->order_quantity( $id ); |
| 106 |
} |
| 107 |
|
| 108 |
$total_sold = array_sum( $quantities ); |
| 109 |
} |
| 110 |
|
| 111 |
return $total_sold; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get Product Last Order Date. |
| 116 |
* |
| 117 |
* @param int $id Product id. |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function last_order_date( $id ) { |
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
$date = $wpdb->get_var(// phpcs:ignore |
| 124 |
$wpdb->prepare( |
| 125 |
" |
| 126 |
SELECT date_created |
| 127 |
FROM {$wpdb->prefix}wc_order_product_lookup |
| 128 |
WHERE product_id = %d ORDER BY order_id DESC LIMIT 1 |
| 129 |
", |
| 130 |
$id |
| 131 |
) |
| 132 |
); |
| 133 |
|
| 134 |
if ( $date ) { |
| 135 |
return gmdate( 'Y-m-d', strtotime( $date ) ); |
| 136 |
} |
| 137 |
|
| 138 |
return ''; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get Single product total order quantity. |
| 143 |
* |
| 144 |
* @param int $product_id Product ID. |
| 145 |
* @return float |
| 146 |
*/ |
| 147 |
private function order_quantity( $product_id ) { |
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
return (int) $wpdb->get_var(// phpcs:ignore |
| 151 |
$wpdb->prepare( |
| 152 |
" |
| 153 |
SELECT COUNT(DISTINCT(order_id)) |
| 154 |
FROM {$wpdb->prefix}wc_order_product_lookup |
| 155 |
WHERE product_id = %d |
| 156 |
", |
| 157 |
$product_id |
| 158 |
) |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get Single product net revenue. |
| 164 |
* |
| 165 |
* @param int $product_id Product ID. |
| 166 |
* @return float |
| 167 |
*/ |
| 168 |
private function net_revenue( $product_id ) { |
| 169 |
global $wpdb; |
| 170 |
|
| 171 |
return (float) $wpdb->get_var(// phpcs:ignore |
| 172 |
$wpdb->prepare( |
| 173 |
" |
| 174 |
SELECT SUM(product_net_revenue) |
| 175 |
FROM {$wpdb->prefix}wc_order_product_lookup |
| 176 |
WHERE product_id = %d |
| 177 |
", |
| 178 |
$product_id |
| 179 |
) |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|