• Resolved ianbaek

    (@ianbaek)


    Hello,

    Add to cart are very slow. Especially, if I click many products to add to cart at once using ‘Add selected items to cart’ function, it takes over 40 seconds. By disabling other plugins, I found that shipping cost calculation plugins (ex. FedEx Shipping Method plugin by Woocommerce) make a significant lag. I tried to change to other alternative shipping plugins, but all have the same issue. I think this is because shipping cost calculates every single item when you add to cart.

    Is there a way to make only a one-time shipping cost calculation when performing ‘Add selected items to cart’ function?

    Or, is there a way to fully disable automatic shipping cost calculation when add to cart? And maybe making a button like ‘check shipping cost’ in the cart page will do the same function.

Viewing 5 replies - 1 through 5 (of 5 total)
  • If I had to guess it’s because they are running the add to cart handler (ex: WC_Form_Handler::add_to_cart_handler_simple() on each product you add to the cart. This results in calling WC()->cart->add_to_cart() which will run update_totals() for each product… which gets slow as you add more and more products simultaneously.

    See:
    https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-cart.php#L1276

    and

    https://github.com/woocommerce/woocommerce/blob/9db2c452905ebf796a8e8b4d63db64b57e169dbe/plugins/woocommerce/includes/class-wc-cart.php#L106

    Thread Starter ianbaek

    (@ianbaek)

    Can you advise a snippet to disable running update_total for every single item?

    Thread Starter ianbaek

    (@ianbaek)

    I found a snippet to prevent the slow add-to-cart. This is a big bug.

    /*
    Remove recalculation of shipping cost when Add-to-Cart. This prevents slow add-to-cart.
    */
    function filter_need_shipping ($val) {
    	$prevent_after_add = WC()->cart->prevent_recalc_on_add_to_cart;
    	return $val && !$prevent_after_add;
    }
    add_filter( 'woocommerce_cart_needs_shipping', 'filter_need_shipping' );
    
    function mark_cart_not_to_recalc ($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    	WC()->cart->prevent_recalc_on_add_to_cart = true;
    }
    add_action('woocommerce_add_to_cart', 'mark_cart_not_to_recalc', 10, 6);
    Plugin Author WC Product Table

    (@wcproducttable)

    Hi,

    Thank you for your input. Glad to hear that you have resolved this issue for your specific requirement. However, making this change might affect other sites that have been using the plugin for years expecting the current functionality. It might potentially break some functionality on another site. If anyone else faces a similar requirement as yours I will be sure to point them here.

    Please feel free to write in if you have any further questions.

    Regards,
    Kartik

    Hi. Can you add some function in Woocommerce, which we can use to prevent calculation on add to cart?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add to cart is slow due to shipping cost calculation’ is closed to new replies.