Merge pull request #7103 from woothemes/cart-undo

Cart undo
This commit is contained in:
Claudio Sanches 2015-01-14 10:38:31 -02:00
commit 0b4074cf22
2 changed files with 45 additions and 25 deletions

View File

@ -16,6 +16,9 @@ class WC_Cart {
/** @var array Contains an array of cart items. */
public $cart_contents = array();
/** @var array Contains an array of removed cart items. */
public $removed_cart_contents = array();
/** @var array Contains an array of coupon codes applied to the cart. */
public $applied_coupons = array();
@ -192,8 +195,8 @@ class WC_Cart {
$this->$key = WC()->session->get( $key, $default );
}
// Load coupons
$this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) );
$this->removed_cart_contents = array_filter( WC()->session->get( 'removed_cart_contents', array() ) );
$this->applied_coupons = array_filter( WC()->session->get( 'applied_coupons', array() ) );
// Load the cart
$cart = WC()->session->get( 'cart', array() );
@ -255,6 +258,7 @@ class WC_Cart {
WC()->session->set( 'applied_coupons', $this->applied_coupons );
WC()->session->set( 'coupon_discount_amounts', $this->coupon_discount_amounts );
WC()->session->set( 'coupon_discount_tax_amounts', $this->coupon_discount_tax_amounts );
WC()->session->set( 'removed_cart_contents', $this->removed_cart_contents );
foreach ( $this->cart_session_data as $key => $default ) {
WC()->session->set( $key, $this->$key );
@ -669,20 +673,14 @@ class WC_Cart {
/**
* Gets the url to re-add an item into the cart.
*
* @param int product_id
* @param int quantity
* @param int variation_id
* @param object variation
* @param int $cart_item_key
* @return string url to page
*/
public function get_undo_url( $product_id, $quantity = 1, $variation_id = '', $variation = '' ) {
public function get_undo_url( $cart_item_key ) {
$cart_page_id = wc_get_page_id( 'cart' );
$query_args = array(
'undo_item' => $product_id,
'quantity' => $quantity,
'variation' => $variation,
'variation_id' => $variation_id
'undo_item' => $cart_item_key,
);
return apply_filters( 'woocommerce_get_undo_url', $cart_page_id ? wp_nonce_url( add_query_arg( $query_args, get_permalink( $cart_page_id ) ), 'woocommerce-cart' ) : '' );
@ -996,6 +994,36 @@ class WC_Cart {
return $cart_item_key;
}
public function remove_cart_item( $cart_item_key ) {
if ( isset( $this->cart_contents[ $cart_item_key ] ) ) {
$remove = $this->cart_contents[ $cart_item_key ];
$this->removed_cart_contents[ $cart_item_key ] = $remove;
unset( $this->cart_contents[ $cart_item_key ] );
$this->calculate_totals();
return true;
}
return false;
}
public function restore_cart_item( $cart_item_key ) {
if ( isset( $this->removed_cart_contents[ $cart_item_key ] ) ) {
$restore = $this->removed_cart_contents[ $cart_item_key ];
$this->cart_contents[ $cart_item_key ] = $restore;
unset( $this->removed_cart_contents[ $cart_item_key ] );
$this->calculate_totals();
return true;
}
return false;
}
/**
* Set the quantity for an item in the cart.
*

View File

@ -379,13 +379,9 @@ class WC_Form_Handler {
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
$product = wc_get_product( $cart_item['product_id'] );
WC()->cart->set_quantity( $cart_item_key, 0 );
WC()->cart->remove_cart_item( $cart_item_key );
if ( $product->product_type != 'variable' ) {
$undo = WC()->cart->get_undo_url( $cart_item['product_id'], $cart_item['quantity'] );
} else {
$undo = WC()->cart->get_undo_url( $cart_item['product_id'], $cart_item['quantity'], $cart_item['variation_id'], $cart_item['variation'] );
}
$undo = WC()->cart->get_undo_url( $cart_item_key );
wc_add_notice( sprintf( __( '%s removed. %sUndo?%s', 'woocommerce' ), $product->get_title(), '<a href="' . $undo . '">', '</a>' ) );
$referer = wp_get_referer() ? remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) : WC()->cart->get_cart_url();
@ -394,17 +390,13 @@ class WC_Form_Handler {
}
//Undo Cart Item
// Undo Cart Item
elseif ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'woocommerce-cart' ) ) {
$product = wc_get_product( $_GET['undo_item'] );
$cart_item_key = $_GET['undo_item'];
if ( $product->product_type != 'variable' ) {
WC()->cart->add_to_cart( $_GET['undo_item'], $_GET['quantity'] );
} else {
WC()->cart->add_to_cart( $_GET['undo_item'], $_GET['quantity'], $_GET['variation_id'], $_GET['variation'] );
}
WC()->cart->restore_cart_item( $cart_item_key );
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', 'quantity', 'variation_id', 'variation', '_wpnonce' ), wp_get_referer() ) : WC()->cart->get_cart_url();
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) : WC()->cart->get_cart_url();
wp_safe_redirect( $referer );
exit;
}