function fflcommerce_add_to_cart_action($url = false) { if (empty($_REQUEST['add-to-cart']) || !fflcommerce::verify_nonce('add_to_cart')) { return false; } $options = FFLCommerce_Base::get_options(); $product_added = false; switch ($_REQUEST['add-to-cart']) { case 'variation': // ensure we have a valid quantity, product and variation id, that is numeric, without any SQL injection attempts. $product_id = isset($_REQUEST['product_id']) && is_numeric($_REQUEST['product_id']) ? (int) $_REQUEST['product_id'] : -1; if ($product_id < 0) { break; // drop out and put up message, unable to add product. } if (empty($_REQUEST['variation_id']) || !is_numeric($_REQUEST['variation_id'])) { break; // drop out and put up message, unable to add product. } $quantity = isset($_REQUEST['quantity']) && is_numeric($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1; $product_id = apply_filters('fflcommerce_product_id_add_to_cart_filter', $product_id); $variation_id = apply_filters('fflcommerce_variation_id_add_to_cart_filter', (int) $_REQUEST['variation_id']); $attributes = (array) maybe_unserialize(get_post_meta($product_id, 'product_attributes', true)); $variations = array(); $all_variations_set = true; if (get_post_meta($product_id, 'customizable', true) == 'yes') { // session personalization initially set to parent product until variation selected $custom_products = (array) fflcommerce_session::instance()->customized_products; // transfer it to the variation $custom_products[$variation_id] = $custom_products[$product_id]; unset($custom_products[$product_id]); fflcommerce_session::instance()->customized_products = $custom_products; } foreach ($attributes as $attribute) { if (!$attribute['variation']) { continue; } $attr_name = 'tax_' . sanitize_title($attribute['name']); if (!empty($_REQUEST[$attr_name])) { $variations[$attr_name] = esc_attr($_REQUEST[$attr_name]); } else { $all_variations_set = false; } } // Add to cart validation $is_valid = apply_filters('fflcommerce_add_to_cart_validation', true, $product_id, $quantity); if ($all_variations_set && $is_valid) { if (fflcommerce_cart::add_to_cart($product_id, $quantity, $variation_id, $variations)) { $product_added = true; } } break; case 'group': if (empty($_REQUEST['quantity']) || !is_array($_REQUEST['quantity'])) { break; } // do nothing foreach ($_REQUEST['quantity'] as $product_id => $quantity) { // Skip if no quantity if (!$quantity) { continue; } $quantity = (int) $quantity; // Add to cart validation $is_valid = apply_filters('fflcommerce_add_to_cart_validation', true, $product_id, $quantity); // Add to the cart if passsed validation if ($is_valid) { if (fflcommerce_cart::add_to_cart($product_id, $quantity)) { $product_added = true; } } } break; default: if (!is_numeric($_REQUEST['add-to-cart'])) { // Handle silently for now break; } // Get product ID & quantity $product_id = apply_filters('fflcommerce_product_id_add_to_cart_filter', (int) $_REQUEST['add-to-cart']); $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1; // Add to cart validation $is_valid = apply_filters('fflcommerce_add_to_cart_validation', true, $product_id, $quantity); // Add to the cart if passed validation if ($is_valid) { if (fflcommerce_cart::add_to_cart($product_id, $quantity)) { $product_added = true; } } break; } if (!$product_added) { fflcommerce::add_error(__('The Product could not be added to the cart. Please try again.', 'fflcommerce')); wp_safe_redirect(remove_query_arg(array('add-to-cart', 'quantity', 'product_id', '_n'), wp_get_referer()), 301); exit; } else { switch ($options->get('fflcommerce_redirect_add_to_cart', 'same_page')) { case 'same_page': $message = __('Product successfully added to your cart.', 'fflcommerce'); $button = __('View Cart →', 'fflcommerce'); $message = '<a href="%s" class="button">' . $button . '</a> ' . $message; fflcommerce::add_message(sprintf($message, fflcommerce_cart::get_cart_url())); break; case 'to_checkout': // Do nothing break; default: fflcommerce::add_message(__('Product successfully added to your cart.', 'fflcommerce')); break; } $url = apply_filters('add_to_cart_redirect', $url); if ($url) { wp_safe_redirect($url, 301); exit; } else { if ($options->get('fflcommerce_redirect_add_to_cart', 'same_page') == 'to_checkout' && !fflcommerce::has_errors()) { wp_safe_redirect(fflcommerce_cart::get_checkout_url(), 301); exit; } else { if ($options->get('fflcommerce_redirect_add_to_cart', 'to_cart') == 'to_cart' && !fflcommerce::has_errors()) { wp_safe_redirect(fflcommerce_cart::get_cart_url(), 301); exit; } else { if (wp_get_referer()) { wp_safe_redirect(remove_query_arg(array('add-to-cart', 'quantity', 'product_id'), wp_get_referer()), 301); exit; } else { wp_safe_redirect(home_url(), 301); exit; } } } } } }