| 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 Cart { |
| 24 |
|
| 25 |
/** |
| 26 |
* Count Total Items in Cart. |
| 27 |
* |
| 28 |
* @return int |
| 29 |
*/ |
| 30 |
public function cart_items_count() { |
| 31 |
// Get all items from the cart |
| 32 |
$cart_items = WC()->cart->get_cart(); |
| 33 |
|
| 34 |
// Create an array to store unique product IDs or variation IDs |
| 35 |
$unique_ids = array(); |
| 36 |
|
| 37 |
// Loop through each item in the cart |
| 38 |
if ( empty( $cart_items ) ) { |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
foreach ( $cart_items as $cart_item ) { |
| 43 |
// Use variation ID if available, otherwise use product ID |
| 44 |
$id = $cart_item['variation_id'] ?: $cart_item['product_id'];//phpcs:ignore |
| 45 |
|
| 46 |
// Add the ID to the array if it's not already there |
| 47 |
if ( in_array( $id, $unique_ids, true ) ) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
$unique_ids[] = $id; |
| 52 |
} |
| 53 |
|
| 54 |
// Return the count of unique product or variation IDs |
| 55 |
return count( $unique_ids ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Total Items Quantity in Cart. |
| 60 |
* |
| 61 |
* @return int |
| 62 |
*/ |
| 63 |
public function cart_items_quantity() { |
| 64 |
if ( is_array( WC()->cart->get_cart_item_quantities() ) ) { |
| 65 |
return array_sum( WC()->cart->get_cart_item_quantities() ); |
| 66 |
} |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Total Items Weight in Cart. |
| 73 |
* |
| 74 |
* @return float |
| 75 |
*/ |
| 76 |
public function cart_total_weight() { |
| 77 |
return WC()->cart->get_cart_contents_weight(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Cart Subtotal. |
| 82 |
* |
| 83 |
* @return float |
| 84 |
*/ |
| 85 |
public function cart_subtotal() { |
| 86 |
// @phpstan-ignore-next-line |
| 87 |
return apply_filters( 'disco_convert_price', (float) WC()->cart->subtotal ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Chosen Payment Method in Checkout. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function cart_payment_method() { |
| 96 |
$payment_method = WC()->session->get( 'chosen_payment_method' ); |
| 97 |
|
| 98 |
if ( is_array( $payment_method ) ) { |
| 99 |
$payment_method = implode( ',', $payment_method ); |
| 100 |
} |
| 101 |
|
| 102 |
return $payment_method; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Cart Coupons. |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function cart_coupons() { |
| 111 |
$coupons = WC()->cart->get_coupons(); |
| 112 |
|
| 113 |
if ( !empty( $coupons ) ) { |
| 114 |
return array_keys( $coupons ); |
| 115 |
} |
| 116 |
|
| 117 |
return array(); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|