• Resolved scotlandtravel

    (@scotlandtravel)


    I am currently using the Chauffeur Booking System paid plugin for my taxi website. However, I noticed that the plugin does not support giving discounts on return rides. Is there any way I can use WooCommerce to apply a discount on return rides, as I am using WooCommerce for my payment method and checkout process? Please check the booking form on my website below for reference. If custom coding is required to achieve this functionality, could you please point me in the right direction or provide any guidance on how to implement this?

    Website link: https://scotlandtravelsolution.com/book-now/
    Since woocommerce also provides coupon I just need to know if it will work on this booking form then I can do some custom coding using hooks?

    Thank you for your assistance.

Viewing 4 replies - 16 through 19 (of 19 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @scotlandtravel

    To apply a discount based on the transfer type, you can use the woocommerce_before_calculate_totals hook to modify the cart total before it’s calculated.

    Although writing or providing custom code is not within the scope of our support policy, here is an example for better understanding:

    function apply_discount_based_on_transfer_type( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
    return;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    $product = $cart_item['data'];
    $transfer_type = $product->get_attribute('pa_transfer-type'); // Replace with your actual attribute name

    if( $transfer_type == 'return_new_ride' ) {
    // Apply WooCommerce coupon code
    $coupon_code = 'your_coupon_code'; // Replace with your actual coupon code
    if ( ! WC()->cart->has_discount( $coupon_code ) ) {
    WC()->cart->add_discount( $coupon_code );
    }
    }
    }
    }
    add_action( 'woocommerce_before_calculate_totals', 'apply_discount_based_on_transfer_type', 10, 1 );

    Also if possible I don’t know which line of should I copy from inspect element, if you can just help me find its class on cart page

    You can right-click on the element and select “Inspect” or “Inspect Element” from the context menu. This will open the browser’s developer tools and highlight the code for the selected element. The class of the element would be listed within the opening tag of the highlighted code.

    f you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter scotlandtravel

    (@scotlandtravel)

    Thank you for your detailed answer, I’ll try this out and get back to use.

    Thread Starter scotlandtravel

    (@scotlandtravel)

    add_filter( 'woocommerce_coupon_is_valid', 'custom_coupon_validation_based_on_transfer_type', 10, 3 ); function custom_coupon_validation_based_on_transfer_type( $valid, $coupon, $discount ) { // Loop through cart items to check transfer type foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( isset( $cart_item['booking_id'] ) ) { $booking_id = $cart_item['booking_id']; $transfer_type = get_post_meta( $booking_id, 'transfer_type', true ); // Log transfer type for debugging error_log( "Booking ID: $booking_id, Transfer Type: $transfer_type" ); // Check if transfer type is RETURN_NEW_RIDE (3) if ( $transfer_type != 3 ) { error_log( "Coupon is invalid for Transfer Type: $transfer_type" ); return false; // Disallow coupon } } } // If all items are valid for the coupon error_log( "Coupon is valid" ); return $valid; }

    I have comeup with this code yet its not working any help on it or nah?

    Hi there @scotlandtravel,

    Helping out with custom coding of this nature is outside the scope of support that our support staff can help out with here, although I would recommend the following:

    • Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    • Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions):?https://woo.com/community-slack/

    Hope it helps!

Viewing 4 replies - 16 through 19 (of 19 total)
  • You must be logged in to reply to this topic.