2

I am getting bool(false) as a return making calls for a product, using the product ID. Depending on which code I use, I have also had NULL and 0 returns but never any product data.

I have tried...

$_pf = new WC_Product_Factory();
$product = $_pf->get_product($id);
$_pf = new WC_Product_Factory();
$product = $_pf->wc_get_product($id);
$product = new WC_Product('1651');

I have even tried calling all the way back to WC()->.

I have tried the above code in functions.php. I am also having issues with the child div file not over riding the parent div file, so I have placed this code in the parent div file. That issue is for another time, I just need to get a simple call to the product to go through.

<script>
    function getAJAX() {
        jQuery.ajax({
            type: "post",
            url: "/wp-admin/admin-ajax.php",
            data: "action=get_Title&id=1651",
            success: function (data) {
                console.log(data);
            }
        });
    } // End function for getAJAX.
    var button = document.getElementById("ajax_button");
    button.addEventListener("click", getAJAX);

</script>

This is the AJAX call from the template, content-single-product.php. I do not think there is any issues here. I am getting data returned.

add_action("wp_ajax_get_Title", "get_Title");
add_action("wp_ajax_nopriv_get_Title", "get_Title");

function get_Title() {
    $id = $_REQUEST['id'];
    $_pf = new WC_Product_Factory();
    $product = $_pf->get_product($id);

    echo $product;

    die();
}

Above is some code I have been using to test. Last thing I tried and still getting bool(false).

Yes, that is a valid product ID. I was just testing out connecting to a product and pulling out the title, or something, before I start coding for all the pieces of data I need to pull from the product.

This is a multi-site platform, and the AJAX calls and calls to woocommerce products are working on other pages within the very site I am working on, but will not pull anything on the project I am working on.

I am very new to woocommerce and WP, so any help here is greatly appreciated.

1
  • I am unable to reply to my previous comment due to just signing up, but setting the variable to a int returns nothing, an actual blank response. Thank you for the suggestion Stephan but it did not fix the issue at this time. Back to the drawing board, any other suggestions? Thank you again.
    – johgehl
    Commented May 6, 2019 at 12:58

1 Answer 1

2

I don't have an extra WC instance around to try on but I do see one possible issue.

$_REQUEST['id'] is a string (always). WC_Product_Factory->get_product() may very well be expecting an int. PHP is not, and WP definitely is not, always clever about transparent recasting.

Try this to see if it helps:

function get_Title() {
    $id = $_REQUEST['id'];
    if (is_string($id) && is_numeric($id))
        $id = intval($id);
    $_pf = new WC_Product_Factory();
    $product = $_pf->get_product($id);

    echo $product;

    die();
}

Unable to test I can't diagnose if that's really what's going on but it's a quick change so it's worth trying.

1
  • Thank you for your reply Stephan. I won't go into it, but I also cannot check against the code base right now, but it is the first thing I will try and reply when I have the chance.
    – johgehl
    Commented May 3, 2019 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.