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

Top WooCommerce & WordPress Plugins Forums Maximum Products per User for WooCommerce How can I make a specific variation count as 4 instead of 1?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #100459
    blankchris
    Participant

    I have a limit of 4 per product category set up and am wondering if there is a way to make a specific variation count as 4 instead of 1. In my case, the limit is set on a “cards” category. Normally people purchase individual cards, but there are now variations that are Packages of 4 cards which need to count as 4 instead of 1. Is there a function I can use to multiply the quantity for these specific products?

    #100460
    blankTom Anbinder
    Moderator
    Plugin Support

    Hi Chris,

    First solution that comes to mind – you can set the plugin’s “Mode” option (in “WooCommerce > Settings > Maximum Products per User > General”) to “Product weights”, and then set weights for “normal” variations to “1”, and weight for that “special” variation to “4”. In all frontend notices you can still say “quantity”, so customers won’t really understand that you are limiting it by weight. P.S. Don’t forget to “Recalculate sales data” after changing the “Mode” option.

    If that’s not good enough (e.g. if you are using real product weights for shipping), we can do it with PHP snippet, which you’d have to add to e.g. your (child) theme’s functions.php file. Please let me know if this is what you prefer, and I will send you the snippet.

    #100461
    blankchris
    Participant

    Thanks for the weight suggestion Tom. Unfortunately that’s not going to work because I have very specific weights set for each variation that don’t divide evenly. If you can help get me on the right track with a snippet that would be great!

    #100462
    blankTom Anbinder
    Moderator
    Plugin Support

    Sure. Please give me a couple of hours or so…

    #100463
    blankTom 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;
        }
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.