• Resolved abisson

    (@abisson)


    Hi!

    I am trying to write a PHP Snippet with the following code in it:

    global $product;
    $sku = $product->get_sku();

    Now, it seems I can’t activate that and it says to me:

    Snippet not activated, the following error was encountered: Call to a member function get_sku() on null

    Now, I got it working and activated at some point… and then it stopped. Ideas?

    I want to add this snippet in a UX Block that I then call in UX Builder… other workarounds?

Viewing 1 replies (of 1 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @abisson,

    From the code you mentioned & the error it seems that you need to add a check to make sure the $product variable is actually a WC product.

    I don’t know more about the context of the snippet you are trying to use but specifically for the code you have posted this should fix the error when the $product is not yet set:

    
    global $product;
    if ( is_a( 'WC_Product', $product ) ) {
    	$sku = $product->get_sku();
    }
    

    You should probably also add a check after that to prevent further execution if $sku is not set or is empty.

Viewing 1 replies (of 1 total)

The topic ‘Call WooCommerce Product’ is closed to new replies.