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.
Related
I'm preparing a shopping site with woocommerce. As in the sample link, I show the product stock information in the drop down menu. this is the code I use:
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$stock_status = get_variation_stock_status( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ).$stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
$html .= '</select>';
return $html;
}
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'] );
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme') : ' - ' . $stock_qty . ' ' . __(pll__('adet Stokta'), 'mytheme-');
}
If the stock quantity is over 20, I don't want to show this information. how can I do that?
Just add a check to the get_variation_stock_status function:
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() > 20 ) {
return '';
}
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - (Out Of Stock)' : ' - ' . $stock_qty . ' In Stock';
}
The entire code (from the link in your comment) has been tested and works. Add it to your active theme's functions.php.
I'm getting this error:
Uncaught Error: Call to a member function get_price() on null in...
when category/shop/archive page is empty or has not products:
function display_loop_item_stock_notice() {
global $product;
// For active product price: Free shipping || NO Free shipping
$text = $product->get_price() >= 500 ? '<span>Free shipping</span>' : '<span>Free shipping from 500 €</span>';
// In stock
if ( $product->is_in_stock() ) {
$text = '<span>In stock</span>' . ' <span class="free-shipping">' . $text . '</span>';
}
// Out of stock
else {
$text = '<span><strong>' . get_field( 'date_of_availability' ) . '</strong></span>' . '<span class="free-shipping">' . $text . '</span>';
echo '<div class="remaining text-center">' . $text . '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'display_loop_item_stock_notice', 90 );
You can check if $product is null.
...
global $product;
if (is_null($product)) {
return;
}
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.
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 "(€"
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;
}