Woocommerce change total price depending payment methods [duplicate] - wordpress

This question already has an answer here:
Add a fee based on shipping method and payment method in Woocommerce
(1 answer)
Closed 2 years ago.
I want to change total price depending payment method in checkout page , i have two payments method
if the customer select Cash on delivery Total price become Total * 0.015 + Total
Else the Total price remains unchanged

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file. This code is tested and works.
All payment methods are only available on Checkout page.
add_action('woocommerce_cart_calculate_fees','custom_handling_fee',10,1);
function custom_handling_fee($cart){
if(is_admin() && ! defined('DOING_AJAX'))
return;
if('cod' === WC()->session->get('chosen_payment_method')){
$extra_cost = 0.015;
$cart_total = $cart->cart_contents_total;
$fee = $cart_total * $extra_cost;
if($fee != 0)
$cart->add_fee('COD Charge',$fee,true);
}
}
You will need the following to refresh checkout on payment method change, to get it work:
add_action( 'wp_footer','custom_checkout_jqscript');
function custom_checkout_jqscript(){
if(is_checkout() && ! is_wc_endpoint_url()):
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}

Related

I want to include fees depending on the payment method, using the plugin "Deposits for Woocommerce"

I'm using "deposits for Woocommerce" from Pluginhive and Woocommerce, and I have a question with a code I want to implement.
I would like to be able to charge a small amount (2%) to people who use the credit card payment. I am using the plugin "WooCommerce Payment Gateway Based Fees" for this.
Everything would be great if that would work, but there's a problem.
Deposits for Woocommerce allows you to split the payment, I split it into 30% for the first payment and 70 for the rest. But the plugin "WooCommerce Payment Gateway Based Fees" calculates 2% of the total, that is, not 30% but 100%.
But when it comes to collecting the second payment, it does calculate 60% of the payment correctly.
I have tried to include codes like the following to change the plugin, but what happens is that they load perfectly in the first payment (they calculate 30%), BUT they don't work in the second one, so they don't add anything. So, I thought I could use that code to reduce the rate on the first payment (as a hidden discount).
Code:
add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 10, 1 );
function ts_add_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Mention the payment method e.g. cod, bacs, cheque or paypal
$payment_method = 'redsys';
// The percentage to apply
$percent = 4.755; // 4.755%.
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('$chosen_payment_method'); //Get the selected payment method
if( $payment_method == $chosen_payment_method ){
$label_text = __( "Discount International Charge" );
// Calculating percentage
$discount = number_format(($cart_total / 100) * $percent, 2);
// Adding the discount
$cart_object->add_fee( $label_text, -$discount, false );
$label_text = __( "Result" );
//// Calculating percentage
//$result = $fees_calc - $discount;
//// Adding the discount
//$cart_object->add_fee( $label_text,-$result, false );
}
}
add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );
function ts_refresh_payment_method(){
// jQuery
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 10, 1 );
So far everything seemed to be working correctly. I then realised that there is a bug: When someone tries to pay 100% directly, the plugin correctly calculates 2% of the total price, but my code jumps too, reducing that 2% and actually creating a discount.
I have tried several codes, but none of them works on the second payment. Only the plugin has managed to work there, so there must be some code or string that uses that plugin to affect the price of that second payment, which my code is not detecting.
I would be delighted if anyone could help with that code.
Thanks!

Woocommerce - addon fees and filtering payments based on shipping

