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;

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.


    No comments yet..

Leave a reply

Feel free to ask questions or make comments.