示例#1
0
function jigoshop_add_to_cart_action( $url = false ) {
	
	if (isset($_GET['add-to-cart']) && $_GET['add-to-cart']) :
	
		if ( !jigoshop::verify_nonce('add_to_cart', '_GET') ) :

		elseif (is_numeric($_GET['add-to-cart'])) :
		
			$quantity = 1;
			if (isset($_POST['quantity'])) $quantity = $_POST['quantity'];
			jigoshop_cart::add_to_cart($_GET['add-to-cart'], $quantity);
			
			jigoshop::add_message( sprintf(__('<a href="%s" class="button">View Cart &rarr;</a> Product successfully added to your basket.', 'jigoshop'), jigoshop_cart::get_cart_url()) );
			
		elseif ($_GET['add-to-cart']=='group') :
			
			// Group add to cart
			if (isset($_POST['quantity']) && is_array($_POST['quantity'])) :
				
				$total_quantity = 0;
				
				foreach ($_POST['quantity'] as $item => $quantity) :
					if ($quantity>0) :
						jigoshop_cart::add_to_cart($item, $quantity);
						jigoshop::add_message( sprintf(__('<a href="%s" class="button">View Cart &rarr;</a> Product successfully added to your basket.', 'jigoshop'), jigoshop_cart::get_cart_url()) );
						$total_quantity = $total_quantity + $quantity;
					endif;
				endforeach;
				
				if ($total_quantity==0) :
					jigoshop::add_error( __('Please choose a quantity&hellip;', 'jigoshop') );
				endif;
			
			elseif ($_GET['product']) :
				
				/* Link on product pages */
				jigoshop::add_error( __('Please choose a product&hellip;', 'jigoshop') );
				wp_redirect( get_permalink( $_GET['product'] ) );
				exit;
			
			endif; 
			
		endif;
		
		$url = apply_filters('add_to_cart_redirect', $url);
		
		// If has custom URL redirect there
		if ( $url ) {
			wp_safe_redirect( $url );
			exit;
		}
		
		// Otherwise redirect to where they came
		else if ( isset($_SERVER['HTTP_REFERER'])) {
			wp_safe_redirect($_SERVER['HTTP_REFERER']);
			exit;
		}
		
		// If all else fails redirect to root
		else {
			wp_safe_redirect('/');
			exit;
		}
	endif;
	
}
示例#2
0
 function jigoshop_add_to_cart_action($url = false)
 {
     if (empty($_REQUEST['add-to-cart']) || !jigoshop::verify_nonce('add_to_cart')) {
         return false;
     }
     $options = Jigoshop_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('jigoshop_product_id_add_to_cart_filter', $product_id);
             $variation_id = apply_filters('jigoshop_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) jigoshop_session::instance()->customized_products;
                 // transfer it to the variation
                 $custom_products[$variation_id] = $custom_products[$product_id];
                 unset($custom_products[$product_id]);
                 jigoshop_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('jigoshop_add_to_cart_validation', true, $product_id, $quantity);
             if ($all_variations_set && $is_valid) {
                 if (jigoshop_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('jigoshop_add_to_cart_validation', true, $product_id, $quantity);
                 // Add to the cart if passsed validation
                 if ($is_valid) {
                     if (jigoshop_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('jigoshop_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('jigoshop_add_to_cart_validation', true, $product_id, $quantity);
             // Add to the cart if passed validation
             if ($is_valid) {
                 if (jigoshop_cart::add_to_cart($product_id, $quantity)) {
                     $product_added = true;
                 }
             }
             break;
     }
     if (!$product_added) {
         jigoshop::add_error(__('The Product could not be added to the cart.  Please try again.', 'jigoshop'));
         wp_safe_redirect(remove_query_arg(array('add-to-cart', 'quantity', 'product_id', '_n'), wp_get_referer()), 301);
         exit;
     } else {
         switch ($options->get('jigoshop_redirect_add_to_cart', 'same_page')) {
             case 'same_page':
                 $message = __('Product successfully added to your cart.', 'jigoshop');
                 $button = __('View Cart &rarr;', 'jigoshop');
                 $message = '<a href="%s" class="button">' . $button . '</a> ' . $message;
                 jigoshop::add_message(sprintf($message, jigoshop_cart::get_cart_url()));
                 break;
             case 'to_checkout':
                 // Do nothing
                 break;
             default:
                 jigoshop::add_message(__('Product successfully added to your cart.', 'jigoshop'));
                 break;
         }
         $url = apply_filters('add_to_cart_redirect', $url);
         if ($url) {
             wp_safe_redirect($url, 301);
             exit;
         } else {
             if ($options->get('jigoshop_redirect_add_to_cart', 'same_page') == 'to_checkout' && !jigoshop::has_errors()) {
                 wp_safe_redirect(jigoshop_cart::get_checkout_url(), 301);
                 exit;
             } else {
                 if ($options->get('jigoshop_redirect_add_to_cart', 'to_cart') == 'to_cart' && !jigoshop::has_errors()) {
                     wp_safe_redirect(jigoshop_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;
                     }
                 }
             }
         }
     }
 }