cart ) ) { wc_load_cart(); } } /** * Add product to cart with validation * * @param int $product_id Product ID * @param int $quantity Quantity * @param int $variation_id Variation ID * @param array $variation Variation attributes * @return array Result */ public function add_to_cart( $product_id, $quantity = 1, $variation_id = 0, $variation = array() ) { // Validate product $validation = $this->validate_product( $product_id, $quantity, $variation_id ); if ( !$validation['valid'] ) { return array( 'success' => false, 'message' => $validation['message'], ); } // Initialize cart $this->maybe_init_cart(); try { // Add to cart $cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ); if ( $cart_item_key ) { $product = wc_get_product( $product_id ); return array( 'success' => true, 'message' => sprintf( /* translators: %s: product name */ __( '"%s" has been added to your cart.', 'aica-smart-ai-commerce-assistant' ), $product->get_name() ), 'cart_item_key' => $cart_item_key, 'cart' => $this->get_cart_data(), ); } return array( 'success' => false, 'message' => __( 'Failed to add product to cart.', 'aica-smart-ai-commerce-assistant' ), ); } catch ( Exception $e ) { return array( 'success' => false, 'message' => $e->getMessage(), ); } } /** * Update cart item quantity * * @param string $cart_item_key Cart item key * @param int $quantity New quantity * @return array Result */ public function update_quantity( $cart_item_key, $quantity ) { $this->maybe_init_cart(); if ( $quantity <= 0 ) { return $this->remove_from_cart( $cart_item_key ); } // Validate quantity $cart_item = WC()->cart->get_cart_item( $cart_item_key ); if ( !$cart_item ) { return array( 'success' => false, 'message' => __( 'Cart item not found.', 'aica-smart-ai-commerce-assistant' ), ); } $product = $cart_item['data']; // Check stock if ( $product->managing_stock() ) { $stock_quantity = $product->get_stock_quantity(); if ( $quantity > $stock_quantity ) { return array( 'success' => false, 'message' => sprintf( /* translators: %d: stock quantity */ __( 'Only %d items available in stock.', 'aica-smart-ai-commerce-assistant' ), $stock_quantity ), ); } } // Update quantity WC()->cart->set_quantity( $cart_item_key, $quantity ); return array( 'success' => true, 'message' => __( 'Cart updated successfully.', 'aica-smart-ai-commerce-assistant' ), 'cart' => $this->get_cart_data(), ); } /** * Remove item from cart * * @param string $cart_item_key Cart item key * @return array Result */ public function remove_from_cart( $cart_item_key ) { $this->maybe_init_cart(); $cart_item = WC()->cart->get_cart_item( $cart_item_key ); if ( !$cart_item ) { return array( 'success' => false, 'message' => __( 'Cart item not found.', 'aica-smart-ai-commerce-assistant' ), ); } $product = $cart_item['data']; if ( WC()->cart->remove_cart_item( $cart_item_key ) ) { return array( 'success' => true, 'message' => sprintf( /* translators: %s: product name */ __( '"%s" has been removed from your cart.', 'aica-smart-ai-commerce-assistant' ), $product->get_name() ), 'cart' => $this->get_cart_data(), ); } return array( 'success' => false, 'message' => __( 'Failed to remove item from cart.', 'aica-smart-ai-commerce-assistant' ), ); } /** * Clear cart * * @return array Result */ public function clear_cart() { $this->maybe_init_cart(); WC()->cart->empty_cart(); return array( 'success' => true, 'message' => __( 'Cart has been cleared.', 'aica-smart-ai-commerce-assistant' ), 'cart' => $this->get_cart_data(), ); } /** * Get cart data * * @return array Cart data */ public function get_cart_data() { $this->maybe_init_cart(); $cart_items = array(); foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $cart_items[] = array( 'key' => $cart_item_key, 'product_id' => $cart_item['product_id'], 'variation_id' => $cart_item['variation_id'], 'name' => $product->get_name(), 'quantity' => $cart_item['quantity'], 'price' => $product->get_price(), 'subtotal' => $cart_item['line_subtotal'], 'total' => $cart_item['line_total'], 'image' => wp_get_attachment_url( $product->get_image_id() ), 'url' => $product->get_permalink(), ); } return array( 'items' => $cart_items, 'item_count' => WC()->cart->get_cart_contents_count(), 'subtotal' => WC()->cart->get_subtotal(), 'subtotal_display' => wc_price( WC()->cart->get_subtotal() ), 'total' => WC()->cart->get_total( 'edit' ), 'total_display' => WC()->cart->get_total(), 'needs_shipping' => WC()->cart->needs_shipping(), 'checkout_url' => wc_get_checkout_url(), ); } /** * Validate product before adding to cart * * @param int $product_id Product ID * @param int $quantity Quantity * @param int $variation_id Variation ID * @return array Validation result */ private function validate_product( $product_id, $quantity, $variation_id = 0 ) { // Get product $product = wc_get_product( $variation_id > 0 ? $variation_id : $product_id ); if ( !$product ) { return array( 'valid' => false, 'message' => __( 'Product not found.', 'aica-smart-ai-commerce-assistant' ), ); } // Check if product is purchasable if ( !$product->is_purchasable() ) { return array( 'valid' => false, 'message' => __( 'This product cannot be purchased.', 'aica-smart-ai-commerce-assistant' ), ); } // Check stock if ( !$product->is_in_stock() ) { return array( 'valid' => false, 'message' => __( 'This product is out of stock.', 'aica-smart-ai-commerce-assistant' ), ); } // Check quantity availability if ( $product->managing_stock() ) { $stock_quantity = $product->get_stock_quantity(); if ( $quantity > $stock_quantity ) { return array( 'valid' => false, 'message' => sprintf( /* translators: %d: stock quantity */ __( 'Only %d items available in stock.', 'aica-smart-ai-commerce-assistant' ), $stock_quantity ), ); } } // Check if variation is valid if ( $product->is_type( 'variable' ) && $variation_id === 0 ) { return array( 'valid' => false, 'message' => __( 'Please select product options.', 'aica-smart-ai-commerce-assistant' ), ); } return array( 'valid' => true, ); } /** * Apply coupon to cart * * @param string $coupon_code Coupon code * @return array Result */ public function apply_coupon( $coupon_code ) { $this->maybe_init_cart(); $coupon_code = sanitize_text_field( $coupon_code ); if ( WC()->cart->apply_coupon( $coupon_code ) ) { return array( 'success' => true, 'message' => sprintf( /* translators: %s: coupon code */ __( 'Coupon "%s" applied successfully.', 'aica-smart-ai-commerce-assistant' ), $coupon_code ), 'cart' => $this->get_cart_data(), ); } return array( 'success' => false, 'message' => __( 'Failed to apply coupon.', 'aica-smart-ai-commerce-assistant' ), ); } /** * Remove coupon from cart * * @param string $coupon_code Coupon code * @return array Result */ public function remove_coupon( $coupon_code ) { $this->maybe_init_cart(); if ( WC()->cart->remove_coupon( $coupon_code ) ) { return array( 'success' => true, 'message' => sprintf( /* translators: %s: coupon code */ __( 'Coupon "%s" removed.', 'aica-smart-ai-commerce-assistant' ), $coupon_code ), 'cart' => $this->get_cart_data(), ); } return array( 'success' => false, 'message' => __( 'Failed to remove coupon.', 'aica-smart-ai-commerce-assistant' ), ); } /** * Get product variations for AI context * * @param int $product_id Product ID * @return array Variations */ public function get_product_variations( $product_id ) { $product = wc_get_product( $product_id ); if ( !$product || !$product->is_type( 'variable' ) ) { return array(); } $variations = array(); $available_variations = $product->get_available_variations(); foreach ( $available_variations as $variation ) { $variation_obj = wc_get_product( $variation['variation_id'] ); $variations[] = array( 'variation_id' => $variation['variation_id'], 'attributes' => $variation['attributes'], 'price' => $variation_obj->get_price(), 'in_stock' => $variation['is_in_stock'], 'display_name' => implode( ' / ', array_map( function ( $attr ) { return ucfirst( str_replace( 'attribute_', '', $attr ) ); }, $variation['attributes'] ) ), ); } return $variations; } }