phpwordpresswoocommercehook-woocommercecart

Display total subsidy amount based on WooCommerce cart items shipping class and quantity


With the code below added to my theme's fuctions.php file, I am trying to display a custom total subsidy amount based on WooCommerce cart items shipping class and quantity:

/**
 * Gets the total subsidies amount
 * 
 * @return float
 */
 function get_subsidies_total(){
    $quantity = $cart_item['quantity'];
    $shipping_class_id = $product->get_shipping_class_id(); 
    $shipping_class_term = get_term($shipping_class_id, 'product_shipping_class');
    $subsidio_total = '';

    // WC Cart
    if ( WC()->cart ) {
        // Get cart
        $cart = WC()->cart;

        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ){    
            if ($shipping_class_term->slug == 'envio-gratis-o-preferente-125') {
                $subsidio_subtotal = 125;
            } 
            else if ($shipping_class_term->slug == 'envio-gratis-o-preferente-73') {
                $subsidio_subtotal = 73;
            }
            else if ($shipping_class_term->slug == 'envio-preferente-50') { 
                $subsidio_subtotal = 50;
            }
            else if ($shipping_class_term->slug == 'envio-preferente-30') {
                $subsidio_subtotal = 30;
            }
            else if ($shipping_class_term->slug == 'envio-preferente-25') { 
                $subsidio_subtotal = 25;
            }
            else if ($shipping_class_term->slug == 'envio-preferente-12-50') { 
                $subsidio_subtotal = 12.5;
            }
            else if ($shipping_class_term->slug == 'envio-preferente-5') { 
                $subsidio_subtotal = 5;
            }
            $subsidio_row = $quantity * $subsidio_subtotal;
            $subsidio_total += $subsidio_row;
            return $subsidio_total;
        }
    }
}

add_action ('woocommerce_cart_totals_before_shipping', 'mostrar_subcidio', 1);
function mostrar_subcidio(){
    $subsidio= $subsidio->get_subcidio_total();
    echo $subsidio;
}

But it doesn't work.

How can I display the total subsidy amount based on cart items shipping class and quantity in WooCommerce cart and checkout?


Solution

  • There are multiple mistakes and complications in your code, try instead the following compacted and optimized code (commented):

    /**
     * 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() );
    
            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
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work with classic cart and checkout pages, but not with newly cart and checkout Blocks (not customizable).

    enter image description here