Initial remove and restore cart methods
This commit is contained in:
parent
99095a7b57
commit
3228fc0e67
|
@ -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_card_contents = array();
|
||||
|
||||
/** @var array Contains an array of coupon codes applied to the cart. */
|
||||
public $applied_coupons = array();
|
||||
|
||||
|
@ -187,6 +190,7 @@ class WC_Cart {
|
|||
*/
|
||||
public function get_cart_from_session() {
|
||||
|
||||
|
||||
// Load cart session data from session
|
||||
foreach ( $this->cart_session_data as $key => $default ) {
|
||||
$this->$key = WC()->session->get( $key, $default );
|
||||
|
@ -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_card_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_card_contents[ $cart_item_key ] ) ) {
|
||||
$restore = $this->removed_card_contents[ $cart_item_key ];
|
||||
$this->cart_contents[ $cart_item_key ] = $restore;
|
||||
|
||||
unset( $this->removed_card_contents[ $cart_item_key ] );
|
||||
|
||||
$this->calculate_totals();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the quantity for an item in the cart.
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue