| 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->get_subtotal() ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Cart subtotal with tax. |
| 92 |
* |
| 93 |
* @return float |
| 94 |
*/ |
| 95 |
public function cart_subtotal_with_tax() { |
| 96 |
// @phpstan-ignore-next-line |
| 97 |
return apply_filters( 'disco_convert_price', (float) WC()->cart->subtotal ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Chosen Payment Method in Checkout. |
| 102 |
*/ |
| 103 |
public function cart_payment_method(): string { |
| 104 |
$payment_method = WC()->session->get( 'chosen_payment_method' ); |
| 105 |
|
| 106 |
if ( is_array( $payment_method ) ) { |
| 107 |
return implode( ',', $payment_method ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( is_string( $payment_method ) ) { |
| 111 |
return $payment_method; |
| 112 |
} |
| 113 |
|
| 114 |
// fallback when null or unexpected type |
| 115 |
return ''; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Cart Coupons. |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function cart_coupons() { |
| 124 |
$coupons = WC()->cart->get_coupons(); |
| 125 |
|
| 126 |
if ( !empty( $coupons ) ) { |
| 127 |
return array_keys( $coupons ); |
| 128 |
} |
| 129 |
|
| 130 |
return array(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Products in Cart. |
| 135 |
* |
| 136 |
* @return array Product IDs. |
| 137 |
*/ |
| 138 |
public function products_in_cart() { |
| 139 |
$cart_items = WC()->cart->get_cart(); |
| 140 |
$products = array(); |
| 141 |
|
| 142 |
foreach ( $cart_items as $cart_item ) { |
| 143 |
if ( ! empty( $cart_item['variation_id'] ) ) { |
| 144 |
$product_id = $cart_item['variation_id']; |
| 145 |
} else { |
| 146 |
$product_id = $cart_item['product_id']; |
| 147 |
} |
| 148 |
|
| 149 |
$products[] = $product_id; |
| 150 |
} |
| 151 |
|
| 152 |
return $products; |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|