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!";
}
?>
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!";
}
?>
Share
Improve this question
edited Jul 31, 2018 at 17:44
Omer
asked Jul 31, 2018 at 17:02
OmerOmer
1491 gold badge4 silver badges14 bronze badges
1 Answer
Reset to default 3If 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 thewc_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.