Show WooCommerce Discounts on Entry

If a discount is applied at checkout, it will be displayed in the WooCommerce order summary. By default, this discount is not displayed on the associated Gravity Forms entry (via GSPC). This snippet will display cart item discounts as a line item in the entry order summary as well.

Code

Filename: gspc-display-discounts-on-entry.php

<?php
/**
 * Gravity Shop // Product Configurator // Show WooCommerce Discounts on Entry
 * https://gravitywiz.com/documentation/gravity-shop-product-configurator/
 *
 * If a discount is applied at checkout, it will be displayed in the WooCommerce order summary. By default, this
 * discount is not displayed on the associated Gravity Forms entry (via GSPC). This snippet will display cart item
 * discounts as a line item in the entry order summary as well.
 */
add_filter( 'gform_product_info', function( $product_info, $form, $entry ) {

	$order_item_id = gform_get_meta( $entry['id'], GS_Product_Configurator::ENTRY_WC_ORDER_ITEM_ID );
	$order_item    = \WC_Order_Factory::get_order_item( $order_item_id );
	if ( ! $order_item ) {
		return $product_info;
	}

	$order = $order_item->get_order();

	if ( $order_item->get_subtotal() !== $order_item->get_total() ) {
		$product_info['products'][ \GS_Product_Configurator\WC_Product_Form_Display::BASE_PRICE_PRODUCT_FIELD_ID . '.discount' ] = array(
			'name'     => __( 'Discount' ),
			'price'    => wc_price( wc_format_decimal( ( $order_item->get_subtotal() - $order_item->get_total() ) * -1, '' ), array( 'currency' => $order->get_currency() ) ),
			'quantity' => 1,
		);
	}

	return $product_info;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.