Change Recurring Total Shipping Label to not include "FREE" - php

I am having a hard time finding any documents based on the WooCommerce Subscription plugin to adjust shipping label names.
I would like to remove the word: FREE at the end.
I have tried using this function from another Stack Overflow user, but it doesn't even seem to fire in the 'cart':
/**
* Remove shipping name from the label in Cart and Checkout pages
*/
function sticky_wc_cart_totals_shipping_method_label( $method ) {
$label = $method->get_label();
if ( $method->cost > 0 ) {
if ( WC()->cart->tax_display_cart == 'excl' ) {
$label .= ': ' . wc_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
$label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
} else {
$label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
$label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
}
}
return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'sticky_wc_cart_totals_shipping_method_label', 10, 2 );
What I don't know, is if this is only for the CART TOTALS, and not the recurring total shipping.
Any thoughts on this?

I have found a solution. After downloading the subscriptions plugin, I was able to find the code associated to it. So I created this filter and seems to be working just fine.
/**
* Remove shipping pricing from the label in Cart and Checkout pages
*/
add_filter( 'wcs_cart_totals_shipping_method', 'wcs_method_label', 10, 2 );
function wcs_method_label($method, $cart) {
return $cart->label;
}

Related

How to add next available date in stock WooCommerce

I have this code that's suppose to add a next available date in stock when out of stock. I added the field using custom fields.
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
$date_of_availability = get_post_meta( get_the_ID(), 'date_of_availability', true );
if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
$availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . get_post_meta( get_the_ID(), 'date_of_availability', true ) . '!important' . ')</span>';
}
return $availability;
}
custom fields
Could someone help me ?
The end goal is to have a field in the product with a date, when out of stock is reached it needs to print that line out with the field value
If I however place it like this:
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
$date_of_availability = get_post_meta( get_the_ID(), 'date_of_availability', true );
if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
$availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . get_post_meta( get_the_ID(), 'date_of_availability', true ) . ')</span>';
echo $availability
}
// return $availability;
}
Then it works however for some reason it repeats, like this:
repeating
Also if enabled it leaves an In stock label on every product despite being disabled
The reason your function appears to output twice is that you are echoing the $availability value without modifying or ceasing return output of woocommerce_get_availability text.
The following should work, as it works on Storefront theme:
function filter_product_availability_text( $availability, $product ) {
$date_of_availability = $product->get_meta( 'date_of_availability' );
if ( ! $product->is_in_stock() && $date_of_availability ) {
$availability .= '- Available from: ';
$availability .= $date_of_availability ;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
However, if the above fails, you can output with an action hook. In your case, the filter may be failing because of something Bacola developers chose to do with their code. This should output regardless:
function filter_product_availability_text() {
global $product;
$availability = '';
$date_of_availability = $product->get_meta( 'date_of_availability' );
if ( ! $product->is_in_stock() && $date_of_availability ) {
$availability = '<div class="product-meta date-of-avail"><p>Available from: '. $date_of_availability .'</p></div>';
}
echo $availability;
}
add_action( 'woocommerce_single_product_summary', 'filter_product_availability_text' );
Add to your CSS file:
.product-meta p {
color: #F00;
}
You need to comment:
echo $availability
Code snippets:
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2);
function filter_product_availability_text( $availability, $product ) {
$date_of_availability = '17-05-2022'; // Your custom data
if ( ! $product->is_in_stock() && ! empty($date_of_availability) ) {
$availability .= '<span style="color:#e2401c;"><strong>- (' . __('Available from:', 'flatsome') . ' </strong>' . $date_of_availability . ')</span>';
//echo $availability
}
return $availability;
}
enter image description here
Alternatively, there’s also a built-in setting for this under WooCommerce > Settings > Products > Inventory — you can set when it should be considered “low stock”, and then display a message:
enter image description here
You could change the wording of the message by using a free translation plugin like Loco Translate ( https://wordpress.org/plugins/loco-translate/ ).

Change number of decimals on Woocommerce displayed cart subtotal

I am Using "Change number of decimals in Woocommerce cart totals" answer code to display the total with 2 decimals. The code works fine for the displayed total in cart and checkout pages.
I would like if possible to do the same thing for the subtotal too. Any help is appreciated.
To format the displayed subtotal amount with 2 decimals in cart and checkout pages use the following:
// Displayed formatted cart subtotal in cart and checkout pages
add_filter( 'woocommerce_cart_subtotal', 'filter_cart_subtotal_html', 10, 3 );
function filter_cart_subtotal_html( $cart_subtotal, $compound, $cart ) {
$decimals = 2; // <== <== Set the number of decimals to be displayed
$args = ['decimals' => strval($decimals)]; // Initializing
if ( $compound ) {
$cart_subtotal = wc_price( $cart->get_cart_contents_total() + $cart->get_shipping_total() + $cart->get_taxes_total( false, false ), $args );
} elseif ( $cart->display_prices_including_tax() ) {
$cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax(), $args );
if ( $cart->get_subtotal_tax() > 0 && ! wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$cart_subtotal = wc_price( $cart->get_subtotal(), $args );
if ( $cart->get_subtotal_tax() > 0 && wc_prices_include_tax() ) {
$cart_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
return $cart_subtotal;
}
Code goes in functions.php file of the active child theme (or active theme), or a plugin file. Tested and works.
Related: Change number of decimals in Woocommerce cart totals

Display tax amount based on specific tax class in WooCommerce variations

I'm currently using a custom function to target a specific product and change the output of the price with and without tax on the product page.
This currently works as intended for an individual product id, however trying to get this work for a specific tax_class instead with no avail
add_filter( 'woocommerce_available_variation', 'tax_variation', 10, 3);
function tax_variation( $data, $product, $variation ) {
$product = wc_get_product();
$id = $product->get_id();
$price_excl_tax = wc_get_price_excluding_tax( $variation ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $variation ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
if ( $id == 113576 ) {
$data['price_html'] = "<span class='ex-vat-price'>Price: <b>" . woocommerce_price($variation->get_price_excluding_tax()) . "</b></span><br>";
$data['price_html'] .= "<span class='tax_amount'>Sales Tax 13%: <b>" . woocommerce_price($tax_amount) . "</b></span><br>";
$data['price_html'] .= "<span class='inc-vat-price'>Total: <b>" . woocommerce_price($variation->get_price_including_tax()) . "</b></span>";
return $data;
} else {
$data['price_html'] .= "<span class='regular-price'> " . woocommerce_price($variation->get_price()) . "</span>";
return $data;
}
}
i want to change the if parameter to
$taxclass = $product->get_tax_class();
if ( $taxclass == 'costa-rate' ) {
but currently this does not function correctly and displays the regular price data twice
Since WooCommerce 3, your code is outdated and with some errors.
I guess that you are using woocommerce_available_variation action hook.
Try the following instead:
add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3 );
function custom_variation_price( $data, $product, $variation ) {
$price_excl_tax = (float) wc_get_price_excluding_tax( $variation ); // price without VAT
$price_incl_tax = (float) wc_get_price_including_tax( $variation ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax;
if( $variation->get_tax_class() === 'costa-rate' ) {
$data['price_html'] = '<span class="ex-vat-price">' . __("Price:") . ' <strong>' . wc_price($price_excl_tax) . '</strong></span><br>
<span class="tax_amount">' . __("Sales Tax 13%:") . ' <strong>' . wc_price($tax_amount) . '</strong></span><br>
<span class="inc-vat-price">' . __("Total:") . ' <strong>' . wc_price($price_incl_tax) . '</strong></span>';
} else {
$data['price_html'] .= '<span class="regular-price"> ' . wc_price( wc_get_price_to_display( $variation ) ) . '</span>';
}
return $data;
}
Code goes in functions.php file of your active child theme (or active theme). It should better works.

Make variation stock quantity appear on dropdown selection in Woocommerce

Currently this is what it shows in our site with the current code:
The current code shows all the stocks before even clicking on any of the variation/sizes. What our client wants is to only show that specific variation's stock quantity once they are clicked/selected on the drop down.
Does anyone have an idea how to do this?
Current Code:
add_action( 'woocommerce_before_add_to_cart_button', 'display_stock_variations_loop' );
function display_stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ( $product->get_available_variations() as $key ) {
$attr_string = array();
foreach ( $key['attributes'] as $attr_name => $attr_value ) {
$attr_string[] = $attr_value;
}
if ( $key['max_qty'] > 0 ) {
echo '' . implode( ', ', $attr_string ) . ': ' . $key['max_qty'] . ' in stock<br>';
} else {
echo '' . implode(', ', $attr_string ) . ': out of stock<br>';
}
}
echo '<br>';
}
}
The displayed selected variation stock quantity and status is already managed by woocommerce. You just need to add your "Size" attribute term name value, using the following (without any javascript need):
add_filter( 'woocommerce_get_availability_text', 'filter_variations_availability_text', 10, 2 );
function filter_variations_availability_text( $availability_text, $product ) {
if( $product->get_type() == 'variation' && $product->get_attribute('size') ) {
$availability_text = $product->get_attribute('size') . ': ' . $availability_text;
}
return $availability_text;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Conditionally customize WooCommerce cart grand total output based on a fee

I try to do a quite simple thing but for me i don´t get it to work.
This is from the wc-cart-functions.php
if ( ! empty( $tax_string_array ) ) {
$taxable_address = WC()->customer->get_taxable_address();
$estimated_text = WC()->customer->is_customer_outside_base() && ! WC()->customer->has_calculated_shipping()
? sprintf( ' ' . __( 'estimated for %s', 'woocommerce' ), WC()->countries->estimated_for_prefix( $taxable_address[0] ) . WC()->countries->countries[ $taxable_address[0] ] )
: '';
$value .= '<small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
}
}
echo apply_filters( 'woocommerce_cart_totals_order_total_html', $value );
}
The $value should be different if any $fee is applied.
But I only get 0.00 € Value from $fee Variable if I check for.
Could someone please help me with simply apply the Value of the FEE in a Variable and check in a simple if clause like:
if fee is applied ---> 1
else ---> 2
How can I do it?
As you can see you can use the woocommerce_cart_totals_order_total_html filter hook located in that wc-cart-functions.php core code, to alter the output of grand cart total:
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_cart_totals_order_total_html', 10, 1 );
function custom_cart_totals_order_total_html( $value ) {
// Get the fee cart amount
$fees_total = WC()->cart->fee_total;
// HERE is the condition
if( ! empty($fees_total) && $fees_total != 0 ){
// Change/customize HERE $value (just for test)
$value .= " <small>($fees_total)<small>";
}
// Always return $value
return $value;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Never overide core files, as this is something prohibited, dangerous and not convenient for many reasons
You will need to customize the code in the condition for $value

Categories