I am using this code to enable ajax add to cart on single product pages. I also have enables ajax in WooCommerce settings in Products tab:
Enable AJAX add to cart buttons on archives
This works well when I am on simple products.
add_action('woocommerce_before_single_product', 'wpv_remove_woocommerce_template_single_add_to_cart');
add_action('woocommerce_before_single_product', 'wpv_add_woocommerce_template_loop_add_to_cart');
function wpv_remove_woocommerce_template_single_add_to_cart(){
global $product;
if ($product->is_type('simple')) {
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
}
}
function wpv_add_woocommerce_template_loop_add_to_cart(){
global $product;
if ($product->is_type('simple')) {
add_action('woocommerce_single_product_summary', 'woocommerce_template_loop_add_to_cart', 30);
}
}
However when I try to use add to cart on variable product I get 400 (Bad Request) ajax error. I cant really see in network panel what to look as reason.
I use default WordPress theme 2022, no other plugins. This only happens when I add this code above, it works without.
Related
I have inserted post meta information in the footer for archive product using woocommerce_after_shop_loop_item and wp_footer
woocommerce_after_shop_loop_item To collect the product information
wp_footer To insert information in the footer
add_action('woocommerce_after_shop_loop_item', function() {
$data = get_post_meta(get_the_ID(), 'something')
add_action('wp_footer', function() {
// generate a template using product $data and insert in the footer
})
});
But the problem is when products are loading using Ajax (load more, ajax pagination, etc) the wp_footer does not work. I can identify whether is an ajax request using the DOING_AJAX constant but the problem is when I am in PHP environment how can I insert something in the footer without page reloading?
Check the screenshot.
I am adding coupons in woocommerce programmatically, and is working quite fine via ajax from another page, not the checkout page. However, everytime I go to the checkout page, it says, "Coupon has been applied", but I already applied the coupon from a different page. How do I disable this message from showing when going to the checkout page, after I have performed my ajax for applying a coupon dynamically? Is there some sort of setting or function to call to disable this message from showing on the cart page when a coupon has already been applied from an ajax function?
Here's my php for applying the coupon via ajax from a separate page (not the checkout page):
if (!WC()->cart->add_discount( sanitize_text_field( $coupon_code )))
{
$notices = wc_get_notices();
if (!empty($notices) && isset($notices['error'])) {
$last_fail = end($notices['error']);
echo $last_fail;
}
die();
}
else
{
$notices = wc_get_notices();
// Get last element of array only!
if (!empty($notices) && isset($notices['success'])) {
$last_success = end($notices['success']);
echo $last_success;
}
die();
}
And this works fine. As you can see, I grab errors and have them returned within the response of the ajax call, and output errors that way. I also output something for success, which is fine. The problem I'm having is that after this function executes via ajax, and I browse to the checkout page to see my product and coupon code that was applied, it puts notice at the top of the page, saying "Coupon applied Successfully." and I don't want this notice to appear, but I don't want to get rid of all notices, just this one, if the coupon was not applied on the checkout page, there is no need for this wc_notice to appear when browsing to the checkout page.
How to tell woocommerce not to apply this coupon notice to the checkout page when I browse to the checkout page after adding coupons manually from another page (via ajax)?
You can use the "woocommerce_coupon_message" filter to hide the success message for the coupon. As you want to remove it for checkout page only, just check if current page is checkout page or not.
You can use the following code :
function remove_msg_filter($msg, $msg_code, $this){
if(is_checkout()){
return "";
}
return $msg;
}
add_filter('woocommerce_coupon_message','remove_msg_filter',10,3);
Note : If you also want to change message for error then use "woocommerce_coupon_error" filter.
The way to fix this is to add wc_clear_notices(); before die(); in the function call, so as to remove all notices that the function add_discount adds to the array that gets returned from wc_get_notices() function call.
if (!WC()->cart->add_discount( sanitize_text_field( $coupon_code )))
{
$notices = wc_get_notices();
if (!empty($notices) && isset($notices['error'])) {
$last_fail = end($notices['error']);
echo $last_fail;
}
wc_clear_notices();
die();
}
else
{
$notices = wc_get_notices();
// Get last element of array only!
if (!empty($notices) && isset($notices['success'])) {
$last_success = end($notices['success']);
echo $last_success;
}
wc_clear_notices();
die();
}
This way may be a bit of a lazy way of approaching this. But if you inspect element of the page, find the message, target it and add 'display:none;' in the CSS.
That should do the trick. Try and make it specific for that page only. A unique class is usually page-id found at the top of the page in inspect element
Please Suggest Tutorial or any example How to change exciting controller using local folder and local code. I want change in cart controller. I do when click on Add To Cart then it redirects to checkout page. Please Tell What I change and How to change. I am making cart controller override. It's working, but what can change in this controller to direct redirect to check out page.
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class My_Module_Checkout_CartController extends Mage_Checkout_CartController
{
public function addAction()
{
$returnUrl = Mage::getUrl('checkout/onepage');
$this->getRequest()->setParam('return_url', $returnUrl);
parent::addAction();
}
}
I'm building a commercial website, with a shopping cart.
On most pages (i.e - product page, category page), I want to display the cart contents on a sidebar, which will get updated via AJAX when an item is added to the cart.
On the "display cart" page I want to show a full version of the contents.
Obviously, it seems logical to use the same model and functions to get and/or update the cart, but send the data to a different view (sidebar or full cart), depending on the caller page.
The question is, in the cart model, how can I detect where did the request come from.
I thought I'd check if the request came via AJAX, like so:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') // i.e - the request came as AJAX
{
$this->load->view('cart_sidebar_view', $data);
}else{ /* not ajax */
$data['main_content'] = 'cart_view';
$this->load->view('includes/template', $data);
}
But that is not good enough, because I want to use AJAX on the "display cart" page as well, to allow updating the cart from there.
So, is there a way to detect, in the cart model, where did the request come from? Or will I have to send that info in a hidden form field with every "add to cart" or "remove" button?
There is a simple way. when you are sending request from the display cart page send an additional variable. Than in the controller check for this variable if variable is coming call a logic if variable is not coming do something else.
if($this->input->is_ajax_request())
{
$this->load->view('cart_sidebar_view', $data);
}else{
if($this->input->post('another_variable')){
// do something else
}else{
$data['main_content'] = 'cart_view';
$this->load->view('includes/template', $data);
}
}
I have SCP (simple configurable product) installed and for every simple product that has no custom options, the price doesn’t show up in the product page. As soon as I add an option, the price shows up.
There is no price block or template called on the product page when no options is selected.
I saw that the extension is extending Mage_Catalog_Block_Product_Price block , but I can't find where this block should be called.
Haven't found any solution yet , so I made this jquery workaround that works
Put this at the end of template/catalog/product/view.phtml
jQuery(document).ready(function($) {
if($('.price-box').length==0){
var price = '<div class="price-box"><span class="regular-price"><span class="price">$<?php echo number_format($_product->getPrice(),2); ?></span></span></div>';
$('.product-options-bottom').before(price);
}
});
Of course, you need to have jquery.