Add Tip input by customer to WooCommerce checkout page - php

I am developing a food ordering WooCommerce store. I am trying to add an input field or a grouped button to let the customer add delivery tips to checkout total.
Backend Part:
I wrote an action that add a tip to checkout based on the percentage given (let's say 10% of total order)
add_action( 'woocommerce_cart_calculate_fees', 'calculateTipsPercentage', 10, 1 );
function calculateTipsPercentage( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$total_tax = 0;
$carttotal= 0;
// Get the unformated taxes array
$taxes = $cart->get_taxes();
//get Tax amount from taxes array
foreach($taxes as $tax) $total_tax += $tax;
global $woocommerce;
$percentage = 0.10;//percentage of tips (must be entered by customer)
$carttotalamount = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ); //Cart total without tax
$totalorderwithTax = ( $carttotalamount + $total_tax); //cart total with tax
$extrafee= round( $totalorderwithTax * $percentage, 2 ); //extra fee amount
$percetagetoShow = ( $percentage * 100 );
$woocommerce->cart->add_fee( "Tip ({$percetagetoShow}%)", $extrafee, true, '' );
}
My problem is with the Front-end part.
how can I add any kind of input field with a button (add tips to checkout) that let the customer add the percentage and click this button which will fire the action found above. or if I can do it through ajax/jquery without the button (without refreshing the page) which would be better.
any help would be appreciated.

Woocommerce has various hooks that you can use to add your custom fields to the checkout page such as *
woocommerce_review_order_before_shipping
woocommerce_review_order_after_shipping
woocommerce_review_order_before_order_total
or you can visit here

Related

Add a fee in woocommerce to the transport tax calculated by the DPD plugin calculator

I am trying to add a fee on the checkout page of my woocommerce /wordpress site. The problem is that I need that fee to be added to the total transport calculation fee when cod payment method is chosen. I don't know so much PHP code, I do have basic understanding of the code, but I cannot write. However I did scrape the internet and managed to get to the point where I define and add the tax to the checkout page as a standalone tax, using the code below in my functions.php file but now I need that fee of 5 to be added to the transport rate when cod is chosen.
// Add a custom fee (fixed or based cart subtotal percentage) by payment
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_payment_id = WC()->session->get('chosen_payment_method');
if ( empty( $chosen_payment_id ) )
return;
$subtotal = $cart->subtotal;
// SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
$targeted_payment_ids = array(
'cod' => 5, // Fixed fee
);
// Loop through defined payment Ids array
foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
if ( $chosen_payment_id === $payment_id ) {
$cart->add_fee( __('Plata ramburs', 'woocommerce'), $fee_cost, true );
}
}
}
// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
wc_enqueue_js( "jQuery( function($){
$('form.checkout').on('change', 'input[name=payment_method]', function(){
$(document.body).trigger('update_checkout');
});
});");
}
The code above just makes the fee appear on the checkout page as a standalone fee, and it is calculated to the overall total, appears on the order backend, but I need it now to be part of the transport rate fee because I have an integration with my invoice software, and the 5 fee is not taken from the backend only the transport, coupon codes and product prices.

Woocommerce - Reload checkout after tax calculation

I want to surcharge special tax based on specific attribute value (billing_company) at checkout page.
Problem is that order details are not automatically recalculated and refreshed. I can refresh the whole page with F5 and then change is visible (8% vat is added to order).
I would like to write a code which will automatically do this for me after user enters specific attribute value on checkout.
Code review:
// Add 8% tax for Companies
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_companies', 10, 1 );
function custom_tax_surcharge_for_companies( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// Only for Companies (if not we exit)
if ( 'TEST COMPANY' != WC()->customer->get_billing_company() ) return;
$percent = 8;
// Calculation
$surcharge = ( $cart->cart_contents_total + $cart->shipping_total ) * $percent / 100;
// Add the fee (tax third argument disabled: false)
$cart->add_fee( __( 'VAT', 'woocommerce')." ($percent%)", $surcharge, false );
}
Many thanks in advance.

Apply a discount for a specific user role in Woocommerce

I have a woocommerce store, with 3 user roles, I want to provide a 10% discount on the cart total only for a user role 'company'.
I found "Percentage discount based on user role and payment method in Woocommerce" answer that is very neer of what I need, but don't know how to modify the code to feet my needs, as it contains also a condition based on payment method and displaying the discount only at checkout, but I need it to display in cart as well.
The following code will apply a 10% discount for 'company' user role (on cart and checkout):
// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('company') )
return; // Exit
// HERE define the percentage discount
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
Code goes on functions.php file of your active child theme (or active theme). It should works.

Woocommerce change price based on quantity and variation

I am currently creating my first site using woocommerce for a client, and they are requesting for me to have a price increase per variation and quantity, however i am not sure how to do it. Here is an example of what they are asking for:
Product Type: Limestone
Variations:
Product Size:
10kg bag
20kg bag
40kg bag
Quantity:
1-20
TL;DR
if the customer wants multiple 10kg bags, the price increases by £36 at a time, if they want multiple 20kg bags, the price increases by £30 at a time, and if they want multiple 40kg bags, the price doubles every time.
I have used woocommerce variations and attributes so far but now it would require modifying having 3x20 variations all of which require a different price, which would be easier to change if a statement is possible as follows:
if ($productsize === '10kg') {
$quantity = 2; //get quantity from textbox $_POST
$initial_price = 36; //get inital price from woocommerce global variable
$total_price = $inital_price * $quantity;
update_price($total_price); //a function to update the price in woocommerce when adding to cart
}
Obviously the code above does not use any woocommerce variables, however doing this for only 3 variations would be easier than manually entering 60 variations on one product for which we have >60 on our site.
I don't know if you need it anymore, however try it
add_action( 'woocommerce_before_calculate_totals', 'IG_recalculate_price',5,1 );
function IG_recalculate_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$quantity = 0;
foreach ( $cart_object->get_cart() as $key => $value ) {
// count q.ty
$quantity += $value['quantity'];
// delta q.ty
if( $quantity > 24 ) {
// get price by custom field but you can use a simple var
$newprice = get_post_meta( $value['data']->get_id(), 'custom_field2', true);
$value['data']->set_price( $newprice );
// reset q.ty for check every item in the cart
$quantity = 0;
}else{
$newprice = get_post_meta( $value['data']->get_id(), 'custom_field', true);
$value['data']->set_price( $newprice );
$quantity = 0;
}
}
}

Woocommerce additional price (woocommerce_before_calculate_totals) is not adding during payment

In my application there is need to add extra price to each product if customer wants certain services, so I have added price from session (the extra price ) to each product by using woocommerce_before_calculate_totals hook,It works fine, whenever the total is showing including my extra price.
But the extra price is not added while making payment, only the product original price is sent to payment gateway, other wise in all places like (cart / checkout the right amount is showing)
Is there any hook that I am missing for payment where the extra price is not added.
Thanks in advance.
You have to use the following hook woocommerce_cart_calculate_fees.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
For reference: https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
add_fee reference: http://woocommerce.wp-a2z.org/oik_api/wc_cartadd_fee/

Categories