• Resolved jitendrabanjara1991

    (@jitendrabanjara1991)


    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 &quot;%s&quot; 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 &quot;%1$s&quot; 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 &mdash; 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 ) );
        }
    }
    
    • This topic was modified 5 years, 4 months ago by Jan Dembowski. Reason: Deleted bump and formatted post
Viewing 3 replies - 1 through 3 (of 3 total)
  • Stef

    (@serafinnyc)

    Can you grab a system report please?

    Thread Starter jitendrabanjara1991

    (@jitendrabanjara1991)

    Hello @serafinnyc,

    Thanks for the response.

    When I activate new WooCommerce version then It will display above error which was asked in first support topic.

    When I activate WooCommerce 3.5.8 then it’s working fine. Please check system report with 3.5.8 version.

    
    ### WordPress Environment ###
    
    Home URL: https://erti.shop
    Site URL: https://erti.shop
    WC Version: 3.5.8
    Log Directory Writable: ✔
    WP Version: 5.2
    WP Multisite: –
    WP Memory Limit: 256 MB
    WP Debug Mode: ✔
    WP Cron: ✔
    Language: lv
    External object cache: ✔
    
    ### Server Environment ###
    
    Server Info: nginx
    PHP Version: 7.2.18
    PHP Post Max Size: 100 MB
    PHP Time Limit: 300
    PHP Max Input Vars: 6144
    cURL Version: 7.64.1
    OpenSSL/1.1.0j
    
    SUHOSIN Installed: –
    MySQL Version: 5.5.5-10.1.17-MariaDB
    Max Upload Size: 100 MB
    Default Timezone is UTC: ✔
    fsockopen/cURL: ✔
    SoapClient: ✔
    DOMDocument: ✔
    GZip: ✔
    Multibyte String: ✔
    Remote Post: ✔
    Remote Get: ✔
    
    ### Database ###
    
    WC Database Version: 3.5.8
    WC Database Prefix: wp_
    MaxMind GeoIP Database: ✔
    Total Database Size: 59.50MB
    Database Data Size: 46.87MB
    Database Index Size: 12.63MB
    wp_woocommerce_sessions: Data: 0.33MB + Index: 0.02MB
    wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.05MB
    wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_order_itemmeta: Data: 0.13MB + Index: 0.16MB
    wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB
    wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB
    wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB
    wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB
    wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB
    wp_aws_index: Data: 0.09MB + Index: 0.00MB
    wp_commentmeta: Data: 0.00MB + Index: 0.01MB
    wp_comments: Data: 0.08MB + Index: 0.09MB
    wp_css_js_manager: Data: 0.02MB + Index: 0.00MB
    wp_eg_grids: Data: 0.02MB + Index: 0.02MB
    wp_eg_item_elements: Data: 0.02MB + Index: 0.02MB
    wp_eg_item_skins: Data: 1.52MB + Index: 0.03MB
    wp_eg_navigation_skins: Data: 0.06MB + Index: 0.02MB
    wp_emporos_messages: Data: 0.02MB + Index: 0.00MB
    wp_ewwwio_images: Data: 0.02MB + Index: 0.03MB
    wp_ewwwio_queue: Data: 0.02MB + Index: 0.02MB
    wp_icl_cms_nav_cache: Data: 0.02MB + Index: 0.00MB
    wp_icl_content_status: Data: 0.02MB + Index: 0.02MB
    wp_icl_core_status: Data: 0.02MB + Index: 0.02MB
    wp_icl_flags: Data: 0.02MB + Index: 0.02MB
    wp_icl_languages: Data: 0.02MB + Index: 0.03MB
    wp_icl_languages_translations: Data: 0.19MB + Index: 0.11MB
    wp_icl_locale_map: Data: 0.02MB + Index: 0.00MB
    wp_icl_message_status: Data: 0.02MB + Index: 0.03MB
    wp_icl_mo_files_domains: Data: 0.02MB + Index: 0.02MB
    wp_icl_node: Data: 0.02MB + Index: 0.00MB
    wp_icl_reminders: Data: 0.02MB + Index: 0.00MB
    wp_icl_strings: Data: 3.52MB + Index: 4.88MB
    wp_icl_string_packages: Data: 0.02MB + Index: 0.00MB
    wp_icl_string_pages: Data: 0.02MB + Index: 0.02MB
    wp_icl_string_positions: Data: 0.02MB + Index: 0.02MB
    wp_icl_string_status: Data: 0.02MB + Index: 0.02MB
    wp_icl_string_translations: Data: 0.09MB + Index: 0.05MB
    wp_icl_string_urls: Data: 0.02MB + Index: 0.02MB
    wp_icl_translate: Data: 1.52MB + Index: 0.02MB
    wp_icl_translate_job: Data: 0.02MB + Index: 0.02MB
    wp_icl_translations: Data: 0.28MB + Index: 0.69MB
    wp_icl_translation_batches: Data: 0.02MB + Index: 0.00MB
    wp_icl_translation_status: Data: 1.44MB + Index: 0.09MB
    wp_links: Data: 0.02MB + Index: 0.02MB
    wp_mc_banklinks: Data: 0.02MB + Index: 0.00MB
    wp_options: Data: 4.06MB + Index: 0.06MB
    wp_pmxi_files: Data: 0.02MB + Index: 0.00MB
    wp_pmxi_history: Data: 0.02MB + Index: 0.00MB
    wp_pmxi_images: Data: 0.02MB + Index: 0.00MB
    wp_pmxi_imports: Data: 0.11MB + Index: 0.00MB
    wp_pmxi_posts: Data: 0.02MB + Index: 0.03MB
    wp_pmxi_templates: Data: 0.02MB + Index: 0.00MB
    wp_postmeta: Data: 19.33MB + Index: 4.17MB
    wp_posts: Data: 10.20MB + Index: 0.66MB
    wp_redirection_404: Data: 0.02MB + Index: 0.06MB
    wp_redirection_groups: Data: 0.02MB + Index: 0.03MB
    wp_redirection_items: Data: 0.02MB + Index: 0.09MB
    wp_redirection_logs: Data: 0.02MB + Index: 0.08MB
    wp_revslider_css: Data: 0.13MB + Index: 0.00MB
    wp_revslider_layer_animations: Data: 0.02MB + Index: 0.00MB
    wp_revslider_navigations: Data: 0.02MB + Index: 0.00MB
    wp_revslider_sliders: Data: 0.13MB + Index: 0.00MB
    wp_revslider_slides: Data: 2.08MB + Index: 0.00MB
    wp_revslider_static_slides: Data: 0.02MB + Index: 0.00MB
    wp_smush_dir_images: Data: 0.02MB + Index: 0.03MB
    wp_termmeta: Data: 0.02MB + Index: 0.03MB
    wp_terms: Data: 0.02MB + Index: 0.03MB
    wp_term_relationships: Data: 0.02MB + Index: 0.02MB
    wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB
    wp_usermeta: Data: 0.23MB + Index: 0.16MB
    wp_users: Data: 0.02MB + Index: 0.05MB
    wp_wc_download_log: Data: 0.02MB + Index: 0.03MB
    wp_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB
    wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB
    wp_woocommerce_makecommerce_sc: Data: 0.02MB + Index: 0.02MB
    wp_woof_query_cache: Data: 0.02MB + Index: 0.02MB
    wp_yith_wcwl: Data: 0.02MB + Index: 0.02MB
    wp_yith_wcwl_lists: Data: 0.02MB + Index: 0.05MB
    wp_yoast_seo_links: Data: 0.06MB + Index: 0.02MB
    wp_yoast_seo_meta: Data: 0.05MB + Index: 0.00MB
    
    ### Post Type Counts ###
    
    attachment: 3168
    braintree_log: 2
    nav_menu_item: 42
    oembed_cache: 1
    option-tree: 1
    page: 84
    pi_critical_css: 1
    post: 5
    product: 11
    revision: 305
    scheduled-action: 1
    shop_coupon: 1
    shop_order: 117
    shop_order_refund: 1
    wcct_countdown: 2
    wooinstashop: 2
    wpcf7_contact_form: 4
    wphb_minify_group: 6
    wpmm_theme: 2
    
    ### Security ###
    
    Secure connection (HTTPS): ✔
    Hide errors from visitors: ✔
    
    ### Active Plugins (24) ###
    
    Query Monitor: by John Blackbourn – 3.3.5
    Advanced Woo Search: by ILLID – 1.71
    Akismet Anti-Spam: by Automattic – 4.1.2
    Classic Editor: by WordPress Contributors – 1.5
    Contact Form 7: by Takayuki Miyoshi – 5.1.2
    Emporos Addons: by Emporos – 1.6
    Emporos Social Share: by Multidots – 1.1
    Emporos Taxonomy: by Multidots – 1.0
    WPBakery Page Builder: by Michael M - WPBakery.com – 5.7
    Loco Translate: by Tim Whitlock – 2.2.2
    Redirection: by John Godley – 4.2.3
    Slider Revolution: by ThemePunch – 5.4.8.3
    Braintree For WooCommerce: by Payment Plugins
    support@paymentplugins.com – 2.6.61
    
    WooCommerce PayPal Checkout Gateway: by WooCommerce – 1.6.14
    WooCommerce PayPal Powered by Braintree Gateway: by WooCommerce – 2.2.4
    WooCommerce Swedbank gateway: by Darius Augaitis – 1.0 – Not tested with the active version of WooCommerce
    WOOF - WooCommerce Products Filter: by realmag777 – 2.2.2.1
    WooCommerce: by Automattic – 3.5.8 – 3.6.3 is available
    Yoast SEO: by Team Yoast – 11.2.1
    WP Mega Menu: by Themeum – 1.2.7
    YITH WooCommerce Compare: by YITH – 2.3.10
    YITH WooCommerce Featured Video: by YITH – 1.2.2
    YITH WooCommerce Social Login: by YITH – 1.3.2
    YITH WooCommerce Wishlist: by YITH – 2.2.10
    
    ### Settings ###
    
    API Enabled: ✔
    Force SSL: –
    Currency: EUR (€)
    Currency Position: right_space
    Thousand Separator: ,
    Decimal Separator: .
    Number of Decimals: 2
    Taxonomies: Product Types: external (external)
    grouped (grouped)
    simple (simple)
    variable (variable)
    
    Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog)
    exclude-from-search (exclude-from-search)
    featured (featured)
    outofstock (outofstock)
    rated-1 (rated-1)
    rated-2 (rated-2)
    rated-3 (rated-3)
    rated-4 (rated-4)
    rated-5 (rated-5)
    
    ### WC Pages ###
    
    Shop base: #955002905 - /shop/
    Cart: #955002906 - /cart/
    Checkout: #955002907 - /checkout/
    My account: #955002908 - /my-account/
    Terms and conditions: #955002173 - /terms-conditions/
    
    ### Theme ###
    
    Name: Emporos
    Version: 1.5.3
    Author URL: https://themeforest.net/user/creatastudio/portfolio
    Child Theme: ❌ – If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme. See: How to create a child theme
    WooCommerce Support: ✔
    
    ### Templates ###
    
    Overrides: woocommerce/archive-product.php version 3.3.0 is out of date. The core version is 3.4.0
    woocommerce/cart/cart-shipping.php version 3.2.0 is out of date. The core version is 3.5.0
    woocommerce/cart/cart-totals.php
    woocommerce/cart/cart.php version 3.3.0 is out of date. The core version is 3.5.0
    woocommerce/cart/mini-cart.php version 3.3.0 is out of date. The core version is 3.5.0
    woocommerce/cart/proceed-to-checkout-button.php
    woocommerce/checkout/thankyou.php
    woocommerce/content-product.php version 3.0.0 is out of date. The core version is 3.4.0
    woocommerce/content-single-product.php version 3.0.0 is out of date. The core version is 3.4.0
    woocommerce/content-widget-product.php version 3.3.0 is out of date. The core version is 3.5.5
    woocommerce/emails/email-header.php
    woocommerce/emails/email-styles.php
    woocommerce/global/quantity-input.php version 3.3.0 is out of date. The core version is 3.4.0
    woocommerce/global/sidebar.php
    woocommerce/loop/add-to-cart.php
    woocommerce/loop/loop-end.php
    woocommerce/loop/loop-start.php
    woocommerce/loop/orderby.php
    woocommerce/loop/pagination.php
    woocommerce/loop/price.php
    woocommerce/loop/result-count.php
    woocommerce/loop/sale-flash.php
    woocommerce/myaccount/dashboard.php
    woocommerce/myaccount/downloads.php
    woocommerce/myaccount/form-edit-account.php version 3.3.0 is out of date. The core version is 3.5.0
    woocommerce/myaccount/form-edit-address.php version 3.3.0 is out of date. The core version is 3.4.0
    woocommerce/myaccount/form-login.php version 3.3.0 is out of date. The core version is 3.5.0
    woocommerce/myaccount/form-lost-password.php version 3.3.0 is out of date. The core version is 3.5.2
    woocommerce/myaccount/form-reset-password.php version 3.3.0 is out of date. The core version is 3.5.5
    woocommerce/myaccount/my-address.php
    woocommerce/myaccount/my-downloads.php
    woocommerce/myaccount/orders.php
    woocommerce/order/order-details-item.php
    woocommerce/order/order-details.php version 3.3.0 is out of date. The core version is 3.5.2
    woocommerce/single-product/add-to-cart/external.php version 2.1.0 is out of date. The core version is 3.4.0
    woocommerce/single-product/add-to-cart/grouped.php version 3.3.0 is out of date. The core version is 3.4.0
    woocommerce/single-product/add-to-cart/simple.php version 3.0.0 is out of date. The core version is 3.4.0
    woocommerce/single-product/add-to-cart/variable.php version 3.0.0 is out of date. The core version is 3.5.5
    woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php version 3.0.0 is out of date. The core version is 3.4.0
    woocommerce/single-product/meta.php
    woocommerce/single-product/product-attributes.php
    woocommerce/single-product/product-image.php version 3.3.2 is out of date. The core version is 3.5.1
    woocommerce/single-product/product-thumbnails.php version 3.3.2 is out of date. The core version is 3.5.1
    woocommerce/single-product/rating.php
    woocommerce/single-product/related.php
    woocommerce/single-product/review-rating.php
    woocommerce/single-product/stock.php
    woocommerce/single-product/tabs/additional-information.php
    woocommerce/single-product/tabs/description.php
    woocommerce/single-product/tabs/tabs.php
    woocommerce/single-product/up-sells.php
    woocommerce/single-product-reviews.php version 3.2.0 is out of date. The core version is 3.5.0
    woocommerce/single-product.php
    
    Outdated Templates: ❌
    					
    					
    						Learn how to update
    

    ##########################

    Thanks

    Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    Here is a plugin that addresses the same problem. A REST API endpoint for managing the WooCommerce cart: https://wordpress.org/plugins/cart-rest-api-for-woocommerce/

    Kind regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add to cart API Issue’ is closed to new replies.