Reply To: How can I make a specific variation count as 4 instead of 1?

#100463
Tom Anbinder
Moderator
Plugin Support

Hi again, Chris,

As promised, here is the snippet below. You need to change only one place there – where it says “TODO: you need to add your products with special quantities here”. Please give it a try and let me know if it’s working as expected:

add_filter( 'alg_wc_mppu_get_cart_item_quantities',         'my_alg_wc_mppu_get_cart_item_quantities',         PHP_INT_MAX );
add_filter( 'alg_wc_mppu_validate_on_add_to_cart_quantity', 'my_alg_wc_mppu_validate_on_add_to_cart_quantity', PHP_INT_MAX, 2 );
add_filter( 'alg_wc_mppu_save_quantities_item_qty',         'my_alg_wc_mppu_save_quantities_item_qty',         PHP_INT_MAX, 2 );

if ( ! function_exists( 'my_alg_wc_mppu_maybe_change_qty' ) ) {
    function my_alg_wc_mppu_maybe_change_qty( $product_id, $qty ) {

        // TODO: you need to add your products with special quantities here; in "product (or variation) ID => quantity multiplier" format.
        $products = array(
            123  => 4,
            125  => 4,
        );

        return ( isset( $products[ $product_id ] ) ? $products[ $product_id ] * $qty : $qty );
    }
}

if ( ! function_exists( 'my_alg_wc_mppu_save_quantities_item_qty' ) ) {
    function my_alg_wc_mppu_save_quantities_item_qty( $item_qty, $item ) {
        $product_id = ( ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'] );
        return my_alg_wc_mppu_maybe_change_qty( $product_id, $item_qty );
    }
}

if ( ! function_exists( 'my_alg_wc_mppu_validate_on_add_to_cart_quantity' ) ) {
    function my_alg_wc_mppu_validate_on_add_to_cart_quantity( $quantity, $product_id ) {
        return my_alg_wc_mppu_maybe_change_qty( $product_id, $quantity );
    }
}

if ( ! function_exists( 'my_alg_wc_mppu_get_cart_item_quantities' ) ) {
    function my_alg_wc_mppu_get_cart_item_quantities( $cart_item_quantities ) {
        foreach ( $cart_item_quantities as $product_id => &$quantity ) {
            $quantity = my_alg_wc_mppu_maybe_change_qty( $product_id, $quantity );
        }
        return $cart_item_quantities;
    }
}