Last active
May 19, 2017 05:16
-
-
Save gschoppe/46f37a75841738ab86ef3039fc8010e6 to your computer and use it in GitHub Desktop.
This file contains 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 | |
// this handler adds our custom javascript to fire AJAX requests when we click one of these buttons | |
add_action('wp_footer', 'my_custom_wc_button_script'); | |
function my_custom_wc_button_script() { | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
var ajaxurl = "<?php echo esc_attr( admin_url( 'admin-ajax.php' ) ); ?>"; // get the url we use to submit AJAX | |
$( document.body).on('click', '.my-custom-add-to-cart-button', function(e) { // i made this delegated, rather than a traditional click handler, so you could add additional buttons via AJAx, if you ever wanted to | |
e.preventDefault(); // stop the click from doing normal button things | |
var $this = $(this); | |
if( $this.is(':disabled') ) { // don't do anything if the button is disabled (item is in cart)... this could be changed to toggle whether you are in cart or not | |
return; | |
} | |
var id = $(this).data("product-id"); // get the product ID from the button | |
var data = { // prep our AJAX request | |
action : 'my_custom_add_to_cart', // This is the AJAX function we define in PHP below | |
product_id : id | |
}; | |
$.post(ajaxurl, data, function(response) { | |
if( response.success ) { | |
// we added to cart so change the message, and make sure no one can add again | |
$this.text("added to cart"); | |
$this.attr('disabled', 'disabled'); | |
// make woocommerce update cart counts in the menu widget | |
$( document.body ).trigger( 'wc_fragment_refresh' ); | |
} | |
}, 'json'); | |
}) | |
}); | |
</script> | |
<?php | |
} | |
// here's where we define the ajax processing functions.. the part after wp_ajax, and wp_ajax_nopriv has to match the action we used in our javascript | |
add_action('wp_ajax_my_custom_add_to_cart', "my_custom_add_to_cart"); | |
add_action('wp_ajax_nopriv_my_custom_add_to_cart', "my_custom_add_to_cart"); | |
function my_custom_add_to_cart() { | |
// just setting up data to return... we override this as we go | |
$retval = array( | |
'success' => false, | |
'message' => "" | |
); | |
if( !function_exists( "WC" ) ) { | |
// check if woocommerce is installed | |
$retval['message'] = "woocommerce not installed"; | |
} elseif( empty( $_POST['product_id'] ) ) { | |
// check product id was sent | |
$retval['message'] = "no product id provided"; | |
} else { | |
$product_id = $_POST['product_id']; | |
// my_custom_cart_contains is defined below.. checks if the cart contains a product | |
if( my_custom_cart_contains( $product_id ) ) { | |
// make sure we can't add a product twice | |
$retval['message'] = "product already in cart"; | |
} else { | |
// we are good to add to cart | |
$cart = WC()->cart; | |
//add_to_cart returns an id, but we only need to make sure it doesn't return false... hence casting to a boolean | |
$retval['success'] = (bool) $cart->add_to_cart( $product_id ); | |
if( !$retval['success'] ) { | |
// the add to cart failed | |
$retval['message'] = "product could not be added to cart"; | |
} else { | |
// the add succeeded | |
$retval['message'] = "product added to cart"; | |
} | |
} | |
} | |
// we send the data back to javascript by outputting it as JSON, and exiting. | |
echo json_encode( $retval ); | |
wp_die(); | |
} | |
// check if cart contains product | |
function my_custom_cart_contains( $product_id ) { | |
$cart = WC()->cart; | |
$cart_items = $cart->get_cart(); | |
if( $cart_items ) { | |
foreach( $cart_items as $item ) { | |
$product = $item['data']; | |
if( $product_id == $product->id ) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment