• Resolved mitchellworks

    (@mitchellworks)


    Hi guys,
    Up until recently, I have had a working cart with a small custom function that adds a default product to the cart whenever it is empty (code below). Now, if the default product is the only product in the cart, the cart will not proceed to the checkout page. It simply redirects back to the cart over and over. The link says it will go to /checkout but it then bounces back to /cart.

    If you shop more and add a different product manually, the cart will go on to the checkout page, as expected. I haven’t made any code changes or added plugins or anything to cause this to break. I have updated WP and WooCommerce along the way, though.

    See the issue at:
    http://americancremation.com/cart

    Any ideas are welcome. I have already checked the WC settings, changed to a different theme (doesn’t help), checked .htaccess, made sure I’m not forcing https, everything I can think of.

    ———————
    CODE for default product (in functions.php)

    function woo_default_product() {
        if (WC()->cart->get_cart_contents_count() == 0) {
            WC()->cart->add_to_cart( 330 );
        }
    }
    add_action('woocommerce_check_cart_items', 'woo_default_product');
    

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter mitchellworks

    (@mitchellworks)

    By the way, here is the system status report:

    
    ### WordPress Environment ###
    
    Home URL: http://americancremation.com
    Site URL: http://americancremation.com
    WC Version: 3.2.6
    Log Directory Writable: ✔
    WP Version: 4.9.2
    WP Multisite: –
    WP Memory Limit: 256 MB
    WP Debug Mode: –
    WP Cron: ✔
    Language: en_US
    
    ### Server Environment ###
    
    Server Info: Apache
    PHP Version: 5.6.33
    PHP Post Max Size: 8 MB
    PHP Time Limit: 30
    PHP Max Input Vars: 1000
    cURL Version: 7.57.0
    OpenSSL/1.0.2n
    
    SUHOSIN Installed: –
    MySQL Version: 5.6.38
    Max Upload Size: 2 MB
    Default Timezone is UTC: ✔
    fsockopen/cURL: ✔
    SoapClient: ✔
    DOMDocument: ✔
    GZip: ✔
    Multibyte String: ✔
    Remote Post: ✔
    Remote Get: ✔
    
    ### Database ###
    
    WC Database Version: 3.2.6
    WC Database Prefix: wpsa_
    MaxMind GeoIP Database: ✔
    Total Database Size: 2.18MB
    Database Data Size: 1.73MB
    Database Index Size: 0.45MB
    wpsa_woocommerce_sessions: Data: 0.01MB + Index: 0.00MB
    wpsa_woocommerce_api_keys: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_attribute_taxonomies: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_downloadable_product_permissions: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_order_items: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.02MB
    wpsa_woocommerce_tax_rates: Data: 0.00MB + Index: 0.01MB
    wpsa_woocommerce_tax_rate_locations: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_shipping_zones: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_shipping_zone_locations: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_shipping_zone_methods: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_payment_tokens: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_payment_tokenmeta: Data: 0.00MB + Index: 0.00MB
    wpsa_woocommerce_log: Data: 0.00MB + Index: 0.00MB
    wpsa_commentmeta: Data: 0.00MB + Index: 0.00MB
    wpsa_comments: Data: 0.01MB + Index: 0.01MB
    wpsa_links: Data: 0.00MB + Index: 0.00MB
    wpsa_options: Data: 0.85MB + Index: 0.05MB
    wpsa_postmeta: Data: 0.69MB + Index: 0.26MB
    wpsa_posts: Data: 0.14MB + Index: 0.04MB
    wpsa_smush_dir_images: Data: 0.00MB + Index: 0.00MB
    wpsa_termmeta: Data: 0.00MB + Index: 0.01MB
    wpsa_terms: Data: 0.00MB + Index: 0.01MB
    wpsa_term_relationships: Data: 0.00MB + Index: 0.01MB
    wpsa_term_taxonomy: Data: 0.00MB + Index: 0.00MB
    wpsa_usermeta: Data: 0.01MB + Index: 0.02MB
    wpsa_users: Data: 0.00MB + Index: 0.01MB
    wpsa_wcpdf_invoice_number: Data: 0.00MB + Index: 0.00MB
    wpsa_yoast_seo_links: Data: 0.00MB + Index: 0.00MB
    wpsa_yoast_seo_meta: Data: 0.00MB + Index: 0.00MB
    
    ### Post Type Counts ###
    
    attachment: 80
    custom_css: 1
    jetpack_migration: 2
    jp_img_sitemap: 1
    jp_sitemap: 1
    jp_sitemap_master: 1
    nav_menu_item: 6
    page: 12
    post: 1
    product: 43
    product_variation: 61
    revision: 41
    shop_order: 20
    
    ### Security ###
    
    Secure connection (HTTPS): ❌Your store is not using HTTPS. Learn more about HTTPS and SSL Certificates.
    Hide errors from visitors: ✔
    
    ### Active Plugins (4) ###
    
    Advanced Custom Fields: by Elliot Condon – 4.4.12
    Jetpack by WordPress.com: by Automattic – 5.7.1
    WooCommerce PDF Invoices & Packing Slips: by Ewout Fernhout – 2.1.3
    WooCommerce: by Automattic – 3.2.6
    
    ### 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)
    
    ### WC Pages ###
    
    Shop base: #60 - /shop/
    Cart: #62 - /cart/
    Checkout: #64 - /checkout/
    My account: #66 - /my-account/
    Terms and conditions: ❌ Page not set
    
    ### Theme ###
    
    Name: ACCA Mitchellworks
    Version: – 0 is available
    Author URL: 
    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: ❌ Not declared
    
    ### Templates ###
    
    Overrides: acca-mitchellworks/woocommerce/archive-product.php
    acca-mitchellworks/woocommerce/cart/cart-empty.php
    acca-mitchellworks/woocommerce/cart/cart-totals.php
    acca-mitchellworks/woocommerce/cart/cart.php
    acca-mitchellworks/woocommerce/cart/proceed-to-checkout-button.php
    acca-mitchellworks/woocommerce/checkout/form-billing.php
    acca-mitchellworks/woocommerce/checkout/form-shipping.php
    acca-mitchellworks/woocommerce/single-product.php
    
    

    @mitchellworks

    By chance did you try changing the theme to storefront or twentyseventeen and seeing if the issue is the same? (Toss just that function in the function file for those themes).

    Thread Starter mitchellworks

    (@mitchellworks)

    I did try that, but I did it again to be sure:

      Add functions code to end of twentyseventeen functions.php file
      Upload
      Activate theme Twenty Seventeen
      Go to /cart
      See default product
      Try to remove, see that it comes back (as designed)
      Click “Proceed to checkout” button which links to /checkout
      Bounce back to /cart
      Activate old theme again

    BTW, if I add a regular item from the /shop page, I can get to the /checkout page, and then I can get there forevermore, even if I go back to the cart and remove the extra product (so the default is the only one again). This is true until I empty the cache/cookies or use a new browser.

    @mitchellworks

    Give this a try

    
    function woo_default_product() {
    
        if( ! is_admin() ) {
    
    	    if ( WC()->cart->is_empty() ) {
    
    		    WC()->cart->add_to_cart( 330 );
    
    	    }
    
        }
    
    }
    add_action('template_redirect', 'woo_default_product', 10, 0 );
    

    I had issues with it on the woocommerce_check_cart_items hook. It always went through the if statement and would validate as 0 items in the cart then re-add the product.

    Testing using the above, I was able to go directly to the checkout page with the cart present and checkout.

    Thread Starter mitchellworks

    (@mitchellworks)

    @mtruitt

    This worked wonderfully. Thank you so much. I just wasn’t getting it on my own!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Default product won’t go to Checkout’ is closed to new replies.