unique-product-count-in-cart.php
· 1.2 KiB · PHP
原始文件
/* START Unique product count in cart */
// Shortcode to display the number of unique products in the cart with styling
function display_unique_cart_product_count_styled() {
// Initialize an empty array to store unique product IDs
$unique_product_ids = array();
// Get the cart contents
$cart_contents = WC()->cart->get_cart();
// Loop through each item in the cart
foreach ($cart_contents as $cart_item) {
// Get the product ID
$product_id = $cart_item['product_id'];
// Check if the product ID is not already in the array
if (!in_array($product_id, $unique_product_ids)) {
// If not, add it to the array
$unique_product_ids[] = $product_id;
}
}
// Count the number of unique products
$unique_product_count = count($unique_product_ids);
// Style the output
$output = '<span style="color: #21201E; font-family: Nexa-Bold, sans-serif; font-size: 24px; line-height: 32px;">Tu pedido (' . $unique_product_count . ' productos)</span>';
// Return the styled output
return $output;
}
add_shortcode('unique_cart_product_count_styled', 'display_unique_cart_product_count_styled');
/* END Unique product count in cart */
1 | /* START Unique product count in cart */ |
2 | // Shortcode to display the number of unique products in the cart with styling |
3 | function display_unique_cart_product_count_styled() { |
4 | // Initialize an empty array to store unique product IDs |
5 | $unique_product_ids = array(); |
6 | |
7 | // Get the cart contents |
8 | $cart_contents = WC()->cart->get_cart(); |
9 | |
10 | // Loop through each item in the cart |
11 | foreach ($cart_contents as $cart_item) { |
12 | // Get the product ID |
13 | $product_id = $cart_item['product_id']; |
14 | |
15 | // Check if the product ID is not already in the array |
16 | if (!in_array($product_id, $unique_product_ids)) { |
17 | // If not, add it to the array |
18 | $unique_product_ids[] = $product_id; |
19 | } |
20 | } |
21 | |
22 | // Count the number of unique products |
23 | $unique_product_count = count($unique_product_ids); |
24 | |
25 | // Style the output |
26 | $output = '<span style="color: #21201E; font-family: Nexa-Bold, sans-serif; font-size: 24px; line-height: 32px;">Tu pedido (' . $unique_product_count . ' productos)</span>'; |
27 | |
28 | // Return the styled output |
29 | return $output; |
30 | } |
31 | add_shortcode('unique_cart_product_count_styled', 'display_unique_cart_product_count_styled'); |
32 | |
33 | /* END Unique product count in cart */ |
34 |