• Resolved alordiel

    (@alordiel)


    Hey there,
    Thanks for the great plugin.
    I have seen quite some errors in my debug.log like this one:

    
    PHP Notice:  Undefined variable: discount in /public_html/example.com/wp-content/plugins/magic-coupon/includes/class-magic-coupon.php on line 203

    It is related to that method:

    
    public function manipulate_get_price( $base_price, $_product ) {
    	//Only on the main product
    	if ( $this->coupon_is_valid_for_product( $this->coupon, $_product->get_id() ) ) {
    		$coupon_amount = $this->coupon->get_amount();
    		switch( $this->coupon->get_discount_type() ) {
    			case 'percent':
    				$discount = floor( $base_price * ( $coupon_amount / 100 ) );
    				break;
    			case 'fixed_product':
    				$discount = $coupon_amount;
    				break;
    		}
    		$base_price  = $discount < $base_price ? $base_price - $discount : 0;
    	} else {
    		//NOT VALID
    	}
    	return $base_price;
    }
    

    There is no default case in the switch statement which is making the $discount undefined. I have added a default case that is equal to null and before doing the $base_price calculation I’m checking if the $discount is not null. Also don’t think you need that else statement. Here how the edit code looks like:

    
    public function manipulate_get_price( $base_price, $_product ) {
    	//Only on the main product
    	if ( $this->coupon_is_valid_for_product( $this->coupon, $_product->get_id() ) ) {
    		$coupon_amount = $this->coupon->get_amount();
    		switch( $this->coupon->get_discount_type() ) {
    			case 'percent':
    				$discount = floor( $base_price * ( $coupon_amount / 100 ) );
    				break;
    			case 'fixed_product':
    				$discount = $coupon_amount;
    				break;
    			default:
    				$discount = null;
    		}
    		$base_price  = ( $discount !== null && $discount < $base_price ) ? $base_price - $discount : 0;
    	} 
    	return $base_price;
    }
    

    Cheers,
    Al

    • This topic was modified 4 years, 10 months ago by alordiel.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP notice from manipulate_get_price’ is closed to new replies.