Add to cart API Issue
-
Hello there,
When I call add_to_cart API then it is displaying some error like this.<!DOCTYPE html> <!-- Ticket #11289, IE bug fix: always pad the error page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono --> <html> </html>
Below is My api code:
function add_to_cart_endpoint_handler( $data = array() ) { $product_id = ! isset( $data['product_id'] ) ? 0 : absint( $data['product_id'] ); $quantity = ! isset( $data['quantity'] ) ? 1 : absint( $data['quantity'] ); $variation_id = ! isset( $data['variation_id'] ) ? 0 : absint( $data['variation_id'] ); $variation = ! isset( $data['variation'] ) ? array() : $data['variation']; $cart_item_data = ! isset( $data['cart_item_data'] ) ? array() : $data['cart_item_data']; if ( $product_id <= 0 ) { return new WP_Error( 'wc_cart_rest_product_id_required', __( 'Product ID number is required!', 'woocommerce' ), array( 'status' => 500 ) ); } if ( ! is_numeric( $product_id ) ) { return new WP_Error( 'wc_cart_rest_product_id_not_numeric', __( 'Product ID must be numeric!', 'woocommerce' ), array( 'status' => 500 ) ); } $product_data = wc_get_product( $variation_id ? $variation_id : $product_id ); if ( $quantity <= 0 || ! $product_data || 'trash' === $product_data->get_status() ) { return new WP_Error( 'wc_cart_rest_product_does_not_exist', __( 'Warning: This product does not exist!', 'woocommerce' ), array( 'status' => 500 ) ); } // Force quantity to 1 if sold individually and check for existing item in cart. if ( $product_data->is_sold_individually() ) { $quantity = 1; $cart_contents = WC()->cart->cart_contents; $found_in_cart = apply_filters( 'woocommerce_add_to_cart_sold_individually_found_in_cart', $cart_item_key && $cart_contents[ $cart_item_key ]['quantity'] > 0, $product_id, $variation_id, $cart_item_data, $cart_id ); if ( $found_in_cart ) { /* translators: %s: product name */ return new WP_Error( 'wc_cart_rest_product_sold_individually', sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_name() ), array( 'status' => 500 ) ); } } // Product is purchasable check. if ( ! $product_data->is_purchasable() ) { return new WP_Error( 'wc_cart_rest_cannot_be_purchased', __( 'Sorry, this product cannot be purchased.', 'woocommerce' ), array( 'status' => 500 ) ); } // Stock check - only check if we're managing stock and backorders are not allowed. if ( ! $product_data->is_in_stock() ) { return new WP_Error( 'wc_cart_rest_product_out_of_stock', sprintf( __( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() ), array( 'status' => 500 ) ); } if ( ! $product_data->has_enough_stock( $quantity ) ) { /* translators: 1: product name 2: quantity in stock */ return new WP_Error( 'wc_cart_rest_not_enough_in_stock', sprintf( __( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ), $product_data->get_name(), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ) ), array( 'status' => 500 ) ); } // Stock check - this time accounting for whats already in-cart. if ( $product_data->managing_stock() ) { $products_qty_in_cart = WC()->cart->get_cart_item_quantities(); if ( isset( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] ) && ! $product_data->has_enough_stock( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ] + $quantity ) ) { return new WP_Error( 'wc_cart_rest_not_enough_stock_remaining', sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $product_data->get_stock_quantity(), $product_data ), wc_format_stock_quantity_for_display( $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ], $product_data ) ), array( 'status' => 500 ) ); } } // Add item to cart. $item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data ); // $item_key = WC()->cart->add_to_cart( $product_id, $quantity ); // Return response to added item to cart or return error. if ( $item_key ) { $data = WC()->cart->get_cart_item( $item_key ); do_action( 'wc_cart_rest_add_to_cart', $item_key, $data ); if ( is_array( $data ) ) { return new WP_REST_Response( $data, 200 ); } } else { return new WP_Error( 'wc_cart_rest_cannot_add_to_cart', sprintf( __( 'You cannot add "%s" to your cart.', 'woocommerce' ), $product_data->get_name() ), array( 'status' => 500 ) ); } }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Add to cart API Issue’ is closed to new replies.