Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active September 27, 2017 12:38
Show Gist options
  • Save RadGH/209ffe1711ffceb2e69eeaf9252d88ff to your computer and use it in GitHub Desktop.
Save RadGH/209ffe1711ffceb2e69eeaf9252d88ff to your computer and use it in GitHub Desktop.
WooCommerce URL to add to cart and redirect to cart or checkout with quantity, limit, and option to remove other products.
<?php
/*
* Returns a URL that upon visiting, will add the product to the cart and do some other things as described below.
* $qty = How many items to add to the cart
* $limit = If true, it will prevent you from having more than $qty in your cart.
* $remove_others = If true, any other products will be removed from the cart.
* $redirect_to = If set to "cart" or "checkout", will redirect after adding the item. Does not work with a URL (to avoid XSS).
*
* @param $product_id
* @param int $qty
* @param bool $limit
* @param bool $remove_others
* @param string $redirect_to
*
* @return string
*/
function woo_add_to_cart_url( $product_id, $qty = 1, $limit = false, $remove_others = false, $redirect_to = '' ) {
$args = array(
'acrc' => wp_create_nonce( 'add_cart_redirect_checkout' ),
'acrc_id' => (int) $product_id,
'acrc_qty' => (int) $qty,
'acrc_limit' => $limit ? 1 : 0,
'acrc_remove_others' => $remove_others ? 1 : 0,
'acrc_redirect_to' => strtolower( (string) $redirect_to ),
);
return add_query_arg( $args, get_permalink() );
}
/**
* Hook that implements ?add_to_cart_redirect property. This adds a product to the cart, and redirects to checkout.
*/
function woo_add_to_cart_redirect_checkout() {
$nonce = isset($_REQUEST['acrc']) ? stripslashes($_REQUEST['acrc']) : false;
if ( !$nonce || !wp_verify_nonce( $nonce, 'add_cart_redirect_checkout' ) ) return;
$id = isset($_REQUEST['acrc_id']) ? (int) $_REQUEST['acrc_id'] : false;
$qty = isset($_REQUEST['acrc_qty']) ? max( 1, (int) $_REQUEST['acrc_qty'] ) : false;
$limit = isset($_REQUEST['acrc_limit']) ? (int) $_REQUEST['acrc_limit'] : false;
$remove_others = isset($_REQUEST['acrc_remove_others']) ? (int) $_REQUEST['acrc_remove_others'] : false;
$redirect_to = isset($_REQUEST['acrc_redirect_to']) ? stripslashes($_REQUEST['acrc_redirect_to']) : false;
$skip_add_to_cart = false;
// Remove any other product from the cart
if ( $remove_others ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if ( $values['product_id'] !== $id ) WC()->cart->remove_cart_item( $cart_item_key );
}
}
// Set the quantity if it is already in the cart.
if ( $limit ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if ( $values['product_id'] == $id ) {
WC()->cart->set_quantity( $cart_item_key, $qty );
$skip_add_to_cart = true;
}
}
}
// Add to cart, unless we set quantity above instead
if ( !$skip_add_to_cart ) {
WC()->cart->add_to_cart( $id, $qty );
}
// Redirect to clear post/get query vars.
if ( strtolower($redirect_to) == 'cart' ) $url = wc_get_cart_url();
else if ( strtolower($redirect_to) == 'checkout' ) $url = wc_get_checkout_url();
else $url = remove_query_arg( array( 'acrc', 'acrc_id', 'acrc_qty', 'acrc_limit', 'acrc_remove_others', 'acrc_redirect_to' ) );
wp_redirect( $url );
exit;
}
add_action( 'template_redirect', 'woo_add_to_cart_redirect_checkout', 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment