Merge pull request #8913 from woothemes/add-to-cart-handlers

Add to cart handlers
This commit is contained in:
Mike Jolley 2015-08-21 14:35:20 +01:00
commit 8fca3e2d60
1 changed files with 126 additions and 130 deletions

View File

@ -579,19 +579,106 @@ class WC_Form_Handler {
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) ); $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$was_added_to_cart = false; $was_added_to_cart = false;
$added_to_cart = array();
$adding_to_cart = wc_get_product( $product_id ); $adding_to_cart = wc_get_product( $product_id );
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart ); $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
// Check if the product is published
if ( ! $adding_to_cart->is_purchasable() ) {
wc_add_notice( __( 'Sorry, this product is unavailable.', 'woocommerce' ), 'error' );
return;
}
// Variable product handling // Variable product handling
if ( 'variable' === $add_to_cart_handler ) { if ( 'variable' === $add_to_cart_handler ) {
$was_added_to_cart = self::add_to_cart_handler_variable( $product_id );
// Grouped Products
} elseif ( 'grouped' === $add_to_cart_handler ) {
$was_added_to_cart = self::add_to_cart_handler_grouped( $product_id );
// Custom Handler
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ){
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url );
// Simple Products
} else {
$was_added_to_cart = self::add_to_cart_handler_simple( $product_id );
}
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && wc_notice_count( 'error' ) === 0 ) {
// If has custom URL redirect there
if ( $url = apply_filters( 'woocommerce_add_to_cart_redirect', $url ) ) {
wp_safe_redirect( $url );
exit;
} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
wp_safe_redirect( WC()->cart->get_cart_url() );
exit;
}
}
}
/**
* Handle adding simple products to the cart
* @since 2.4.6 Split from add_to_cart_action
* @param int $product_id
* @return bool success or not
*/
private static function add_to_cart_handler_simple( $product_id ) {
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity ) !== false ) {
wc_add_to_cart_message( $product_id );
return true;
}
return false;
}
/**
* Handle adding grouped products to the cart
* @since 2.4.6 Split from add_to_cart_action
* @param int $product_id
* @return bool success or not
*/
private static function add_to_cart_handler_grouped( $product_id ) {
$was_added_to_cart = false;
$added_to_cart = array();
if ( ! empty( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ) {
$quantity_set = false;
foreach ( $_REQUEST['quantity'] as $item => $quantity ) {
if ( $quantity <= 0 ) {
continue;
}
$quantity_set = true;
// Add to cart validation
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
if ( $passed_validation && WC()->cart->add_to_cart( $item, $quantity ) !== false ) {
$was_added_to_cart = true;
$added_to_cart[] = $item;
}
}
if ( ! $was_added_to_cart && ! $quantity_set ) {
wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce' ), 'error' );
} elseif ( $was_added_to_cart ) {
wc_add_to_cart_message( $added_to_cart );
return true;
}
} elseif ( $product_id ) {
/* Link on product archives */
wc_add_notice( __( 'Please choose a product to add to your cart&hellip;', 'woocommerce' ), 'error' );
}
return false;
}
/**
* Handle adding variable products to the cart
* @since 2.4.6 Split from add_to_cart_action
* @param int $product_id
* @return bool success or not
*/
private static function add_to_cart_handler_variable( $product_id ) {
$adding_to_cart = wc_get_product( $product_id );
$variation_id = empty( $_REQUEST['variation_id'] ) ? '' : absint( $_REQUEST['variation_id'] ); $variation_id = empty( $_REQUEST['variation_id'] ) ? '' : absint( $_REQUEST['variation_id'] );
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] ); $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$missing_attributes = array(); $missing_attributes = array();
@ -633,109 +720,18 @@ class WC_Form_Handler {
if ( $missing_attributes ) { if ( $missing_attributes ) {
wc_add_notice( sprintf( _n( '%s is a required field', '%s are required fields', sizeof( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ), 'error' ); wc_add_notice( sprintf( _n( '%s is a required field', '%s are required fields', sizeof( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ), 'error' );
return;
} elseif ( empty( $variation_id ) ) { } elseif ( empty( $variation_id ) ) {
wc_add_notice( __( 'Please choose product options&hellip;', 'woocommerce' ), 'error' ); wc_add_notice( __( 'Please choose product options&hellip;', 'woocommerce' ), 'error' );
return;
} else { } else {
// Add to cart validation // Add to cart validation
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations ); $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
if ( $passed_validation ) { if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) !== false ) {
if ( WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) !== false ) {
wc_add_to_cart_message( $product_id ); wc_add_to_cart_message( $product_id );
$was_added_to_cart = true; return true;
$added_to_cart[] = $product_id;
} }
} }
} return false;
// Grouped Products
} elseif ( 'grouped' === $add_to_cart_handler ) {
if ( ! empty( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ) {
$quantity_set = false;
foreach ( $_REQUEST['quantity'] as $item => $quantity ) {
if ( $quantity <= 0 ) {
continue;
}
$quantity_set = true;
// Add to cart validation
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
if ( $passed_validation ) {
if ( WC()->cart->add_to_cart( $item, $quantity ) !== false ) {
$was_added_to_cart = true;
$added_to_cart[] = $item;
}
}
}
if ( $was_added_to_cart ) {
wc_add_to_cart_message( $added_to_cart );
}
if ( ! $was_added_to_cart && ! $quantity_set ) {
wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce' ), 'error' );
return;
}
} elseif ( $product_id ) {
/* Link on product archives */
wc_add_notice( __( 'Please choose a product to add to your cart&hellip;', 'woocommerce' ), 'error' );
return;
}
// Custom Handler
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ){
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url );
return;
// Simple Products
} else {
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
// 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 ( WC()->cart->add_to_cart( $product_id, $quantity ) !== false ) {
wc_add_to_cart_message( $product_id );
$was_added_to_cart = true;
$added_to_cart[] = $product_id;
}
}
}
// If we added the product to the cart we can now optionally do a redirect.
if ( $was_added_to_cart && wc_notice_count( 'error' ) == 0 ) {
$url = apply_filters( 'woocommerce_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' ) {
wp_safe_redirect( WC()->cart->get_cart_url() );
exit;
}
}
} }
/** /**