Last active
August 19, 2016 14:49
-
-
Save BenBroide/369101ca6188f1e7558b933ebcce91b0 to your computer and use it in GitHub Desktop.
WooCommerce - Link to add a qty of product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function quick_cart_5_horus(){ | |
| global $woocommerce; | |
| $product_id = sanitize_text_field( $_POST['product_id'] ); | |
| // Be sure cart is empty from previous add to cart | |
| WC()->cart->empty_cart(); | |
| // Addding the product by product ID | |
| WC()->cart->add_to_cart( $product_id ); | |
| // Getting the cart item key | |
| foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| } | |
| // Setting product qty by $cart_item_key | |
| WC()->cart->set_quantity( $cart_item_key, 5 ); | |
| // Cart URL to redirect user from front end | |
| $checkout_url = $woocommerce->cart->get_checkout_url(); | |
| // Echo URL to front | |
| echo $checkout_url; | |
| wp_die(); | |
| } | |
| // bind ajax call to callback function | |
| add_action( 'wp_ajax_quick_cart_5_horus', 'quick_cart_5_horus' ); | |
| // Print ajax js | |
| add_action("wp_footer","print_set_cart_qty_js",100); | |
| function print_set_cart_qty_js(){ ?> | |
| <script> | |
| (function ($) { | |
| $('.quick-cart').click(function(){ | |
| //var product_id= $(this).data('quick_id'); | |
| var loading_type = $(this).data('loading_type'); | |
| $("#"+loading_type).show("slow"); | |
| $.ajax({ | |
| type: "POST", | |
| url: "<?php echo get_site_url() ?>/wp-admin/admin-ajax.php", | |
| data: { | |
| 'action': 'quick_cart_5_horus', | |
| 'product_id': 26 | |
| }, | |
| success: function (response) { | |
| window.location.assign(response) | |
| } | |
| }); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <?php | |
| } | |
| // This article has a direct URL from Woo's core to add variations, didn't test it it http://www.remicorson.com/add-woocommerce-variable-product-with-variations-to-cart/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment