| 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 CustomerHistory { |
| 24 |
|
| 25 |
/** |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
private $customer_id; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $ids; |
| 34 |
|
| 35 |
/** |
| 36 |
* Customer constructor. |
| 37 |
* |
| 38 |
* @param array|object $ids Product IDs. |
| 39 |
* @param int $customer_id Customer ID. |
| 40 |
*/ |
| 41 |
public function __construct( $ids, $customer_id = 0 ) { |
| 42 |
$this->customer_id = $customer_id; |
| 43 |
|
| 44 |
if ( is_user_logged_in() ) { |
| 45 |
$this->customer_id = wp_get_current_user()->ID; |
| 46 |
} |
| 47 |
|
| 48 |
if ( is_object( $ids ) ) { |
| 49 |
$ids = (array) $ids; |
| 50 |
} |
| 51 |
|
| 52 |
$this->ids = $ids; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Is Customer First Order. |
| 57 |
* |
| 58 |
* @return bool |
| 59 |
*/ |
| 60 |
public function is_first_order() { |
| 61 |
$orders = wc_get_customer_order_count( $this->customer_id ); |
| 62 |
|
| 63 |
return $orders === 0; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get Customer Last Order Date. |
| 68 |
* |
| 69 |
* @return string|null |
| 70 |
*/ |
| 71 |
public function last_order_date() { |
| 72 |
$last_order = wc_get_customer_last_order( $this->customer_id ); |
| 73 |
|
| 74 |
if ( ! $last_order instanceof \WC_Order ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
$last_order_date = $last_order->get_date_created(); |
| 79 |
|
| 80 |
if ( $last_order_date instanceof \WC_DateTime ) { |
| 81 |
return $last_order_date->date_i18n(); |
| 82 |
} |
| 83 |
|
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get Customer Last Order Amount. |
| 89 |
* |
| 90 |
* @return float |
| 91 |
*/ |
| 92 |
public function last_order_amount() { |
| 93 |
$last_order = wc_get_customer_last_order( $this->customer_id ); |
| 94 |
|
| 95 |
if ( ! $last_order instanceof \WC_Order ) { |
| 96 |
return 0.00; |
| 97 |
} |
| 98 |
|
| 99 |
return $last_order->get_total(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get Total Order Made By Customer. |
| 104 |
* |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public function total_order_made() { |
| 108 |
return wc_get_customer_order_count( $this->customer_id ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get Total Amount Spent By Customer. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function total_spent() { |
| 117 |
return wc_get_customer_total_spent( $this->customer_id ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get Total Quantity Sold By Specific Customers. |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function total_quantity_sold_by_ids() { |
| 126 |
$total_sold = 0; |
| 127 |
|
| 128 |
if ( ! empty( $this->ids ) ) { |
| 129 |
$quantities = array(); |
| 130 |
|
| 131 |
foreach ( $this->ids as $id ) { |
| 132 |
$product = wc_get_product( $id ); |
| 133 |
|
| 134 |
if ( ! ( $product instanceof \WC_Product ) ) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
$quantities[] = $product->get_total_sales(); |
| 139 |
} |
| 140 |
|
| 141 |
$total_sold = array_sum( $quantities ); |
| 142 |
} |
| 143 |
|
| 144 |
return $total_sold; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get Total Net Revenue By Specific Customers. |
| 149 |
* |
| 150 |
* @return float|int |
| 151 |
*/ |
| 152 |
public function total_amount_sold_by_ids() { |
| 153 |
$total_sold = 0; |
| 154 |
|
| 155 |
if ( ! empty( $this->ids ) ) { |
| 156 |
$quantities = array(); |
| 157 |
|
| 158 |
foreach ( $this->ids as $id ) { |
| 159 |
$quantities[] = $this->net_revenue( $id ); |
| 160 |
} |
| 161 |
|
| 162 |
$total_sold = array_sum( $quantities ); |
| 163 |
} |
| 164 |
|
| 165 |
return $total_sold; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get products sum of total order quantity. |
| 170 |
* |
| 171 |
* @return float|int |
| 172 |
*/ |
| 173 |
public function total_order_made_by_ids() { |
| 174 |
$total_sold = 0; |
| 175 |
|
| 176 |
if ( ! empty( $this->ids ) ) { |
| 177 |
$quantities = array(); |
| 178 |
|
| 179 |
foreach ( $this->ids as $id ) { |
| 180 |
$quantities[] = $this->order_quantity( $id ); |
| 181 |
} |
| 182 |
|
| 183 |
$total_sold = array_sum( $quantities ); |
| 184 |
} |
| 185 |
|
| 186 |
return $total_sold; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get Single product total order quantity. |
| 191 |
* |
| 192 |
* @param int $product_id Product ID. |
| 193 |
* @return float |
| 194 |
*/ |
| 195 |
private function order_quantity( $product_id ) { |
| 196 |
global $wpdb; |
| 197 |
|
| 198 |
return (float) $wpdb->get_var(// phpcs:ignore |
| 199 |
$wpdb->prepare( |
| 200 |
" |
| 201 |
SELECT COUNT(DISTINCT(order_id)) |
| 202 |
FROM {$wpdb->prefix}wc_order_product_lookup |
| 203 |
WHERE product_id = %d AND customer_id = %d |
| 204 |
", |
| 205 |
$product_id, |
| 206 |
$this->customer_id |
| 207 |
) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get Single product total net revenue. |
| 213 |
* |
| 214 |
* @param int $product_id Product ID. |
| 215 |
* @return float |
| 216 |
*/ |
| 217 |
private function net_revenue( $product_id ) { |
| 218 |
global $wpdb; |
| 219 |
|
| 220 |
return (float) $wpdb->get_var(// phpcs:ignore |
| 221 |
$wpdb->prepare( |
| 222 |
" |
| 223 |
SELECT SUM(product_net_revenue) |
| 224 |
FROM {$wpdb->prefix}wc_order_product_lookup |
| 225 |
WHERE product_id = %d AND customer_id = %d |
| 226 |
", |
| 227 |
$product_id, |
| 228 |
$this->customer_id |
| 229 |
) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|