Need to set "Zero Rates" as default tax rate in Woocommerce - 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;
}

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!

Create a shortcode to display the Woocommerce flat rate price as displayed on the checkout

I would like to create a plugin to add 2 shortcodes to my woocommerce shop so I can add the shipping cost as shortcode in text like this example:
Our shipping costs to Amsterdam is [shipping_cost_1] Euro
So on front end the text will be like this: Our shipping costs to Amsterdam is 6,95 Euro
These are the rates and shortcodes I want to use:
[shipping_cost_1] = flat rate woocommerce shipping cost including VAT for shipping zone 1 (shipping inside Amsterdam)
[shipping_cost_2] = flat rate woocommerce shipping cost including VAT for shipping zone 2 (shipping outside Amsterdam)
For reference, this is where the shipping costs for Amsterdam are displayed on the checkout page:
I would like to add the code in the following structure:
// The shortcode function
function shipping_cost_display_1() {
// Get shipping cost from woocommerce
???
// Ad code returned
???
// Register shortcode
add_shortcode('shipping_cost_1', 'shipping_cost_display_1');
just wondering if you checked this article already: WooCommerce: Show Shipping Rates # Single Product Page?
Basically you can get the shipping zones with a simple command:
WC_Shipping_Zones::get_zones();
Once you have them, you can loop over the array and find rates inside ($instance['cost']).
The following should do the trick for you - of course change "amsterdam" to your desired zone name:
function shipping_cost_display_1() {
$zones = WC_Shipping_Zones::get_zones();
foreach ( $zones as $zone_id => $zone ) {
if ( "amsterdam" !== $zone['zone_name'] ) continue;
$zone_shipping_methods = $zone['shipping_methods'];
foreach ( $zone_shipping_methods as $index => $method ) {
$instance = $method->instance_settings;
return $instance['cost'];
}
}
}
add_shortcode( 'shipping_cost_1', 'shipping_cost_display_1' );
There is also to say that the same shortcode could be used to return the two rates, by using a shortcode parameter.

Change cart total for payment gateways In WooCommerce 2.6.x [duplicate]

I am running into issues with the cart total only displaying 0
Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal.
So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value.
I took the code from here: Change total and tax_total Woocommerce and customize it this way:
add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);
function calculate_totals($wc_price){
$new_total = ($wc_price*0.10);
return wc_price($new_total);
}
But the total amount shows 0.00 when that code is enabled. If removed the code, I get the standard total.
I also could not find on the woocommerce site where the full api is listed, only generic articles related to how to create a plugin.
Any help or a point in the right direction would be great.
This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:
function prefix_add_discount_line( $cart ) {
$discount = $cart->subtotal * 0.1;
$cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Since Woocommerce 3.2+
it does not work anymore with the new Class WC_Cart_Totals ...
New answer: Change Cart total using Hooks in Woocommerce 3.2+
First woocommerce_cart_total hook is a filter hook, not an action hook. Also as wc_price argument in woocommerce_cart_total is the formatted price, you will not be able to increase it by 10%. That's why it returns zero.
Before Woocommerce v3.2 it works as some WC_Cart properties can be accessed directly
You should better use a custom function hooked in woocommerce_calculate_totals action hook this way:
// Tested and works for WooCommerce versions 2.6.x, 3.0.x and 3.1.x
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
// $cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
$cart_object->cart_contents_total *= 1.1;
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Is also possible to use WC_cart add_fee() method in this hook, or use it separately like in Cristina answer.

Woocommerce change total price depending payment methods [duplicate]

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;
}

Woocommerce flat rate and free shipping

What's the best way to set up a flat rate shipping fee up to a certain dollar amount, and then free shipping over that dollar amount? I'd like to set it up like this: $15 shipping for all items in their cart if cart is less than $50, free shipping on orders over $50. Thanks!
You can achieve this with following way
Config the flat rate in WooCommerce zone
Config the Free shipping with minimum order amount $50
Use below code snippet to hide Flate rate if the cart total is above $50
add_filter('woocommerce_package_rates', 'hide_flat_rate_based_on_cart_total', 10, 3);
function hide_flat_rate_based_on_cart_total( $available_shipping_methods, $package ){
$price_limit = 50;
if( WC()->cart->get_total() > $price_limit ){
foreach($available_shipping_methods as $method_key => $method){
if ( strpos($method->method_id, 'flat_rate' ) !== false) {
unset($available_shipping_methods[$method_key]);
}
}
}
return $available_shipping_methods;
}

Resources