i have a small problem. In my project a few months ago i used add_filter to filtering payments methods based on shipping. I have shipping methods for cash on delivery packages so I hide online payments methods if customer select one of this methods. My code:
add_filter('woocommerce_available_payment_gateways','gateway_disable_for_shipping_rate', 10);
function gateway_disable_for_shipping_rate($available_gateways)
{
if (!is_admin()) {
$chosen_methods = WC()->session->get('chosen_shipping_methods');
$chosen_shipping = $chosen_methods[0];
// some conditionals...
}
return $available_gateways;
}
Today i must add fees based on payment method. I realize that with this code:
add_action('woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway', 10);
function add_checkout_fee_for_gateway()
{
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$cart = WC()->cart;
// RESET
$cart->fees_api()->set_fees([]);
// END RESET
$chosen_gateway = WC()->session->get('chosen_payment_method');
if ($chosen_gateway === 'paypal') {
$tax = $cart->subtotal * 0.05;
$cart->add_fee('PayPal', $tax);
} else if ($chosen_gateway === 'eh_stripe_pay') {
$tax = $cart->subtotal * 0.02;
$cart->add_fee('Stripe', $tax);
} else if ($chosen_gateway === 'cod') {
$tax = $cart->subtotal * 0.02;
$cart->add_fee('COD', $tax);
}
}
add_action('wp_footer', 'refresh_checkout_on_payment_methods_change');
function refresh_checkout_on_payment_methods_change()
{
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return;
?>
<script type="text/javascript">
(function($){
$('form.checkout').on('change', 'input[name^=\'payment_method\']', function(){
$(document.body).trigger('update_checkout');
});
$('form.checkout').on('change', 'input[name^=\'shipping_method\']', function(){
$(document.body).trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
And problem is with fee. Scenario:
I change shipping method. Shipping method has payment via paypal. I check this and fee is added to total price -> good.
Now i change shipping method. Now shipping method has only payment via cod. My fee is not update.
My fee updates only if i can click to payment method, if i cant click - not update.
I log this and i think actions to calculate fee run first, before run filtering.
So i have 'checked method' and calculate fee, but after run filter this method is disabled but calculates for fee are already saved. If i change methods many times always calculate fee based on previous method, not current checked.
Small example:
https://vimeo.com/720301741

Need to set "Zero Rates" as default tax rate in Woocommerce

I run an eCommerce site that sells food and drink. Most food products are charged 0% VAT here in Ireland so I'm wondering if there's a way of changing the default tax rate to Zero in the Product section in WooCommerce. This would avoid me having to change it for the odd product that does require either Reduced or Standard rates. I'm currently using the following code to make the default Non-taxable but this disables the Zero numbers on the monthly reports (which Revenue needs to see). Any other options?
add_action('woocommerce_product_options_tax', 'new_product_tax_status_none');
function new_product_tax_status_none()
{
global $post, $pagenow;
// Only on new product pages
if( $pagenow === 'post-new.php' ) :
?>
<script>
jQuery(function($){
// On load set the tax status to none
$('select[name="_tax_status"]').val('none');
});
</script>
<?php
endif;
}

Conditionally hide checkout field on chosen payment method at checkout page

I am using the following code for hiding credit card number field for chosen payment method. It's hiding the field perfectly but when the field is hidden and I click on Place Order button, it still shows 'Credit Card Number is required'.
// show hide credit card fields start
add_action( 'wp_footer', 'conditionally_show_hide_card_number_field' );
function conditionally_show_hide_card_number_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#credit_card_number_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' )
{
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "credit_card"
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "credit_card"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
// show hide credit card fields ends
Please suggest the change in code how can I overcome this problem.
You cannot overcome this unless you make the CC number field optional which is a bad idea because then you're agreeing to accept CC payments without a CC number, too; and those payments will fail. This becomes a usability nightmare. WooCommerce, by default, has it's own way of handling this and no manual effort is needed by you (entire section is hidden and non-mandatory when choosing another payment method). What have you changed, or what are you trying to accomplish which has removed this built-in feature? I'm thinking that you might just be asking the wrong question.

WooCommerce - Open the new product that the category is selected

I've been searching all over the internet for hours how to automatically assign a category to new products. When you open the new product that the category is selected.
I have two product categories (Books and FIlms) and I would like that every time we open a new product, Books are selected. In this way it will directly show the custom fields of that product category and we will speed up the creation process.
It's possible?
thanks
UPDATE
I found a code that seems to work with the posts. Any idea how I can adapt the code for products and where should I put the category I care about?
https://wordpress.stackexchange.com/questions/243462/automatically-select-categories-on-new-post-based-on-get-value
When it comes to the checkbox that should already be checked when creating a new product, you can use jQuery
Replace in-product_cat-15 with in-product_cat-{YOUR-CAT-ID}
// Define the admin_head callback
function action_admin_head() {
global $post, $pagenow;
// Only on new product pages
if( $pagenow != 'post-new.php' ) return;
?>
<script>
jQuery(function($){
$( '#in-product_cat-15' ).attr( 'checked', true );
});
</script>
<?php
}
add_action( 'admin_head', 'action_admin_head', 10, 0 );
In product categories click the Make default action of your desired category to make it as default category. For more details refer the image:
You can visit this page using this URL (replace the domain with your domain):
www.example.com/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

Resources