/**
* Add to cart action
*
* Checks for a valid request, does validation (via hooks) and then redirects if valid
**/
function woocommerce_add_to_cart_action( $url = false ) {
global $woocommerce;
if (empty($_REQUEST['add-to-cart']) || !$woocommerce->verify_nonce('add_to_cart', '_REQUEST')) return;
$added_to_cart = false;
switch ($_REQUEST['add-to-cart']) {
// Variable Products
case 'variation' :
// Only allow integer variation ID - if its not set, redirect to the product page
if (empty($_REQUEST['variation_id']) || !is_numeric($_REQUEST['variation_id']) || $_REQUEST['variation_id']<1) {
$woocommerce->add_error( __('Please choose product options…', 'woocommerce') );
wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
exit;
}
// Get product ID to add and quantity
$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['product_id']);
$variation_id = (int) $_REQUEST['variation_id'];
$quantity = (isset($_REQUEST['quantity'])) ? (int) $_REQUEST['quantity'] : 1;
$attributes = (array) maybe_unserialize(get_post_meta($product_id, '_product_attributes', true));
$variations = array();
$all_variations_set = true;
// Verify all attributes for the variable product were set
foreach ($attributes as $attribute) {
if ( !$attribute['is_variation'] ) continue;
$taxonomy = 'attribute_' . sanitize_title($attribute['name']);
if (!empty($_REQUEST[$taxonomy])) {
// Get value from post data
$value = esc_attr(stripslashes($_REQUEST[$taxonomy]));
// Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
$variations[esc_attr($attribute['name'])] = $value;
} else {
$all_variations_set = false;
}
}
if ($all_variations_set) {
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
if ($passed_validation) {
if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
woocommerce_add_to_cart_message();
$added_to_cart = true;
}
}
} else {
$woocommerce->add_error( __('Please choose product options…', 'woocommerce') );
wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
exit;
}
break;
// Grouped Products
case 'group' :
if (isset($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
$quantity_set = false;
foreach ($_REQUEST['quantity'] as $item => $quantity) {
if ($quantity<1) continue;
$quantity_set = true;
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
if ($passed_validation) {
if ($woocommerce->cart->add_to_cart($item, $quantity)) {
woocommerce_add_to_cart_message();
$added_to_cart = true;
}
}
}
if (!$added_to_cart && !$quantity_set) {
$woocommerce->add_error( __('Please choose a quantity…', 'woocommerce') );
wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
exit;
}
} elseif ($_REQUEST['product_id']) {
/* Link on product archives */
$woocommerce->add_error( __('Please choose a product…', 'woocommerce') );
wp_redirect( get_permalink( $_REQUEST['product_id'] ) );
exit;
}
break;
// Simple Products - add-to-cart contains product ID
default :
// Only allow integers
if (!is_numeric($_REQUEST['add-to-cart'])) break;
// Get product ID to add and quantity
$product_id = (int) $_REQUEST['add-to-cart'];
$quantity = (isset($_REQUEST['quantity'])) ? (int) $_REQUEST['quantity'] : 1;
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
if ($passed_validation) {
// Add the product to the cart
if ($woocommerce->cart->add_to_cart($_REQUEST['add-to-cart'], $quantity)) {
woocommerce_add_to_cart_message();
$added_to_cart = true;
}
}
break;
}
// If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
if ($added_to_cart) {
$url = apply_filters('add_to_cart_redirect', $url);
// If has custom URL redirect there
if ( $url ) {
wp_safe_redirect( $url );
exit;
}
// Redirect to cart option
elseif (get_option('woocommerce_cart_redirect_after_add')=='yes' && $woocommerce->error_count() == 0) {
wp_safe_redirect( $woocommerce->cart->get_cart_url() );
exit;
}
}
}