Home / eCommerce / WWOF – Optimize Wholesale Order Form Bulk Adds by Deferring Cart Recalculation
Duplicate Snippet

Embed Snippet on Your Site

WWOF – Optimize Wholesale Order Form Bulk Adds by Deferring Cart Recalculation

Code Preview
php
<?php
/**
 * Suppress WWP's per-item cart recalculation during Wholesale Order Form batch adds.
 * Defers to a single recalculation at the end of each batch request instead.
 *
 * Add to functions.php or a mu-plugin.
 * Requires: Wholesale Prices 2.2.8+, Wholesale Order Form 3.x+
 */
add_action( 'woocommerce_loaded', function () {
    // Only suppress during WWOF add-to-cart requests
    // (header sent by the Order Form's axios layer)
    if ( empty( $_SERVER['HTTP_X_WWOF_ADD_TO_CART'] ) ) {
        return;
    }
    // Remove WWP's per-add recalculation using the public singleton accessor
    remove_action(
        'woocommerce_add_to_cart',
        [ WWP_Wholesale_Prices::getInstance(), 'recalculate_cart_totals_for_wholesale_customer_on_add_to_cart' ],
        1
    );
    // Run one recalculation after all items in this batch are added,
    // before WooCommerce saves the cart session at shutdown priority 20
    add_action( 'shutdown', function () {
        if ( WC()->cart instanceof WC_Cart && ! empty( WC()->cart->get_cart() ) ) {
            WC()->cart->calculate_totals();
        }
    }, 5 );
} );

Comments

Add a Comment