I am trying to pass a custom price / custom attributes to the woocommerce cart from an ordinary WordPress page using PHP and Ajax, but I can't get the job done dynamically.
I'm working with an AJAX app embedded on our dev site that essentially calculates a total based on configured product variations. I am trying to grab the total from there and submit a product to the Woocommerce cart (default price 0.00) and override the price there.
I've been working from some PHP provided here with a very similar circumstance, but I think I'm missing something: Adding a product to cart with custom info and price
I can set a static price using the 'woocommerce_before_calculate_totals' hook but, using the code outlined in the link above, I can't seem to 'pass' a custom price to the cart using an AJAX button. The POST data never reaches the PHP function. The way I'm attempting it right now is by using a button with some AJAX code to make the post, but I keep hitting walls. I think I've got the wrong idea about something here. I'm using a static price in the ajax below.
jQuery(function(){
jQuery('#newQuoteBtn').on('click', function(e){
e.preventDefault();
jQuery.ajax({
url: '/wp-content/plugins/override_price.php',
type: 'post',
data: { 'custom_options': {'action': 'newquote', 'newprice': '555.55', 'quotenum': '1234567890'}},
success: function(data, status) {
/* Product added and redirect to cart */
console.log('success');
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}); // end ajax call
});
});
I expected the data to pass to the PHP which takes care of overriding the price from there, but I can't get the variables to the PHP. The authors' comments stated to put that code in the functions.php file, but how do I post data there? There's a disconnect somewhere here and I need help illuminating the correct way to approach this problem.