• Resolved williamadama

    (@williamadama)


    I have a fairly simple function that gets several products data and bulk-adds those products to the cart via a foreach loop. The problem is that, if the cart already contains any product added, the first product in my loop is skipped and not added. Any other products after the first one, get added no problem. If the cart IS empty, all products get added no problem, including the first one.

    I have recreated the problem using a clean installation.?I have only WooCommerce and Code Snippets activated. You can see the behaviour at this?/sample-page/?clicking the “Add All to Cart” button. It will work for the first time, but if you reload and click again, the problem will appear.

    add_shortcode( 'add-all-to-cart', 'add_all_to_cart_shortcode' );
    
    function add_all_to_cart_shortcode() {
        global $woocommerce;
        ob_start();
        ?>
            <form id="add-all-to-cart" method="post">
            <button type="submit" name="add-all-to-cart-button" class="add-all button alt">Add all to cart</button>
            </form>
        <?php
        return ob_get_clean();
    }
    
    add_action( 'init', 'add_all_to_cart_handler' );
    function add_all_to_cart_handler() {
        global $woocommerce;
        if ( isset( $_POST['add-all-to-cart-button'] ) ) {
            $products_data = array(
                array(
                    'product_id' => 18,
                    'variation_id' => 19,
                    'quantity' => 1
                ),
                array(
                    'product_id' => 21,
                    'variation_id' => 22,
                    'quantity' => 1
                ),
                array(
                    'product_id' => 24,
                    'variation_id' => 26,
                    'quantity' => 1
                )
            );
    
            foreach ( $products_data as $product ) {
                $product_id = $product['product_id'];
                $variation_id = $product['variation_id'];
                $quantity = $product['quantity'];
                WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
            }
            wp_safe_redirect( wc_get_cart_url() );
            exit();
        }
    }

    What I’ve tried:

    I’ve dumped all variables and all data is good, all integers. I tried checking the cart_item_keyand it is being correctly assigned. I tried changing the code to first look for any given product already in cart and just update their quantity, but that did not solved the problem. Also tried throwing some WC()->cart->maybe_set_cart_cookies(); statements in there, but to no luck.

    Searching for answers I found some people talking about cart sessions and the persistent cart. But I am unsure if this is related.

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    This is odd, I was checking, and I was able to see the issue, all products are added when the cart is empty, but if not empty, then the first product is not added.

    Please note custom code is not supported in our forums, I highly recommend you to use debugging tools or advanced debugging to help you to get more information about the issue.

    If that does not give you a clue about the issue, can you please share a copy of your site’s System Status? I am asking because the form shows Gravity Forms as a title. You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”.?

    Cheers.

    • This reply was modified 1 year, 7 months ago by Igor H.
    Thread Starter williamadama

    (@williamadama)

    Hello, Igor. Thanks for your response.
    I apologize for incorrect behaviour regarding the custom code. I did not know. Thank you for the debugging info, I will get into that.

    The reason it says Gravity Forms in the title is because the sample page previously had a GF form for testing purposes. It does not anymore, but the title remained.

    As I said before, only WooCommerce and Code Snippets are activated. Here is the system report:

    `
    ### WordPress Environment ###
    
    WordPress address (URL): https://magikprintsforyou.com
    Site address (URL): https://magikprintsforyou.com
    WC Version: 7.6.1
    REST API Version: ? 7.6.1
    WC Blocks Version: ? 9.8.5
    Action Scheduler Version: ? 3.5.4
    Log Directory Writable: ?
    WP Version: 6.2
    WP Multisite: –
    WP Memory Limit: 768 MB
    WP Debug Mode: –
    WP Cron: ?
    Language: en_US
    External object cache: –
    
    ### Server Environment ###
    
    Server Info: Apache
    PHP Version: 7.4.33
    PHP Post Max Size: 256 MB
    PHP Time Limit: 120
    PHP Max Input Vars: 3000
    cURL Version: 7.85.0
    OpenSSL/3.0.8
    
    SUHOSIN Installed: –
    MySQL Version: 5.7.39-42-log
    Max Upload Size: 256 MB
    Default Timezone is UTC: ?
    fsockopen/cURL: ?
    SoapClient: ?
    DOMDocument: ?
    GZip: ?
    Multibyte String: ?
    Remote Post: ?
    Remote Get: ?
    
    ### Database ###
    
    WC Database Version: 7.6.1
    WC Database Prefix: rmf_
    Total Database Size: 5.91MB
    Database Data Size: 4.22MB
    Database Index Size: 1.69MB
    rmf_woocommerce_sessions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    rmf_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    rmf_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_actionscheduler_actions: Data: 0.02MB + Index: 0.11MB + Engine InnoDB
    rmf_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_actionscheduler_logs: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_comments: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
    rmf_gf_draft_submissions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_gf_entry: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_gf_entry_meta: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
    rmf_gf_entry_notes: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_gf_form: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_gf_form_meta: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_gf_form_revisions: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_gf_form_view: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_options: Data: 3.06MB + Index: 0.06MB + Engine InnoDB
    rmf_postmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_posts: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    rmf_sgs_log_events: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_sgs_log_visitors: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_snippets: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_termmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_terms: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_term_relationships: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_term_taxonomy: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_usermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
    rmf_wc_admin_notes: Data: 0.06MB + Index: 0.00MB + Engine InnoDB
    rmf_wc_admin_note_actions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_wc_customer_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_wc_order_product_lookup: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
    rmf_wc_order_stats: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
    rmf_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
    rmf_wc_product_attributes_lookup: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_wc_product_download_directories: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
    rmf_wc_rate_limits: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
    rmf_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    rmf_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
    
    ### Post Type Counts ###
    
    attachment: 1
    page: 7
    post: 1
    product: 3
    product_variation: 6
    revision: 5
    wp_global_styles: 1
    
    ### Security ###
    
    Secure connection (HTTPS): ?
    					Your store is not using HTTPS. Learn more about HTTPS and SSL Certificates.
    Hide errors from visitors: ?
    
    ### Active Plugins (2) ###
    
    Code Snippets: by Code Snippets Pro – 3.3.0
    WooCommerce: by Automattic – 7.6.1
    
    ### Inactive Plugins (4) ###
    
    Gravity Forms: by Gravity Forms – 2.7.3
    SiteGround Central: by SiteGround – 2.0.9
    SiteGround Optimizer: by SiteGround – 7.3.1
    SiteGround Security: by SiteGround – 1.4.3
    
    ### Settings ###
    
    API Enabled: ?
    Force SSL: –
    Currency: USD ($)
    Currency Position: left
    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)
    
    Connected to WooCommerce.com: –
    Enforce Approved Product Download Directories: ?
    Order datastore: WC_Order_Data_Store_CPT
    
    ### WC Pages ###
    
    Shop base: #5 - /shop/
    Cart: #6 - /cart/
    Checkout: #7 - /checkout/
    My account: #8 - /my-account/
    Terms and conditions: ? Page not set
    
    ### Theme ###
    
    Name: Twenty Twenty-One
    Version: 1.8
    Author URL: https://www.ads-software.com/
    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: –
    
    ### Admin ###
    
    Enabled Features: activity-panels
    analytics
    coupons
    customer-effort-score-tracks
    import-products-task
    experimental-fashion-sample-products
    shipping-smart-defaults
    shipping-setting-tour
    homescreen
    marketing
    multichannel-marketing
    mobile-app-banner
    navigation
    onboarding
    onboarding-tasks
    remote-inbox-notifications
    remote-free-extensions
    payment-gateway-suggestions
    shipping-label-banner
    subscriptions
    store-alerts
    transient-notices
    woo-mobile-welcome
    wc-pay-promotion
    wc-pay-welcome-page
    
    Disabled Features: block-editor-feature-enabled
    minified-js
    new-product-management-experience
    product-variation-management
    settings
    
    Daily Cron: ? Next scheduled: 2023-04-30 14:03:23 +00:00
    Options: ?
    Notes: 56
    Onboarding: -
    
    ### Action Scheduler ###
    
    Complete: 23
    Oldest: 2023-04-27 22:29:30 +0000
    Newest: 2023-04-29 01:10:05 +0000
    
    Failed: 1
    Oldest: 2023-04-27 22:35:31 +0000
    Newest: 2023-04-27 22:35:31 +0000
    
    Pending: 1
    Oldest: 2023-04-30 01:10:05 +0000
    Newest: 2023-04-30 01:10:05 +0000
    
    
    ### Status report information ###
    
    Generated at: 2023-04-29 22:14:59 +00:00
    `

    Hi @williamadama

    Thanks for sharing the requested information above.

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

    Thread Starter williamadama

    (@williamadama)

    Hello. I don’t think I am asking help with custom code. I pasted the code as example of WooCommerce API usage.

    Since I have been able to reproduce the problem on a clean installation, might there be a chance that this is a bug on your side?

    If so, who can look at this and decide if it is worth reporting? How can I report it myself?

    Thank you.

    Hello,

    I understand that you might be feeling frustrated, and I’m here to help in any way I can. However, it’s important to note that our WooCommerce support, as outlined in our support policy (https://woocommerce.com/support-policy/), does not cover code customizations.

    Our primary focus is to assist with issues and questions relating to WooCommerce core functionality, as well as our own themes and extensions. While we understand that customizations are an essential part of creating unique websites, providing assistance for custom code falls outside the scope of the support we can offer.

    For custom code inquiries or adjustments, we recommend reaching out to a developer. On the other hand if you think this is an issue, you can create a report on GitHub, here is the link https://github.com/woocommerce/woocommerce/issues

    Sorry for the inconvenience, if you have any other questions or concerns relating to WooCommerce that are within our support range, please feel free to ask, and we’ll be more than happy to help.

    Thread Starter williamadama

    (@williamadama)

    Thanks for the GitHub link.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add_to_cart() FAILS if cart is NOT empty’ is closed to new replies.