I had a surcharge extra fee on functions.php for woocommerce but i need that this surcharge are not refundable. THe idea is that make full refunds automatically but this surcharge are NOT REFUNDABLE and we need that the Order Status change automatically to REFUNDED on Woocommerce.
Anyone had do this before?
This the code used:
/**
** Surcharge for MercadoPago
**/
add_action( 'woocommerce_cart_calculate_fees', 'add_mp_checkout_fee_for_gateway' );
function add_mp_checkout_fee_for_gateway() {
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( $chosen_gateway == 'woo-mercado-pago-basic' ) {
WC()->cart->add_fee( 'Tasa de EmisiĆ³n', 35 );
}
}
add_action( 'woocommerce_after_checkout_form', 'refresh_checkout_on_payment_methods_change' );
function refresh_checkout_on_payment_methods_change(){
wc_enqueue_js( "
$( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() {
$('body').trigger('update_checkout');
});
");
}
Related
I'm trying to add an additional fee to the COD payment method.
The below code is working fine but only when I'm logetin as a administrator.
If I logout the fee doesn't show in the front end and I don't understand why.
For some reason the code is working only if I'm logget in as a administrator.
// add addition fee to cod payment method
if ( ICL_LANGUAGE_CODE=='cs' ){
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// get your payment method
$chosen_gateway = WC()->session->chosen_payment_method;
//echo $chosen_gateway;
$fee = 17;
if ( $chosen_gateway == 'cod' ) { //test with cash on delivery method
WC()->cart->add_fee( 'Delivery fee', $fee, false, '' );
}
}
}
// 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');
});
});");
}
I've created a specific method of payment to manage approval or decline of orders for specific products.
Once order was approved I set order in status "Waiting for payment" and send an email to customer with link to pay.
The problem is that for some payment method I have to apply a discount.
I'm able to apply discount before order was placed ( I use it for order that doesn't require approval), but I don't know how to recalcultate the total in this moment.
For do that I use this code:
add_action( 'woocommerce_cart_calculate_fees','payment_method_discount', 20, 1 );
function payment_method_discount( $cart_object ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
foreach($cart_object->cart_contents as $cart_item_id => $cart_item) {
$payment_method_arr = array('ppcp-gateway');
foreach($payment_method_arr as $payment_method) {
$percent = 25;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ) {
WC()->cart->add_fee( 'PayPal fee', 0 - $discount );
}
}
}
}
To trigger the update I used:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods', 20, 1 );
function refresh_payment_methods() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
I've tried also to add the function refresh_payment_methods to wp_footer hook.
When a user is on my woocommerce store checkout, I want to set the tax to 0 if they choose a certain payment gateway like "paypal_pro", "cheque", "bacs", or in my case "wdc_woo_credits". Which is a woocredits plugin that allows users to pay with credits instead of a credit card.
I know this function sets the taxes correctly because when I print_r($cart_object) I see I set all the taxes to 0, but yet the checkout still applies the tax in the total.
I think I need a function that recalculates the taxes after I set it using the function below.
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
$cart_object->set_cart_contents_taxes(0);
$cart_object->set_cart_contents_tax(0);
$cart_object->set_subtotal_tax(0);
$cart_object->set_shipping_tax(0);
$cart_object->set_total_tax(0);
$cart_object->set_fee_tax(0);
$cart_object->set_fee_taxes(0);
$cart_object->set_subtotal_tax(0);
foreach($cart_object->cart_contents as $product){
$cart_object->cart_contents[$product['key']]['line_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_subtotal_tax'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['subtotal'] = 0;
$cart_object->cart_contents[$product['key']]['line_tax_data']['total'] = 0;
}
}
//echo '<pre>'; print_r($cart_object); echo '</pre>';
}
This function detects what payment selection they choose and re-runs the update checkout function. But yet the tax is still in the total.
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
After lots of googling, i found out that I don't need to mess with the cart totals at all. I need to alter the current logged in customer.
I can do it with this function which removes tax for the current logged in user.
WC()->customer->set_is_vat_exempt( true );
all this goes in functions.php. You still need the woocommerce_review_order_before_payment function from above which triggers the ajax. So here's the complete code to hide tax on a certain payment gateway for only logged in users.
// calculate fees on checkout page
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
//if( $chosen_payment_method == 'cheque' ){
if( $chosen_payment_method == 'wdc_woo_credits' ){
// if user buys with credits, dont allow tax.
WC()->customer->set_is_vat_exempt( true );
}else{
// if user buys with credit card, allow tax
WC()->customer->set_is_vat_exempt( false );
}
}
// refresh the checkout page totals once user selects
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
I have the following code that adds a Handling fee to orders with subtotal under $100. I'm trying to make a coupon that will remove the handling fee (or make it $0) that I can give to specific customers, even if their order is under $100. I've made a coupon in Woocommerce and it takes off the $25 from the cart subtotal without changing the Handling. If I don't make a coupon in Woocommerce, I get the "this coupon doesn't exist" message when I try to apply the coupon. Help?
function woo_add_cart_fee() {
global $woocommerce;
$subt = $woocommerce->cart->subtotal;
if ($subt < 100 && $coupon_code == "NOHANDLING" ) {
$handling = 0;
$woocommerce->cart->add_fee( __('Handling', 'woocommerce'), $handling );
}
elseif ($subt < 100 ) {
$handling = 25;
$woocommerce->cart->add_fee( __('Handling', 'woocommerce'), $handling );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
Thanks.
I need to disable Woocommerce COD option and make it unchecked when total price exceeded certain amount.
I tried this code, but does nothing!
add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {
global $woocommerce;
$shipping_cost = WC()->cart->get_cart_shipping_total();
$amount = $woocommerce->cart->cart_contents_total + $woocommerce->cart->tax_total + $shipping_cost;
$max = 999.9 * WCPBC()->customer->exchange_rate;
if($amount >= $max){ ?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$("#payment_method_cod").disabled = true;
$("#payment_method_cod").checked = false;
});
</script>
<?php
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $available_gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD amount exceeded!', 'woocommerce' ), 'notice' );
}
add_filter( 'woocommerce_available_payment_gateways' , 'hide_payment_gateway', 20, 1);
/**
* remove cod gateway if cart total > 100
* #param $gateways
* #return mixed
*/
function hide_payment_gateway( $gateways ){
//change whatever amount you want
if( WC()->cart->subtotal < 699 ){
// then unset the 'cod' key (cod is the unique id of COD Gateway)
unset( $gateways['cod'] );
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD option not available on orders below 699.00', 'woocommerce' ), 'notice' );
}
First you need to deregister the woocommerce/assets/js/frontend/checkout.js
Then include checkout.js in your theme assets folder and register it again after theme script files
then your code will work as you expect.
for deregister use below code,
wp_deregister_script( $handle );
again register by,
wp_enqueue_script(parameters);
You should change your jquery code. Can you please try below code and check your code again. Because that is working for me.
<script type="text/javascript">
jQuery( document ).ready(function($) {
$("#payment_method_cod").prop('checked', false);
$("#payment_method_cod").attr('disabled', true);
});
</script>