yanknudtskov
9/22/2017 - 8:01 AM

woocommerce-dynamic-discount.php

woocommerce-dynamic-discount.php

<?php
/*
 * Discount all Products by percentage
 */
function yanco_filter_woocommerce_get_price_html( $price, $instance ) {
    $discount_decimal_percentage = 0.7; // INSERT PERCENTAGE in decimal
    
	if ( is_admin() ) {
		return $price;
	}
	else {
		// Check if the bogklub is in the cart
		global $woocommerce;
		$raw_price = preg_replace("/[^0-9\.,]/", "", $price);
		$discounted_price = $raw_price * 0.7;
		return '<strike>'.$price.'</strike>'.'<span class="amount">'.$discounted_price.'&nbsp;DKK</span>';
	}
	return $price;
};
add_filter( 'woocommerce_variation_price_html', 'yanco_filter_woocommerce_get_price_html', 200, 2 );
add_filter( 'woocommerce_get_price_html', 'yanco_filter_woocommerce_get_price_html', 200, 2 );
/*
 * Discount all Products in cart
 */
//add_action( 'woocommerce_cart_calculate_fees','yanco_custom_discount' );
function yanco_custom_discount( ) {
    $discount_decimal_percentage = 0.3;
    
    if ( is_admin() ) {
		return;
	} else {
        global $woocommerce;
        $total_discount = 0;
        foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
			$_product = $values['data'];
            $total_discount += $_product->price * $discount_decimal_percentage;
		}
        // Alter the cart discount total
        WC()->cart->add_fee( 'Bogklub Rabat', -1 * $total_discount );
    }
}