Don't like ads? PRO users don't see any ads ;-)

Add to cart function

By: dadadovich on May 15th, 2012  |  syntax: PHP  |  size: 5.32 KB  |  hits: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Add to cart action
  3.  *
  4.  * Checks for a valid request, does validation (via hooks) and then redirects if valid
  5.  **/
  6. function woocommerce_add_to_cart_action( $url = false ) {
  7.         global $woocommerce;
  8.  
  9.         if (empty($_REQUEST['add-to-cart']) || !$woocommerce->verify_nonce('add_to_cart', '_REQUEST')) return;
  10.    
  11.     $added_to_cart              = false;
  12.    
  13.     switch ($_REQUEST['add-to-cart']) {
  14.        
  15.         // Variable Products
  16.         case 'variation' :
  17.                
  18.                 // Only allow integer variation ID - if its not set, redirect to the product page
  19.                 if (empty($_REQUEST['variation_id']) || !is_numeric($_REQUEST['variation_id']) || $_REQUEST['variation_id']<1) {
  20.                         $woocommerce->add_error( __('Please choose product options&hellip;', 'woocommerce') );
  21.                         wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
  22.                         exit;
  23.                 }
  24.                
  25.                 // Get product ID to add and quantity
  26.                 $product_id             = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['product_id']);
  27.                 $variation_id           = (int) $_REQUEST['variation_id'];
  28.                 $quantity                       = (isset($_REQUEST['quantity'])) ? (int) $_REQUEST['quantity'] : 1;
  29.                 $attributes             = (array) maybe_unserialize(get_post_meta($product_id, '_product_attributes', true));
  30.                 $variations             = array();
  31.                 $all_variations_set = true;
  32.                
  33.                 // Verify all attributes for the variable product were set
  34.                 foreach ($attributes as $attribute) {
  35.                 if ( !$attribute['is_variation'] ) continue;
  36.  
  37.                 $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
  38.                 if (!empty($_REQUEST[$taxonomy])) {
  39.                     // Get value from post data
  40.                     $value = esc_attr(stripslashes($_REQUEST[$taxonomy]));
  41.  
  42.                     // Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
  43.                     $variations[esc_attr($attribute['name'])] = $value;
  44.                                 } else {
  45.                     $all_variations_set = false;
  46.                 }
  47.             }
  48.  
  49.             if ($all_variations_set) {
  50.                 // Add to cart validation
  51.                 $passed_validation      = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
  52.                
  53.                 if ($passed_validation) {
  54.                                         if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
  55.                                                 woocommerce_add_to_cart_message();
  56.                                                 $added_to_cart = true;
  57.                                         }
  58.                                 }
  59.             } else {
  60.                 $woocommerce->add_error( __('Please choose product options&hellip;', 'woocommerce') );
  61.                 wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
  62.                 exit;
  63.            }
  64.                            
  65.         break;
  66.        
  67.         // Grouped Products
  68.         case 'group' :
  69.        
  70.                         if (isset($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
  71.                                
  72.                                 $quantity_set = false;
  73.                                
  74.                                 foreach ($_REQUEST['quantity'] as $item => $quantity) {
  75.                                         if ($quantity<1) continue;
  76.                                        
  77.                                         $quantity_set = true;
  78.                                        
  79.                                         // Add to cart validation
  80.                                         $passed_validation      = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
  81.                                        
  82.                                         if ($passed_validation) {
  83.                                                 if ($woocommerce->cart->add_to_cart($item, $quantity)) {
  84.                                                         woocommerce_add_to_cart_message();
  85.                                                         $added_to_cart = true;
  86.                                                 }
  87.                                         }
  88.                                 }
  89.                                
  90.                                 if (!$added_to_cart && !$quantity_set) {
  91.                                         $woocommerce->add_error( __('Please choose a quantity&hellip;', 'woocommerce') );
  92.                                         wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
  93.                                         exit;
  94.                                 }
  95.        
  96.                         } elseif ($_REQUEST['product_id']) {
  97.                        
  98.                                 /* Link on product archives */
  99.                                 $woocommerce->add_error( __('Please choose a product&hellip;', 'woocommerce') );
  100.                                 wp_redirect( get_permalink( $_REQUEST['product_id'] ) );
  101.                                 exit;
  102.                                
  103.                         }
  104.  
  105.         break;
  106.        
  107.         // Simple Products - add-to-cart contains product ID
  108.         default :
  109.                
  110.                 // Only allow integers
  111.                 if (!is_numeric($_REQUEST['add-to-cart'])) break;
  112.                
  113.                 // Get product ID to add and quantity
  114.                 $product_id             = (int) $_REQUEST['add-to-cart'];
  115.                 $quantity               = (isset($_REQUEST['quantity'])) ? (int) $_REQUEST['quantity'] : 1;
  116.                
  117.                 // Add to cart validation
  118.                 $passed_validation      = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
  119.                
  120.                 if ($passed_validation) {
  121.                         // Add the product to the cart
  122.                         if ($woocommerce->cart->add_to_cart($_REQUEST['add-to-cart'], $quantity)) {
  123.                                 woocommerce_add_to_cart_message();
  124.                                 $added_to_cart = true;
  125.                         }
  126.                 }
  127.        
  128.         break;
  129.        
  130.     }
  131.    
  132.     // If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
  133.     if ($added_to_cart) {
  134.    
  135.                 $url = apply_filters('add_to_cart_redirect', $url);
  136.                
  137.                 // If has custom URL redirect there
  138.                 if ( $url ) {
  139.                         wp_safe_redirect( $url );
  140.                         exit;
  141.                 }
  142.                
  143.                 // Redirect to cart option
  144.                 elseif (get_option('woocommerce_cart_redirect_after_add')=='yes' && $woocommerce->error_count() == 0) {
  145.                         wp_safe_redirect( $woocommerce->cart->get_cart_url() );
  146.                         exit;
  147.                 }
  148.  
  149.     }
  150.  
  151. }