// korting per product tonen in winkelwagen WC
function my_custom_show_sale_price_at_cart( $old_display, $cart_item, $cart_item_key ) {

	/** @var WC_Product $product */
	$product = $cart_item['data'];

	if ( $product ) {
		return $product->get_price_html();
	}

	return $old_display;

}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price_at_cart', 10, 3 );

// Toon korting bij afrekenen WC
function my_custom_show_sale_price_at_checkout( $subtotal, $cart_item, $cart_item_key ) {

	/** @var WC_Product $product */
	$product = $cart_item['data'];
	$quantity = $cart_item['quantity'];

	if ( ! $product ) {
		return $subtotal;
	}

	$regular_price = $sale_price = $suffix = '';

	if ( $product->is_taxable() ) {

		if ( 'excl' === WC()->cart->tax_display_cart ) {

			$regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
			$sale_price    = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );

			if ( WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
				$suffix .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
			}
		} else {

			$regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
			$sale_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );

			if ( ! WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
				$suffix .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
			}
		}
	} else {
		$regular_price    = $product->get_price() * $quantity;
		$sale_price       = $product->get_sale_price() * $quantity;
	}

	if ( $product->is_on_sale() && ! empty( $sale_price ) ) {
		$price = wc_format_sale_price(
			         wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) ),
			         wc_get_price_to_display( $product, array( 'qty' => $quantity ) )
		         ) . $product->get_price_suffix();
	} else {
		$price = wc_price( $regular_price ) . $product->get_price_suffix();
	}

	// VAT suffix
	$price = $price . $suffix;

	return $price;

}

add_filter( 'woocommerce_cart_item_subtotal', 'my_custom_show_sale_price_at_checkout', 10, 3 );

// Toon totale korting in winkelwagen
if ( ! function_exists( 'wbmz_cart_total_discounts' ) ) {
    /**
     *
     */
    function wbmz_cart_total_discounts()  {
        global $woocommerce;
        $discount_total = 0;
 
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
 
            $_product = $values['data'];
 
            if ($_product->is_on_sale()) {
                $regular_price = $_product->get_regular_price();
                $sale_price = $_product->get_sale_price();
 
                if (empty($regular_price)){ //then this is a variable product
                    $available_variations = $_product->get_available_variations();
                    $variation_id=$available_variations[0]['variation_id'];
                    $variation= new WC_Product_Variation( $variation_id );
                    $regular_price = $variation ->regular_price;
                    $sale_price = $variation ->sale_price;
                }
 
                $discount = ceil(($regular_price - $sale_price) * $values['quantity'] );
                $discount_total += $discount;
            }
 
        }
        if ($discount_total > 0) { ?>
            <tr class="discounts-total">
                <th><?php echo esc_html__( 'Totale korting', 'wbmz' ); ?></th>
                <td><?php echo wc_price($discount_total + $woocommerce->cart->discount_cart); ?></td>
            </tr>
        <?php }
    }
}
 
 
add_action( 'woocommerce_cart_totals_before_shipping',       'wbmz_cart_total_discounts', 10 );
add_action( 'woocommerce_review_order_before_order_total',  'wbmz_cart_total_discounts', 10);
Geef een reactie 0