Cartflows get cart total discount

For one of my projects I needed the total discount of the items added in Cartflows to my cart so I could show the customer what he/she would save.

I created the following function which loops over all the cart items and substracts the “custom price” from the “regular price”. The custom price is an added value by Cartflows. Normally woocommerce uses sales price for this.

 * Returns Cartflows total cart discounts
function bcc_get_cartflows_cart_total_discount() {
   return array_reduce( WC()->cart->get_cart(), function( $total_discount, $item ) {
      if ( empty( $item['data'] ) || empty( $item['custom_price'] ) ) return $total_discount;
      $product = $item['data'];
      $regular_price = $product->get_regular_price();
      $custom_price  = $item['custom_price'];
      $item_discount = ( $regular_price - $custom_price ) * $item['quantity'];
      $total_discount += $item_discount;
      return $total_discount;

Robbert Vermeulen

Need help from a WordPress expert?

I would love to hear about your project and how I can help you achieve your goals