How to Restrict WooCommerce Discounts from applying to PPOM fields?

๐Ÿ“ Note: PPOM meta fields become part of the product meta; hence, the discounts set for the product will be applied to the meta fields by default.

This article presents how to avoid applying the WooCommerce coupon to the PPOM meta fields too. This way, the coupon will be applied only to the product price.

1

Copy this code snippet.

add_filter( 'woocommerce_coupon_get_discount_amount', function ( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
	if ( $coupon->is_type( 'percent' ) ) {
		$ppom_addons_total = 0;


		if ( isset( $cart_item['ppom']['fields'] ) ) {
			$product_id       = $cart_item['product_id'];
			$variation_id     = $cart_item['variation_id'] ?? '';
			$ppom_fields_post = $cart_item['ppom']['fields'];
			$product_quantity = floatval( $cart_item['quantity'] );


			if ( $ppom_fields_post ) {
				$ppom_field_prices = ppom_get_field_prices( $ppom_fields_post, $product_id, $product_quantity, $variation_id, $cart_item );
				$ppom_addons_total = ppom_price_get_addon_total( $ppom_field_prices );
			}
		}


		$adjusted_amount = $discounting_amount - $ppom_addons_total;
		if ( $adjusted_amount < 0 ) {
			$adjusted_amount = 0;
		}


		$discount = $adjusted_amount * ( $coupon->get_amount() / 100 );
	}


	return $discount;
}, 10, 5 );
2

Paste it into the currently active theme functions.php file.

๐Ÿ“Note: If you paste the code snippet into the plugin's source file, the changes will be lost when you update the plugin. The same thing may happen when you update the theme, this is why we recommend using a child theme.

3

Save the changes and recreate the scenario to see how it works.

๐Ÿ’กResult

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us