get_parent_id() ); if ( $parent ) { return $parent->get_name(); } } return $product instanceof \WC_Product ? $product->get_name() : ''; } /** * Variation values and item data on one line: "L · Blue · Gift wrap: Yes". * * Plain text (escape it when printing). Attribute values are shown alone (the customer knows what "L" is); other data keeps its label. * * @param array $item Cart item. */ public static function meta_line( array $item ): string { $parts = array(); foreach ( self::data_list( $item ) as $entry ) { $label = trim( wp_strip_all_tags( (string) ( $entry['key'] ?? '' ) ) ); $value = trim( wp_strip_all_tags( (string) ( $entry['display'] ?? ( $entry['value'] ?? '' ) ) ) ); if ( '' === $value ) { continue; } if ( '' === $label ) { $parts[] = $value; } elseif ( ! empty( $entry['aiolos_attribute'] ) ) { // "Blue", "One size" read on their own; a short value keeps its name: "Size L", "EU 42". $parts[] = ( function_exists( 'mb_strlen' ) ? mb_strlen( $value ) : strlen( $value ) ) <= 3 ? $label . ' ' . $value : $value; } else { $parts[] = $label . ': ' . $value; } } return implode( ' · ', $parts ); } /** * Variation attributes and item data, as wc_get_formatted_cart_item_data() collects them; * attribute entries are marked aiolos_attribute. * * @param array $item Cart item. * @return array */ public static function data_list( array $item ): array { $product = $item['data']; $data = array(); if ( $product instanceof \WC_Product && $product->is_type( 'variation' ) && ! empty( $item['variation'] ) && is_array( $item['variation'] ) ) { foreach ( $item['variation'] as $name => $value ) { $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( (string) $name ) ) ); if ( taxonomy_exists( $taxonomy ) ) { $term = get_term_by( 'slug', $value, $taxonomy ); if ( ! is_wp_error( $term ) && $term && $term->name ) { $value = $term->name; } $label = wc_attribute_label( $taxonomy ); } else { $value = apply_filters( 'woocommerce_variation_option_name', $value, null, $taxonomy, $product ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment $label = wc_attribute_label( str_replace( 'attribute_', '', (string) $name ), $product ); } if ( '' === (string) $value ) { continue; } $data[] = array( 'key' => $label, 'value' => $value, 'aiolos_attribute' => true, ); } } return (array) apply_filters( 'woocommerce_get_item_data', $data, $item ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment } /** * Line subtotal, as the cart shows it. * * @param array $item Cart item. * @param string $key Cart item key. */ public static function subtotal( array $item, string $key ): string { $html = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $item['data'], $item['quantity'] ), $item, $key ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment return wp_kses_post( (string) $html ); } /** * Regular line subtotal when the product is on sale (for a struck-through price), else ''. * * @param array $item Cart item. */ public static function regular_subtotal( array $item ): string { $product = $item['data']; if ( ! $product instanceof \WC_Product || ! $product->is_on_sale() ) { return ''; } $regular = (float) $product->get_regular_price(); if ( $regular <= 0 ) { return ''; } $args = array( 'qty' => (int) $item['quantity'], 'price' => $regular, ); $amount = WC()->cart->display_prices_including_tax() ? wc_get_price_including_tax( $product, $args ) : wc_get_price_excluding_tax( $product, $args ); $current = WC()->cart->display_prices_including_tax() ? wc_get_price_including_tax( $product, array( 'qty' => (int) $item['quantity'] ) ) : wc_get_price_excluding_tax( $product, array( 'qty' => (int) $item['quantity'] ) ); return $amount > $current ? wp_kses_post( wc_price( $amount ) ) : ''; } /** * Sale percentage, e.g. 17, or 0. * * @param array $item Cart item. */ public static function sale_percent( array $item ): int { $product = $item['data']; if ( ! $product instanceof \WC_Product || ! $product->is_on_sale() ) { return 0; } $regular = (float) $product->get_regular_price(); $price = (float) $product->get_price(); return ( $regular > 0 && $price < $regular ) ? (int) round( ( 1 - $price / $regular ) * 100 ) : 0; } /** * Thumbnail HTML. * * @param array $item Cart item. * @param string $key Cart item key. * @param string $size Image size. */ public static function thumbnail( array $item, string $key, string $size = 'woocommerce_thumbnail' ): string { $product = $item['data']; $html = apply_filters( 'woocommerce_cart_item_thumbnail', $product->get_image( $size, array( 'loading' => 'lazy' ) ), $item, $key ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment return wp_kses_post( (string) $html ); } /** * Product permalink, or '' when the product is not visible. * * @param array $item Cart item. * @param string $key Cart item key. */ public static function permalink( array $item, string $key ): string { $product = $item['data']; $link = apply_filters( 'woocommerce_cart_item_permalink', $product->is_visible() ? $product->get_permalink( $item ) : '', $item, $key ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment return (string) $link; } /** * Stock badge: [ 'text' => ..., 'tone' => success|warning ] or null. * * @param array $item Cart item. * @param int $low_from Show "Only N left" at or under this stock (0: never). */ public static function stock_badge( array $item, int $low_from ): ?array { $product = $item['data']; if ( ! $product instanceof \WC_Product ) { return null; } if ( $product->managing_stock() && $low_from > 0 ) { $stock = (int) $product->get_stock_quantity(); if ( $stock > 0 && $stock <= $low_from ) { return array( /* translators: %d: units left in stock */ 'text' => sprintf( _n( 'Only %d left', 'Only %d left', $stock, 'aiolos-checkout-for-woocommerce' ), $stock ), 'tone' => 'warning', ); } } if ( $product->is_on_backorder( (int) $item['quantity'] ) ) { return array( 'text' => __( 'On backorder', 'aiolos-checkout-for-woocommerce' ), 'tone' => 'warning', ); } return null; } /** * Whether the quantity can be changed (not sold individually, not locked by another plugin). * * @param array $item Cart item. * @param string $key Cart item key. */ public static function quantity_editable( array $item, string $key ): bool { $product = $item['data']; $editable = $product instanceof \WC_Product && ! $product->is_sold_individually() && ! self::is_child_item( $item ) && ! self::quantity_locked( $item, $key ); if ( $editable ) { // A quantity that can only be one value (min = max) is not a choice. $args = self::quantity_args( $item, $key ); $editable = ! ( $args['max'] > 0 && $args['max'] <= $args['min'] ); } /** * Filters whether a cart item's quantity can be changed in the side cart. * * @since 1.0.0 * * @param bool $editable Whether it can. * @param array $item Cart item. * @param string $key Cart item key. */ return (bool) apply_filters( 'aiolos_cart_item_quantity_editable', $editable, $item, $key ); } /** * Whether the item belongs to another item (a product in a bundle, a composite, a grouped * kit): its quantity and removal follow its parent. * * @param array $item Cart item. */ public static function is_child_item( array $item ): bool { foreach ( array( 'bundled_by', 'composite_parent', 'woosb_parent_id', 'wooco_parent_id', 'mnm_container', 'yith_wapo_parent', 'gift_card_parent' ) as $key ) { if ( ! empty( $item[ $key ] ) ) { return true; } } /** * Filters whether a cart item is part of another item. * * @since 1.0.0 * * @param bool $child Whether it is. * @param array $item Cart item. */ return (bool) apply_filters( 'aiolos_cart_item_is_child', false, $item ); } /** * The quantity rules of the cart page's quantity box, through the same filters * (`woocommerce_quantity_input_step` and `woocommerce_quantity_input_args`), so * Min/Max Quantities, packs of 6 and similar plugins apply here too. * * @param array $item Cart item. * @param string $key Cart item key ('' when unknown). * @return array min (0 or more), max (0: no limit), step (more than 0). */ public static function quantity_args( array $item, string $key = '' ): array { static $cache = array(); $product = $item['data'] ?? null; if ( ! $product instanceof \WC_Product ) { return array( 'min' => 0, 'max' => 0, 'step' => 1, ); } $cache_key = $key . '|' . $product->get_id() . '|' . ( $item['quantity'] ?? 0 ); if ( isset( $cache[ $cache_key ] ) ) { return $cache[ $cache_key ]; } // As templates/cart/cart.php: 0 lets the customer remove the item. $sold_individually = $product->is_sold_individually(); $defaults = array( 'input_name' => '' !== $key ? 'cart[' . $key . '][qty]' : 'quantity', 'input_value' => $item['quantity'] ?? 1, // The cart page passes these two itself (so their _min/_max filters do not apply there). 'max_value' => $sold_individually ? 1 : $product->get_max_purchase_quantity(), 'min_value' => $sold_individually ? 1 : 0, 'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ), // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment 'product_name' => $product->get_name(), ); $args = apply_filters( 'woocommerce_quantity_input_args', $defaults, $product ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment $args = is_array( $args ) ? $args : $defaults; $min = is_numeric( $args['min_value'] ?? null ) ? max( 0.0, (float) $args['min_value'] ) : 0.0; $max = is_numeric( $args['max_value'] ?? null ) ? max( 0.0, (float) $args['max_value'] ) : 0.0; $step = is_numeric( $args['step'] ?? null ) ? (float) $args['step'] : 1.0; if ( $max > 0 && $max < $min ) { $max = $min; } $cache[ $cache_key ] = array( 'min' => $min, 'max' => $max, 'step' => $step > 0 ? $step : 1.0, ); return $cache[ $cache_key ]; } /** * Whether another plugin replaced the cart page's quantity box for this item with something * that is not a quantity box (bundles, packs, free gifts print a fixed number): then the * quantity is shown, not edited. * * @param array $item Cart item. * @param string $key Cart item key. */ public static function quantity_locked( array $item, string $key ): bool { if ( ! has_filter( 'woocommerce_cart_item_quantity' ) || ! function_exists( 'woocommerce_quantity_input' ) || ! $item['data'] instanceof \WC_Product ) { return false; } $args = self::quantity_args( $item, $key ); $default = woocommerce_quantity_input( array( 'input_name' => 'cart[' . $key . '][qty]', 'input_value' => $item['quantity'], 'max_value' => $args['max'] > 0 ? $args['max'] : -1, 'min_value' => $args['min'], ), $item['data'], false ); $html = apply_filters( 'woocommerce_cart_item_quantity', $default, $key, $item ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment if ( ! is_string( $html ) || $html === $default ) { return false; } // Wrapped or restyled (themes add +/- buttons around it): still a quantity box. return ! preg_match( '/]*\bname=["\']cart\[' . preg_quote( $key, '/' ) . '\]\[qty\]["\'])(?![^>]*\btype=["\']hidden["\'])[^>]*>/i', $html ); } /** * Whether the customer may remove the item: not a part of another item, and WooCommerce's * `woocommerce_cart_item_remove_link` not emptied by a plugin (free gifts, required items). * * @param array $item Cart item. * @param string $key Cart item key. */ public static function removable( array $item, string $key ): bool { if ( self::is_child_item( $item ) ) { return false; } $product = $item['data'] ?? null; $name = $product instanceof \WC_Product ? wp_strip_all_tags( $product->get_name() ) : ''; $link = sprintf( '×', esc_url( wc_get_cart_remove_url( $key ) ), /* translators: %s: product name (WooCommerce's own wording) */ esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), $name ) ), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- WooCommerce's own label, as its cart page passes it to the filter. esc_attr( (string) ( $item['product_id'] ?? 0 ) ), esc_attr( $product instanceof \WC_Product ? $product->get_sku() : '' ) ); $link = apply_filters( 'woocommerce_cart_item_remove_link', $link, $key ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment return is_string( $link ) && '' !== trim( $link ); } /** * The row's extra classes from `woocommerce_cart_item_class` (sanitized), plus is-child for a * part of another item. * * @param array $item Cart item. * @param string $key Cart item key. */ public static function row_class( array $item, string $key ): string { $classes = apply_filters( 'woocommerce_cart_item_class', 'cart_item', $item, $key ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment $clean = array(); foreach ( preg_split( '/\s+/', is_string( $classes ) ? $classes : '' ) as $class ) { $class = sanitize_html_class( $class ); if ( '' !== $class ) { $clean[] = $class; } } if ( self::is_child_item( $item ) ) { $clean[] = 'is-child'; } return implode( ' ', array_unique( $clean ) ); } /** * What the customer saves on this line against the regular price (displayed with or * without tax, as the cart shows prices), or 0. * * @param array $item Cart item. */ public static function saving( array $item ): float { $product = $item['data'] ?? null; if ( ! $product instanceof \WC_Product || ! $product->is_on_sale() || ! WC()->cart ) { return 0.0; } $regular = (float) $product->get_regular_price(); if ( $regular <= 0 ) { return 0.0; } $qty = (float) $item['quantity']; $with_tax = WC()->cart->display_prices_including_tax(); $was = $with_tax ? wc_get_price_including_tax( $product, array( 'qty' => $qty, 'price' => $regular ) ) : wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $regular ) ); // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound $now = $with_tax ? wc_get_price_including_tax( $product, array( 'qty' => $qty ) ) : wc_get_price_excluding_tax( $product, array( 'qty' => $qty ) ); $saving = round( (float) $was - (float) $now, wc_get_price_decimals() ); return $saving > 0 ? $saving : 0.0; } /** * Everything a cart row needs, in one call (the checkout summary and the side cart use it). * * @param array $item Cart item. * @param string $key Cart item key. * @return array name (HTML), base_name (text), meta (text), link, thumbnail (HTML), subtotal * (HTML), regular (HTML or ''), saving (float), saving_html (HTML or ''), * sale_percent, child, editable, removable, quantity, min, max, step, class. */ public static function view( array $item, string $key ): array { $args = self::quantity_args( $item, $key ); $saving = self::saving( $item ); return array( 'name' => self::name( $item, $key ), 'base_name' => wp_strip_all_tags( self::base_name( $item ) ), 'meta' => self::meta_line( $item ), 'link' => self::permalink( $item, $key ), 'thumbnail' => self::thumbnail( $item, $key ), 'subtotal' => self::subtotal( $item, $key ), 'regular' => self::regular_subtotal( $item ), 'saving' => $saving, 'saving_html' => $saving > 0 ? wp_kses_post( wc_price( $saving ) ) : '', 'sale_percent' => self::sale_percent( $item ), 'child' => self::is_child_item( $item ), 'editable' => self::quantity_editable( $item, $key ), 'removable' => self::removable( $item, $key ), 'quantity' => (float) $item['quantity'], 'min' => $args['min'], 'max' => $args['max'], 'step' => $args['step'], 'class' => self::row_class( $item, $key ), ); } /** * A requested quantity moved onto the item's rules: a whole number of steps, between the * minimum and the maximum. 0 stays 0 (removal). * * @param array $item Cart item. * @param float $quantity Requested quantity. * @param array $args quantity_args(). */ public static function allowed_quantity( array $item, float $quantity, array $args ): float { if ( $quantity <= 0 ) { return 0.0; } $step = (float) $args['step']; $base = (float) $args['min']; if ( $step > 0 ) { $quantity = $base + round( ( $quantity - $base ) / $step ) * $step; } if ( $quantity < $args['min'] ) { $quantity = (float) $args['min']; } if ( $args['max'] > 0 && $quantity > $args['max'] ) { // The largest whole number of steps under the maximum. $quantity = $step > 0 ? $base + floor( ( (float) $args['max'] - $base ) / $step + 1e-9 ) * $step : (float) $args['max']; } $quantity = max( $quantity, (float) $args['min'] ); return $quantity > 0 ? $quantity : (float) max( 1, $item['quantity'] ?? 1 ); } /** * Largest quantity allowed, or 0 for no limit. * * @param array $item Cart item. */ public static function max_quantity( array $item ): int { $product = $item['data']; if ( ! $product instanceof \WC_Product ) { return 0; } $max = (int) $product->get_max_purchase_quantity(); $args = self::quantity_args( $item ); if ( $args['max'] > 0 ) { $max = $max > 0 ? min( $max, (int) $args['max'] ) : (int) $args['max']; } return $max > 0 ? $max : 0; } /** * Changes a cart item's quantity (0 removes it) through WooCommerce's cart API and the cart * page's validation, which plugins use for their min/max/step rules. * * The quantity is moved onto the item's rules first (minimum, maximum, step). * * @param string $key Cart item key. * @param int|float $quantity New quantity. * @return array errors (string[]), removed (key and name of a removed item, or null), missing (bool). */ public static function set_quantity( string $key, $quantity ): array { $quantity = is_numeric( $quantity ) ? (float) $quantity : 0.0; $cart = WC()->cart; $item = '' !== $key ? $cart->get_cart_item( $key ) : array(); $result = array( 'errors' => array(), 'removed' => null, 'missing' => false, ); if ( ! $item ) { $result['missing'] = true; $result['errors'] = array( __( 'This product is no longer in your cart.', 'aiolos-checkout-for-woocommerce' ) ); return $result; } wc_clear_notices(); $changed = false; if ( $quantity <= 0 && ! self::removable( $item, $key ) ) { // A free gift or a required item whose plugin hides the remove link. $result['errors'][] = __( 'This product cannot be removed from your cart.', 'aiolos-checkout-for-woocommerce' ); } elseif ( $quantity <= 0 ) { $result['removed'] = array( 'key' => $key, 'name' => wp_strip_all_tags( $item['data']->get_name() ), ); $cart->remove_cart_item( $key ); $changed = true; } elseif ( self::quantity_editable( $item, $key ) ) { $args = self::quantity_args( $item, $key ); $max = self::max_quantity( $item ); $allowed = self::allowed_quantity( $item, (float) $quantity, array_merge( $args, array( 'max' => (float) $max ) ) ); $allowed = function_exists( 'wc_stock_amount' ) ? wc_stock_amount( $allowed ) : $allowed; if ( (float) $allowed !== (float) $quantity ) { if ( $max > 0 && $quantity > $max ) { /* translators: %d: largest quantity allowed */ wc_add_notice( sprintf( __( 'You can buy at most %d of this product.', 'aiolos-checkout-for-woocommerce' ), $max ), 'error' ); } elseif ( $quantity < $args['min'] ) { /* translators: %s: smallest quantity allowed */ wc_add_notice( sprintf( __( 'The minimum quantity for this product is %s.', 'aiolos-checkout-for-woocommerce' ), wc_format_localized_decimal( (string) $args['min'] ) ), 'error' ); } else { /* translators: %s: quantity step, e.g. 6 */ wc_add_notice( sprintf( __( 'This product is sold in multiples of %s.', 'aiolos-checkout-for-woocommerce' ), wc_format_localized_decimal( (string) $args['step'] ) ), 'error' ); } $quantity = $allowed; } if ( (float) $quantity !== (float) $item['quantity'] && apply_filters( 'woocommerce_update_cart_validation', true, $key, $item, $quantity ) ) { // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment $cart->set_quantity( $key, $quantity, false ); $changed = true; } } if ( $changed ) { // The cart page's hook after an update; WooCommerce then recalculates (and fires // woocommerce_cart_updated) when the caller calls calculate_totals(). apply_filters( 'woocommerce_update_cart_action_cart_updated', true ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment /** * Fires after a quantity change or removal from the side cart or the checkout summary. * * @since 1.6.0 * * @param string $key Cart item key. * @param float $quantity New quantity (0: removed). */ do_action( 'aiolos_cart_item_updated', $key, (float) $quantity ); } foreach ( wc_get_notices( 'error' ) as $notice ) { $text = is_array( $notice ) ? ( $notice['notice'] ?? '' ) : $notice; $text = trim( html_entity_decode( wp_strip_all_tags( (string) $text ), ENT_QUOTES, 'UTF-8' ) ); if ( '' !== $text ) { $result['errors'][] = $text; } } wc_clear_notices(); return $result; } }