• Resolved admin5916

    (@admin5916)


    Hello everyone,

    Here is the plugin I’m using : https://docs.woocommerce.com/document/product-add-ons/

    With this plugin I added a custom select box if user wants to add a wrap to the product. If the select box is checked, then the price of the wrap is added to the total cart.

    But I also need to apply a custom discount only for “local pickup” shipping method. Here is how I implemented it :

    
    // Add discount for local pickup Hook
    add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
    // Add discount for local pickup Function
    function custom_discount_for_pickup_shipping_method( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $percentage = 7; // <=== 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( __('Offre Drive') . ' (-' . $percentage . '%)', -$discount );
        }
    }
    

    I’d like to get the select box addon price value and, if selected, exclude it from the local pickup discount.

    I know the plugin uses the class “WC_Product_Addons” and maybe generate an array but what I’ve tried never worked.

    Does anyone have an idea how to achieve this ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support mouli a11n

    (@mouli)

    Hi there!
    I am not a developer so am not the best person to ask about this question but as you are comfortable with coding yourself and have questions, I would recommend that you join the WooCommerce Slack Community (https://woocommerceslack.herokuapp.com), or the Advanced WooCommerce Facebook group (https://www.facebook.com/groups/advanced.woocommerce).?

    If you want a ready baked solution with a lot of cool features I suggest that you look at:
    https://woocommerce.com/products/local-pickup-plus/

    I hope that helps you to figure it out.
    Feel free to get back to us if you have further questions.

    Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    Kind regards,

    Thread Starter admin5916

    (@admin5916)

    Here is the solution :
    // Add discount for local pickup
    add_action( ‘woocommerce_cart_calculate_fees’, ‘custom_discount_for_pickup_shipping_method’, 10, 1 );
    // Add discount for local pickup
    function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( ‘DOING_AJAX’ ) )
    return;

    $addons_price_before_calc = 0;

    if( $cart ){
    foreach( $cart->get_cart() as $cart_values ){
    $product = wc_get_product( $cart_values[‘product_id’] );
    if( $product ){
    $row_price = wc_get_price_excluding_tax( $product, array( ‘qty’ => $cart_values[‘quantity’]) );
    $addons_price_before_calc = $addons_price_before_calc + $row_price;
    }
    }
    }

    $percentage = 7; // <=== 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
    if( $addons_price_before_calc )
    $discount = $addons_price_before_calc * $percentage / 100;
    else
    $discount = $cart->get_subtotal() * $percentage / 100;
    // Add the discount
    $cart->add_fee( __(‘Offre Drive’) . ‘ (-‘ . $percentage . ‘% sauf consignes)’, -$discount );
    }
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Product add-ons plugin – Exclude add-on price from local pickup discount’ is closed to new replies.