I have added a progressive pricing based on the quantity of each cart item. What I am struggling with, is to reflect the $default_price
from the product (instead the now manually added price) and also to set the layout of the new price like this (default_price has strikethrough):
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart->get_cart() as $item ) {
$quantity = $item['quantity'];
$discount_price = 0.008333333333;
$default_price = 4.25;
$minimum = 10;
$maximum = 100;
if( $quantity > $minimum ) {
if( $quantity > $maximum ) {
$new_price = $default_price - ($discount_price * ($maximum - $minimum));
}
else {
$new_price = $default_price - ($discount_price * ($quantity - $minimum));
}
$item['data']->set_price( '<del>' . $default_price . '</del> <strong>' . $new_price . '</strong>');
}
}
}
The coded $item['data']->set_price( '<del>' . $default_price . '</del> <strong>' . $new_price . '</strong>');
is wrong, but how do I reflect this so it can accept the html elements?
For what you wish to obtain you can use the following:
While the woocommerce_before_calculate_totals
action hook is used to calculate totals
The woocommerce_cart_item_price
filter hook ensures the strikethrough of the original price.
explanation via comment tags added in the code
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Settings
$discount_price = 0.008333333333;
$minimum = 10;
$maximum = 100;
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Product quantity in cart
$quantity = $cart_item['quantity'];
// Quantity greater than minimum
if ( $quantity > $minimum ) {
// Default price
$default_price = $cart_item['data']->get_price();
// Quantity greater than maximum
if ( $quantity > $maximum ) {
$new_price = $default_price - ( $discount_price * ( $maximum - $minimum ) );
} else {
$new_price = $default_price - ( $discount_price * ( $quantity - $minimum ) );
}
// Set price
$cart_item['data']->set_price( $new_price );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get reqular price
$regular_price = $product->get_regular_price();
// New price
$new_price = $cart_item['data']->get_price();
// NOT empty and NOT equal
if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
// Output
$price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
}
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );