With the code below, I calculate and displays the total subsidies amount based on cart item shipping class and quantity:
/**
* Utility function: Get shipping class subsidy amount
*
* @return float
*/
function get_shipping_class_subsidy_amount( $shipping_class ) {
// return a float number for '12-50' case
if ( false !== strpos($shipping_class, '12-50') ) {
return 12.5;
}
// Extract the integer from the slug
else {
return preg_replace('/D/', '', $shipping_class);
}
}
/**
* Utility function: Get total subsidies amount
*
* @return float
*/
function get_total_subsidies( $cart_items ){
$total_subsidy = 0; // Initializing
// Loop through cart items
foreach ( $cart_items as $item ){
$subsidy = (float) get_shipping_class_subsidy_amount( $item['data']->get_shipping_class() );
$price = get_post_meta($values['product_id'] , '_price', true);
if ( $subsidy > 0 ) {
$total_subsidy += $item['quantity'] * $subsidy;
}
}
return $total_subsidy;
}
/**
* Display HTML formatted total subsidies amount in classic cart and checkout pages.
* Hooked function.
*/
function display_total_subsidies() {
if ( $total_subsidy = get_total_subsidies( WC()->cart->get_cart() ) ) {
echo '<tr class="subsidies">
<th>' . esc_html__( 'Subsidies', 'woocommerce' ) . '</th>
<td data-title="' . esc_html__( 'Subsidies', 'woocommerce' ) . '">' . wc_price($total_subsidy) . '</td>
</tr>';
}
}
add_action ( 'woocommerce_cart_totals_before_shipping', 'display_total_subsidies', 10 ); // for Cart page
add_action ( 'woocommerce_review_order_before_shipping', 'display_total_subsidies', 10 ); // for Checkout page
The problem is that the cart item subsidy amount is obtained twice when it’s a bundle product, as it takes into account the bundled product itself and it’s products components.
But the calculation in the label is correct.
New contributor
Eduardo Relax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1