Home › Forums › Basel support forum › Issue with Variation
Issue with Variation
- This topic has 5 replies, 2 voices, and was last updated 5 years, 4 months ago by Artem Temos.
-
AuthorPosts
-
June 24, 2019 at 8:25 am #129770
sweetieParticipantHello,
I am using the following code to add a Buy Now button on my website. The code is by LoicTheAztecHere is the code:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' ); function add_custom_addtocart_and_checkout() { global $product; $addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id(); $button_class = 'single_add_to_cart_button button alt custom-checkout-btn'; $button_text = __("Buy & Checkout", "woocommerce"); if( $product->is_type( 'simple' )) : ?> <script> jQuery(function($) { var url = '<?php echo $addtocart_url; ?>', qty = 'input.qty', button = 'a.custom-checkout-btn'; // On input/change quantity event $(qty).on('input change', function() { $(button).attr('href', url + '&quantity=' + $(this).val() ); }); }); </script> <?php elseif( $product->is_type( 'variable' ) ) : $addtocart_url = wc_get_checkout_url().'?add-to-cart='; ?> <script> jQuery(function($) { var url = '<?php echo $addtocart_url; ?>', vid = 'input[name="variation_id"]', pid = 'input[name="product_id"]', qty = 'input.qty', button = 'a.custom-checkout-btn'; // Once DOM is loaded setTimeout( function(){ if( $(vid).val() != '' ){ $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() ); } }, 300 ); // On input/change quantity event $(qty).on('input change', function() { if( $(vid).val() != '' ){ $(button).attr('href', url + $(vid).val() + '&quantity=' + $(this).val() ); } }); // On select attribute field change event $('.variations_form').on('change blur', 'table.variations select', function() { if( $(vid).val() != '' ){ $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() ); } }); }); </script> <?php endif; echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>'; }
Now, the problem is whenever I am clicking on the Buy Now button with the default variations set in the Product editor, the code is not working.
The Buy Now URL in this case becomes …/?addtocart=0&quantity=n
Any Custom JS that might do the job?
P.S: Tried with a lot of themes, the code works well with all of them.
Thanks
June 24, 2019 at 11:46 am #129822
Artem TemosKeymasterHello,
We don’t know how this code is supposed to work so can’t fix it. It is out of our theme support scope and you need to contact this code’s developers.
Kind Regards
June 24, 2019 at 11:52 am #129824
sweetieParticipantOkay So can you please help me in this:
How can I add a Buy Now button along with Add to Cart in basel?
Thanks.
June 24, 2019 at 4:26 pm #129830
Artem TemosKeymasterWe see that the code you used already did that. It uses WooCommerce’s hook
woocommerce_after_add_to_cart_button
to add the button directly after the add to cart button.June 24, 2019 at 6:14 pm #129877
sweetieParticipantYes, but this code is not working with the theme with default variations. Works only when the user select/update variations on the product page.
Could you please check it on your demo website?
Thanks!June 25, 2019 at 6:05 am #129922
Artem TemosKeymasterSorry, but as we mentioned we don’t know how this code is supposed to work so can’t fix it. It is out of our theme support scope and you need to contact this code’s developers.
-
AuthorPosts
Tagged: buy now, variations, woocommerce
- You must be logged in to create new topics. Login / Register