WordPress.org

Forums

WooCommerce - excelling eCommerce
woocommerce - product_type null (4 posts)

  1. jpemond
    Member
    Posted 1 year ago #

    Hi every One,

    I have a bug to retrieve the product_type when I use:

    $product = new WC_Product("619"); // ID EXISTS
    echo $product->product_type; // return: NULL
    //I try to var_dump $product
    var_dump($product); // return array with post_title, exerpt, id with good information in woocommerce  and the product_type NULL

    I see in the WC_Product class that no function fill the $product_type
    (ref: http://docs.woothemes.com/wc-apidocs/class-WC_Product.html)

    Also,
    I have already try:

    var_dump($product->is_type('simple')); // return: bool(false); this is the good type

    Anyone can be help me ?

    Thank you

    https://wordpress.org/plugins/woocommerce/

  2. lorro
    Member
    Posted 1 year ago #

    $product = new WC_Product("619"); // ID EXISTS

    The "new" keyword is the constructor which makes a new instance of the class, not retrieve an existing instance. Try it without the "new".

  3. jpemond
    Member
    Posted 1 year ago #

    No.

    'new' need to be there. I initialize an Object Product.

    The product is initialize with the id and get all information that this product.

    It's why I get the good informations when I use the Var_dump.

    And, in the previous version of WooCommerce, this code works perfectly.

    This is not a "new" keyword issues.

    Sorry :-/

  4. lorro
    Member
    Posted 1 year ago #

    But it doesn't work, so try this:

    $product = get_product("619");
    echo $product->product_type;
    // prints "simple" (when tried with one of my products)

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic