How to create a WooCommerce discount cart ?


This code add the following functunalities to the WooCommerce stores.

Total Sales Total Orders Total of All Shipping Costs Total of All Discounts Used Total of All Items Ordered Total Sales Per Item

function cw_discount() {
   global $woocommerce;
   $cw_discount = 0;
   foreach ( $woocommerce->cart->get_cart() as $cw_cart_key => $values) {
       $_product = $values['data'];
       if ( $_product->is_on_sale() ) {
            $regular_price = $_product->get_regular_price();
            $sale_price = $_product->get_sale_price();
            $discount = ($regular_price - $sale_price) * $values['quantity'];
            $cw_discount += $discount;
        }
   }
   if ( $cw_discount > 0 ) {
        echo '<tr class="cart-discount">
    <th>'. __( 'Secure', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'Secure', 'woocommerce' ) .' ">'
            . wc_price( $cw_discount + $woocommerce->cart->discount_cart ) .'</td>
    </tr>';
    }

}

add_action( 'woocommerce_cart_totals_after_order_total', 'cw_discount');
add_action( 'woocommerce_review_order_after_order_total', 'cw_discount');

Reference : https://www.cloudways.com/blog/woocommerce-discount-cart/


replace the ugly "echo"-es with beans_xxxx_markup_e

function cw_discount() {
   global $woocommerce;
   $cw_discount = 0;
   foreach ( $woocommerce->cart->get_cart() as $cw_cart_key => $values) {
       $_product = $values['data'];
       if ( $_product->is_on_sale() ) {
            $regular_price = $_product->get_regular_price();
            $sale_price = $_product->get_sale_price();
            $discount = ($regular_price - $sale_price) * $values['quantity'];
            $cw_discount += $discount;
        }
   }
   if ( $cw_discount > 0 ) {
        beans_open_markup_e('discount_table_tr_1', 'tr', array( 'class' => 'cart-discount' ) );
      beans_open_markup_e( 'discount_table_th_1', 'th');
               _e( 'Secure', 'woocommerce' );
     beans_close_markup_e( 'discount_table_th_1', 'th');
     beans_open_markup_e( 'discount_table_td_1', 'td', array( 'data-title' => _e('Secure') ) );
        wc_price( $cw_discount + $woocommerce->cart->discount_cart );
     beans_close_markup_e( 'discount_table_td_1', 'td');
   beans_close_markup_e('discount_table_tr_1', 'tr');
    }

}

add_action( 'woocommerce_cart_totals_after_order_total', 'cw_discount');
add_action( 'woocommerce_review_order_after_order_total', 'cw_discount');

CODE IS UNTESTED!

Write a reply

Login or register to write a reply, it's free!