最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - 'false' returns on all calls for a product

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked May 3, 2019 at 15:48 johgehljohgehl 212 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论