2

I am creating a new shortcode in which I want to show some details about a product in a review-like page.

If I got it right - I can only use wc_get_product if the current page is using the WooCommerce plugin. Since it's a post page it does not includes the WooCommerce plugin and hence I can't use the wc_get_product.

(Please currect me if I'm wrong - I'm here to learn :D )

Is there a way I can get a product details by it's id on a page?

Thanks!

EDIT:

I am using this code as a shortcode with an attribute:

<?php

// Add custom Theme Functions here
// 

function gear_guide_product_view( $atts ) {
    $a = shortcode_atts( array(
        'id' => '0'
    ), $atts );
    echo var_dump(wc_get_product($a["id"]));
    wp_register_script( 'load_product_info' , '/wp-content/themes/theme-child/js/test.js' , array( 'jquery' ) );
    wp_enqueue_script( 'load_product_info' );
    wp_localize_script( 'load_product_info' , 'ids' , $atts);
}

add_action( 'woocommerce_loaded', 'my_function_with_wc_functions' );

add_shortcode( 'fsg' , 'gear_guide_product_view' );

This calls the js script:

function load_product_info() {
    jQuery.ajax({
        type: 'POST',
        url: '/wp-content/themes/theme-child/js/controllers/get_product_info.php',
        data: ids,
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function () {
            console.log("Failed to get data!");
        }
    });
}
load_product_info();

And that ajax function calls the following controller:

<?php
    if (isset($_POST["id"])) {
        $product_id = $_POST["id"];
        $product = wc_get_product($product_id);
        echo json_encode($product);
    } else {
        echo "Nothing to return!";
    }
?>

1 Answer 1

3

If I got it right - I can only use wc_get_product if the current page is using the WooCommerce plugin. Since it's a post page it does not includes the WooCommerce plugin and hence I can't use the wc_get_product.

(Please currect me if I'm wrong - I'm here to learn :D )

You are wrong :D.

There's a fundamental misunderstanding about how plugins work here. Pages don't 'include' plugins. Plugins are installed at the site level, so what's important is whether or not the plugin is installed on the site.

So there's no reason you couldn't use wc_get_product() wherever you wanted, as long as WooCommerce is installed. You can pass it an ID to wc_get_product() to get the product details on any page, like this:

$product = wc_get_product( 12 ); // Assuming the ID is 12.
echo $product->get_sku();

EDIT: Based on the edit to the question, the problem becomes obvious: The problem is that your get_product_info.php file is just a PHP file sitting on its own. It's not even loading WordPress, let alone WooCommerce. Please refer to the documentation for how to do AJAX in WordPress.

7
  • But when I am using 'wc_get_product' I am getting an undefined function error..
    – Omer
    Commented Jul 31, 2018 at 17:38
  • Where are you trying to use it? Commented Jul 31, 2018 at 17:40
  • I'll edit the question, will take me a few minutes
    – Omer
    Commented Jul 31, 2018 at 17:41
  • Edited the question with the code
    – Omer
    Commented Jul 31, 2018 at 17:44
  • Well the problem is that your get_product_info.php is just a PHP file sitting on its own. You're not even loading WordPress, let alone WooCommerce. Please refer to the documentation for how to do AJAX in WordPress. Commented Jul 31, 2018 at 17:48

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.