namespace, '/offers', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_offers' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); // Add product directly to cart. register_rest_route( $this->namespace, '/offers/add-to-cart', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'add_to_cart' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'product_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'quantity' => array( 'required' => false, 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', ), ), ), ) ); } /** * Get personalized offers and recommendations. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_offers( $request ) { $customer_id = $this->get_customer_id(); // Get customer's past orders. $orders = wc_get_orders( array( 'customer_id' => $customer_id, 'limit' => 10, 'status' => array( 'completed', 'processing' ), ) ); $purchased_product_ids = array(); $purchased_cat_ids = array(); foreach ( $orders as $order ) { foreach ( $order->get_items() as $item ) { $pid = $item->get_product_id(); if ( $pid ) { $purchased_product_ids[] = $pid; $cats = wc_get_product_term_ids( $pid, 'product_cat' ); if ( ! empty( $cats ) ) { $purchased_cat_ids = array_merge( $purchased_cat_ids, $cats ); } } } } $purchased_product_ids = array_unique( $purchased_product_ids ); $purchased_cat_ids = array_unique( $purchased_cat_ids ); // 1. Buy It Again (Repurchase). $buy_again = array(); foreach ( array_slice( $purchased_product_ids, 0, 6 ) as $pid ) { $product = wc_get_product( $pid ); if ( $product && $product->is_in_stock() && $product->is_purchasable() ) { $buy_again[] = $this->format_product( $product ); } } // 2. Recommended For You (based on categories of past purchases, excluding already purchased). $recommended_query_args = array( 'status' => 'publish', 'limit' => 6, 'stock_status' => 'instock', 'orderby' => 'popularity', 'order' => 'DESC', ); if ( ! empty( $purchased_cat_ids ) ) { $recommended_query_args['category'] = $purchased_cat_ids; } if ( ! empty( $purchased_product_ids ) ) { $recommended_query_args['exclude'] = $purchased_product_ids; } $recommended_products = wc_get_products( $recommended_query_args ); $recommended = array(); foreach ( $recommended_products as $product ) { $recommended[] = $this->format_product( $product ); } // 3. Special Deals (On Sale items in preferred categories or storewide). $deals_query_args = array( 'status' => 'publish', 'limit' => 6, 'stock_status' => 'instock', 'on_sale' => true, 'orderby' => 'date', 'order' => 'DESC', ); if ( ! empty( $purchased_cat_ids ) ) { $deals_query_args['category'] = $purchased_cat_ids; } $deals_products = wc_get_products( $deals_query_args ); $deals = array(); foreach ( $deals_products as $product ) { $deals[] = $this->format_product( $product ); } return rest_ensure_response( array( 'buy_again' => $buy_again, 'recommended' => $recommended, 'deals' => $deals, ) ); } /** * Add product to cart. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function add_to_cart( $request ) { $product_id = absint( $request->get_param( 'product_id' ) ); $quantity = max( 1, absint( $request->get_param( 'quantity' ) ) ); $product = wc_get_product( $product_id ); if ( ! $product || ! $product->is_purchasable() ) { return new WP_Error( 'rest_invalid_product', __( 'This product cannot be purchased.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } if ( ! $product->is_in_stock() ) { return new WP_Error( 'rest_out_of_stock', __( 'This product is currently out of stock.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Ensure cart is loaded. if ( null === WC()->cart ) { wc_load_cart(); } $cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity ); if ( ! $cart_item_key ) { return new WP_Error( 'rest_cart_error', __( 'Failed to add product to cart.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } return rest_ensure_response( array( 'success' => true, 'message' => sprintf( __( '"%s" has been added to your cart.', 'advanced-customer-account' ), $product->get_name() ), 'cart_count' => WC()->cart->get_cart_contents_count(), 'cart_subtotal' => WC()->cart->get_cart_subtotal(), 'cart_url' => wc_get_cart_url(), 'checkout_url' => wc_get_checkout_url(), ) ); } /** * Format product object for REST response. * * @param WC_Product $product Product object. * @return array */ private function format_product( $product ) { $image_id = $product->get_image_id(); $image_src = $image_id ? wp_get_attachment_image_url( $image_id, 'medium' ) : wc_placeholder_img_src(); return array( 'id' => $product->get_id(), 'name' => $product->get_name(), 'price' => $this->format_price( $product->get_price() ), 'regular_price' => $product->get_regular_price() ? $this->format_price( $product->get_regular_price() ) : null, 'sale_price' => $product->get_sale_price() ? $this->format_price( $product->get_sale_price() ) : null, 'on_sale' => $product->is_on_sale(), 'image' => $image_src, 'permalink' => $product->get_permalink(), 'average_rating' => (float) $product->get_average_rating(), 'rating_count' => (int) $product->get_rating_count(), 'is_purchasable' => $product->is_purchasable() && $product->is_in_stock() && $product->is_type( 'simple' ), ); } }