Change wc_cart_totals_shipping_method_label function in Woocommerce - php

Inside Woocommerce plugin on the includes subfolder, there's a file wc-cart-functions.php.
I would like to change the function wc_cart_totals_shipping_method_label(), but I am not allowed to copy the function to my theme's functions.php. I believe I have to use a custom action/filter to change this core function, but no idea how to do that.
Original function:
function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->get_label();
$has_cost = 0 < $method->cost;
$hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
if ( $has_cost && ! $hide_cost ) {
if ( WC()->cart->display_prices_including_tax() ) {
$label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$label .= ': ' . wc_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}
What the function should be:
function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->get_label();
$has_cost = 0 < $method->cost;
$hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
if ( $has_cost && ! $hide_cost ) {
if ( WC()->cart->display_prices_including_tax() ) {
$label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
if ( $method->get_shipping_tax() > 0 && ! wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$label .= ': ' . wc_price( $method->cost );
if ( $method->get_shipping_tax() > 0 && wc_prices_include_tax() ) {
$label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
}
/* Here's the code I want added */
elseif ( ! $has_cost && ! $hide_cost ) {
$label .= ': Free';
}
return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}
What I want to do:
Shipping methods that have zero cost should have "Free" added to their label. Right now there's nothing shown beside the method title.
How can I make the necessary changes to this function, without overwriting Woocommerce core files?

As you can see in this function you can use woocommerce_cart_shipping_method_full_label filter hook, to manage that change, this way:
add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_cart_shipping_method_full_label', 10, 2 );
function change_cart_shipping_method_full_label( $label, $method ) {
$has_cost = 0 < $method->cost;
$hide_cost = ! $has_cost && in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true );
if ( ! $has_cost && ! $hide_cost ) {
$label = $method->get_label() . ': Free';
}
return $label;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

stock quantity woocommorce

i have a problem with my stock display.
our current code, if the number of "variant products" is more than 5, it does not show stock information. we want to apply it also for "simple products". How should we revise our code?
function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if ( $variation['attributes'][$name] == $term_slug ) {
$variation_obj = wc_get_product( $variation['variation_id'] );
// if the stock of the product is greater than 20 it returns
if ( $variation_obj->get_stock_quantity() > 5 ) {
return '';
}
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme-hello') : ' - ' . $stock_qty . ' ' . __(pll__('adet Stokta'), 'mytheme-hello');
}
I would be very happy if you can help.
Thanks in advance.
You can simply check product type using is_type(). check the below code.
function get_variation_stock_status( $product, $name, $term_slug ){
if ( $product->is_type( 'simple' ) ) {
// if the stock of the product is greater than 20 it returns
if ( $product->get_stock_quantity() > 5 ) {
return '';
}
$stock_qty = $product->get_stock_quantity();
} elseif ( $product->is_type( 'variable' ) ) {
foreach ( $product->get_available_variations() as $variation ){
if ( $variation['attributes'][$name] == $term_slug ) {
$variation_obj = wc_get_product( $variation['variation_id'] );
// if the stock of the product is greater than 20 it returns
if ( $variation_obj->get_stock_quantity() > 5 ) {
return '';
}
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
}
return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme-hello') : ' - ' . $stock_qty . ' ' . __(pll__('adet Stokta'), 'mytheme-hello');
}

Woocommerce get stock quantity of each item variation

I have a page like this:
I have manage stocks enabled in variations, and I want to filter out items that are not in stock. For example, if size 9.5 is not available in variations, it should not appear in the front end as well. The questions posted here are all quite old and I could not get any of the suggestions posted in them to work. Here is what I have done so far:
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $attribute_name ) ) {
$is_attr_color = false;
$attribute_color = wc_sanitize_taxonomy_name( 'color' );
if( $attribute_name == wc_attribute_taxonomy_name( $attribute_color ) ){
$is_attr_color = true;
}
$terms = wc_get_product_terms( $post->ID, $attribute_name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
if( $is_attr_color ){
$datas = get_term_meta( $term->term_id, 'ts_product_color_config', true );
if( strlen( $datas ) > 0 ){
$datas = unserialize( $datas );
}else{
$datas = array(
'ts_color_color' => "#ffffff"
,'ts_color_image' => 0
);
}
}
$class = sanitize_title( $selected_value ) == sanitize_title( $term->slug ) ? 'selected' : '';
$class .= ' option';
if( $is_attr_color ){
$class .= ' color';
}
echo '<div data-value="' . esc_attr( $term->slug ) . '" class="' . $class . '">';
if( $is_attr_color ){
if( absint($datas['ts_color_image']) > 0 ){
echo '' . wp_get_attachment_image( absint($datas['ts_color_image']), 'ts_prod_color_thumb', true, array('title'=>$term->name, 'alt'=>$term->name) ) . '';
}
else{
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
else{
// !!!!!!!!!!MAIN ISSUE HERE!!!!!!!!!!!!
if($term->count > 0){
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
echo '</div>';
}
} else {
foreach ( $options as $option ) {
$class = sanitize_title( $selected_value ) == sanitize_title( $option ) ? 'selected' : '';
$class .= ' option';
echo '<div data-value="' . esc_attr( $option ) . '" class="' . $class . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
}
}
Any idea how I could fix this? I have been at it for hours and I cannot find a solution for it. It's really frustrating.

php operator without spaces

I have this shopkeeper running in Wordpress. And I want to change the colon to parentheses but it gets weird , I don't want spaces between it just like this " (€5,50).
I've been trying to fix this problem for almost a day.
really hope to see any answers.
here is the html : gyazo.com/f05c00f01d2522aa962d90f5c0fcc5ea
method function :
function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->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>';
}
}
} elseif ( $method->id !== 'free_shipping' ) {
$label .= ' (' . __( 'Free', 'woocommerce' ) . ')';
}
return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}
code where I print :
<select name="shipping_method[<?php echo $index; ?>]" data-index="<?php echo $index; ?>" id="shipping_method_<?php echo $index; ?>" class="shipping_method">
<?php foreach ( $available_methods as $method ) : ?>
<option value="<?php echo esc_attr( $method->id ); ?>" <?php selected( $method->id, $chosen_method ); ?>><?php echo wp_kses_post( wc_cart_totals_shipping_method_label( $method ) ); ?></option>
<?php endforeach; ?>
</select>
Before the result try to add this:
$label = '(€'.preg_replace("/\([\s]+?/", "", str_replace(array('€', ' '), '', $label));
First use str_replace to remove html entities, seconds use a regex to remove all whitespaces after the ( and finally append at the start of the string "(€"

FATAL ERROR get_label() woocommerce shipping

Im having an issue with woocommerce shipping, here's the error im getting on cart page.
Fatal error: Call to a member function get_label() on null in public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 327
Screenshot is also attached.
wc-cart-functions.php
function 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 );
}
get_label()
public function get_label() {
return apply_filters( 'woocommerce_shipping_rate_label', $this->label );
}
Any help will be highly appreciated.
Please use add_filter instead of apply_filters in the get_label() function. It will resolve your issue.

Presumably simple PHP if/else statement not working

I seem to be having trouble with another piece of what seems to be super basic PHP, but it just won't work for me.
My client (real estate website) needs to be able to have properties with no price to be either “price upon request” OR “auction”. Currently, leaving the price field blank only allows for one.
I tried changing the following code:
$listing_price_labels = array(
‘sold’ => __( ‘Sold’, ‘wpsight’ ),
‘rented’ => __( ‘Rented’, ‘wpsight’ ),
‘request’ => __( ‘Price on request’, ‘wpsight’ ),
‘auction’ => __( ‘Auction’, ‘wpsight’ ), ***– Added this line***
);
And where this code is found…
if( is_admin() )
$listing_price .= ‘<br />’ . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;
} elseif( $listing_price = ‘auction’ ) {
// When price field contains ‘auction’ (case sensitive)
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['auction'] . ‘</span><!– .listing-price-on-request –>’;
}
function wpsight_get_price( $post_id = '' ) {
// Get post ID from $post_id
if( empty( $post_id ) )
$post_id = get_the_ID();
// If still empty, return false
if( empty( $post_id ) )
return false;
// Set listing price labels
$listing_price_labels = array(
'sold' => __( 'Sold', 'wpsight' ),
'rented' => __( 'Rented', 'wpsight' ),
'request' => __( 'Price on request', 'wpsight' ),
'auction' => __( 'Auction', 'wpsight' ),
);
$listing_price_labels = apply_filters( 'wpsight_get_price_labels', $listing_price_labels );
// Get listing price
$listing_price = wpsight_get_price_value();
// Get custom fields
$custom_fields = get_post_custom( $post_id );
$listing_status = isset( $custom_fields['_price_status'][0] ) ? $custom_fields['_price_status'][0] : false;
$listing_availability = isset( $custom_fields['_price_sold_rented'][0] ) ? $custom_fields['_price_sold_rented'][0] : false;
// Create price output
if( ! empty( $listing_availability ) ) {
// When listing is not available
$sold_rented = ( $listing_status == 'sale' ) ? $listing_price_labels['sold'] : $listing_price_labels['rented'];
// Display sold/rented bold red in admin
$style = is_admin() ? ' style="color:red;font-weight:bold"' : false;
$listing_price = '<span class="listing-price-sold-rented"' . $style . '>' . $sold_rented . '</span><!-- .listing-price-sold-rented -->';
if( is_admin() )
$listing_price .= '<br />' . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['request'] . '</span><!-- .listing-price-on-request -->';
} elseif( $listing_price == "auction" ) {
// When price field contains 'auction' (case sensitive)
$listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['auction'] . '</span><!-- .listing-price-on-request -->';
}
return apply_filters( 'wpsight_listing_price', $listing_price );
}
I’m sure my syntax must just be wrong, because with that code in place it makes any property with anything at all written into the price field display “auction”.
Can anyone see what I've done wrong?
try:
if( is_admin() ){
$listing_price .= ‘<br />’ . wpsight_get_price_value();
} elseif( empty( $listing_price ) ) {
// When no price available Price on request
$listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;
}
btw its not recommended to use if( is_admin() ) since its only applicable on 1 line.

Categories