2

I think there is quite an easy solution:

In WooCommerce I want to apply my delivery time filter to a single product, but only if it is available/in stock. I am getting an error when it comes to any kind of checking the products stock.

function filter_woocommerce_germanized_delivery_time_html( $str_replace, $html ) { 
    
    global $product;
if( $product->is_in_stock() ) {

        echo '<p class="wc-gzd-additional-info delivery-time-info">';
        echo $str_replace;
        echo '</p>'; 
    echo '<p class="wc-gzd-additional-info"><a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i class="fas fa-shipping-fast"></i> EU-Lieferzeiten</a></p></span>';
    
}
    
}   
         
// add the filter 
add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );

I also tried:

if($product->get_stock_quantity()>0) 

But similar errors like:

"Uncaught Error: Call to a member function is_in_stock() on null.."

Thanks for the help!

Regards, Felix

Ruvee
  • 8,611
  • 4
  • 18
  • 44
F Hughes
  • 35
  • 1
  • 8

1 Answers1

7

The error is telling you that you used your function on a null value which means it could not find the $product variable.

Not sure where this woocommerce_germanized_delivery_time_html filter hook comes from and where you're using global $product, but you could use the following snippet to get the product.

global $post;
$product = wc_get_product($post->ID);

Now your entire code would be something like this:

add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );

function filter_woocommerce_germanized_delivery_time_html($str_replace, $html)
{

    global $post;

    $product = wc_get_product($post->ID);

    if ($product->is_in_stock()) {

        echo '<p class="wc-gzd-additional-info delivery-time-info">';
        echo $str_replace;
        echo '</p>';
        echo '<p class="wc-gzd-additional-info">';
        echo '<a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i class="fas fa-shipping-fast"></i> EU-Lieferzeiten</a>';
        echo '</p>';
    } else{
       return $str_replace;
    }

}

Or you could get the stock quantity like this:

add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );

function filter_woocommerce_germanized_delivery_time_html($str_replace, $html)
{

    global $post;

    $stock_quantity = get_post_meta($post->ID, '_stock', true);

    if ($stock_quantity > 0) {

        echo '<p class="wc-gzd-additional-info delivery-time-info">';
        echo $str_replace;
        echo '</p>';
        echo '<p class="wc-gzd-additional-info">';
        echo '<a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i class="fas fa-shipping-fast"></i> EU-Lieferzeiten</a>';
        echo '</p>';
    } else {
       return $str_replace;
    }

}

Let me know if you could get it to work!

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • Thank you for your help so far Ruvee, your first snippet got me an error as well, the second one kind of worked after I changend `if ($stock_quantity)` to `if ($stock_quantity>0)`, but weirdly the delivery time dissapears from the products in the shopping cart view then. Probably I am making it too complicated anyway. I got the hook from here http://hookr.io/filters/woocommerce_germanized_delivery_time_html/ and actually all I want to do is ad some html after the hook. Thanks again! – F Hughes Oct 16 '21 at 10:39
  • Hm that broke it even more, I am getting some weird outputs in the frontend. – F Hughes Oct 16 '21 at 12:50
  • 1
    That's ok, it was to be expected. This is a good example of including as much info and context as possible when you ask a question. I can not debug what I can not see. When you say *"kind of work"* is not clear what happened when you say *"broke it even more"* is not very clear either and it's too way broad. Can you at least provide the error messages? Or some screenshots so that i can see what's happening? – Ruvee Oct 16 '21 at 12:55
  • Ok sure, I just wasn't expecting that you still want to put work in it, thanks a lot! So with your second snippet I get this output: https://i.ibb.co/BKfqF13/Bildschirmfoto-2021-10-16-um-15-44-43.png and in card: https://i.ibb.co/CV0fmvf/Bildschirmfoto-2021-10-16-um-15-44-54.png but it's supposed to be like this: https://i.ibb.co/PjVJQvt/Bildschirmfoto-2021-10-16-um-15-48-35.png (Everything worked in your previous version, but in Card display of the shipping was gone) By EU-right, the shipping time has to be displayed, including my additional link to EU-Shipping times.. – F Hughes Oct 16 '21 at 13:50
  • Thank you, that was the best version so far, it works besides in card view it just tells the regular shipping time without my additional EU-shipping-link (which makes sense because of the `else { return $str_replace;}` part. I will try to figure the rest out by myself and maybe come back to you. Really appreciate your help. – F Hughes Oct 16 '21 at 15:40
  • I think it needs something like `if ($stock_quantity > 0 or ++product_is_in_card++)`to make it work in card view as well. (I made that `++product_is_in_card++` up, I'll find out about it) – F Hughes Oct 16 '21 at 16:00
  • It's the standartized display of shipping time. https://i.ibb.co/1G5sDbd/Bildschirmfoto-2021-10-16-um-18-26-26.png It's ridiculous anyway that WooCommerce can't differ between countries and shipping times.. return $html; gives out Lieferzeit: {delivery_time} in frontend. – F Hughes Oct 16 '21 at 16:28