I am trying to build a custom page where user fills his product criteria by clicking on/off switch buttons and then when user clicks a “Submit” button, the desired product will be visible using a custom woocommerce shortcode.
My jquery is collecting switch button values in order to send the desired product id to server side conditionally. I have a separate php function called test.php to build my custom shortcode.
Ajax call seems to work, when button is clicked, console returns the desired value from jquery.
However, if I try wc_get_product() console returns 500 error. Tried to load WordPress by using something like require( '/wp-content/wp-load.php' );
or assign my function inside functions.php without luck.
I think I miss something vital, any ideas would be highly appreciated.
Here is my query function:
function test_text_javascript() {
?>
<script type="text/javascript">
(function($) {
$("#Lease-Test-Button").click(function() {
// bla bla code
var SuperID = '3197';
$.ajax({
url: '/wp-content/themes/vantage-child/test.php',
method: 'POST',
data: {
SuperID: SuperID,
},
success: function(response) {
console.log("Success");
}
});
});
})(jQuery);
</script>
<?php
}
add_action('wp_footer', 'test_text_javascript');
and this is my test.php
<?php
if(isset($_POST['SuperID'])) {
$SuperID = $_POST['SuperID'];
echo $SuperID;
$product = wc_get_product($SuperID);
if ($product) {
$product_name = $product->get_name();
$product_price = $product->get_price();
echo "<div>Product Name: $product_name</div>";
echo "<div>Product Price: $product_price</div>";
} else {
echo "Product not found.";
}
} else {
echo "Error: SuperID not received";
}
?>