• Hello,

    I’m using a custom php function to add a 10% discount on cart when people select the local pickup shipping method.

    Any help on how to display it on the invoice ?

    Thanks !

    Alexis

    here is the function :

    add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
    function custom_discount_for_pickup_shipping_method( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $percentage = 10; // <=== Discount percentage
    
        $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
        $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
    
        // Only for Local pickup chosen shipping method
        if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {
            // Calculate the discount
            $discount = $cart->get_subtotal() * $percentage / 100;
            // Add the discount
            $cart->add_fee( __('Réduction Clique & Collecte') . ' (' . $percentage . '%)', -$discount );
        }
    }
Viewing 1 replies (of 1 total)
  • Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @alexiscollaudin,

    When you add this code to your site, does the discount appear in the order- details screen (order back-end)? Could you maybe show us a screenshot of one such order? You can use a service like https://imgbb.com/ for the images.

    I ask because our plugin grabs the information it displays from WooCommerce, so if it doesn’t appear in the order details screen, then the information is not in the order, thus the invoice will not have any discount to display.

Viewing 1 replies (of 1 total)
  • The topic ‘custom discount with php’ is closed to new replies.