• When this script is added the site crashes, with or without backorder snippet.

    <?php
    
    /**
    * Round off decimals for coupons
    **/
    
    function filter_woocommerce_coupon_get_discount_amount( $discount, 
    $discounting_amount, $cart_item, $single, $instance ) { 
        $discount = round( $discount, 1 );
        return $discount; 
    } 
    
    add_filter( 'woocommerce_coupon_get_discount_amount', 
    'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
    }
    
    /**-------------------------**/
    
    /**Do not allow coupons to be applied to products in backorder in WooCommerce on your child site.
    *Snippet Type
    *Execute on Child Sites
    *Snippet
    **/
    
    function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {    
        // On backorder
        if ( $cart_item['data']->is_on_backorder() ) {
            $discount = 0;
        }
        return $discount;
    }
    add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Jay

    (@jaygumanid)

    There are a few issues with the provided code:

    There is a closing curly brace (}) at the end of the code block that should not be there. This might cause a parse error and prevent the code from running.

    The function filter_woocommerce_coupon_get_discount_amount is defined twice in the code block. This will cause a fatal error, as it is not allowed to define a function with the same name multiple times.

    The second definition of the filter_woocommerce_coupon_get_discount_amount function has a different number of parameters than the first definition. This might cause an error when the function is called.

    To fix the issues, you can try removing the extra curly brace at the end of the code block and renaming the second function definition to a unique name. Make sure that the function definition matches the number of parameters passed to it when it is called.

    <?php
    
    /**
    * Round off decimals for coupons
    **/
    
    function round_off_coupon_discount( $discount, $discounting_amount, $cart_item, $single, $instance ) { 
        $discount = round( $discount, 1 );
        return $discount; 
    } 
    
    add_filter( 'woocommerce_coupon_get_discount_amount', 'round_off_coupon_discount', 10, 5 );
    
    /**-------------------------**/
    
    /**Do not allow coupons to be applied to products in backorder in WooCommerce on your child site.
    *Snippet Type
    *Execute on Child Sites
    *Snippet
    **/
    
    function prevent_coupon_apply_to_backordered_products( $discount, $price_to_discount , $cart_item, $single, $coupon ) {    
        // On backorder
        if ( $cart_item['data']->is_on_backorder() ) {
            $discount = 0;
        }
        return $discount;
    }
    add_filter( 'woocommerce_coupon_get_discount_amount', 'prevent_coupon_apply_to_backordered_products', 10, 5 );
    

    I removed the extra curly brace at the end of the code block and renamed the second function definition to prevent_coupon_apply_to_backordered_products. I also made sure that the function definition matches the number of parameters passed to it when it is called.

    I hope this helps! Let me know if you have any other questions.

    Thread Starter lorenzo08

    (@lorenzo08)

    Hi Jay
    thanks a lot for finding the bugs, works perfectly now.
    Your help is very much appreciated.

    Cheers
    Lorenzo

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Coupon rounding script in child theme causes WP to crash’ is closed to new replies.