Prevent out of stock in cart
This commit is contained in:
parent
15396d57e4
commit
d3377cdfc6
|
@ -143,18 +143,41 @@ class woocommerce_cart {
|
|||
|
||||
$found_cart_item_key = self::find_product_in_cart($product_id, $variation_id, $variation);
|
||||
|
||||
$product_data = &new woocommerce_product( $product_id );
|
||||
if ($variation_id>0) :
|
||||
$product_data = &new woocommerce_product_variation( $variation_id );
|
||||
else :
|
||||
$product_data = &new woocommerce_product( $product_id );
|
||||
endif;
|
||||
|
||||
// Price set check
|
||||
if( $product_data->get_price() === '' ) {
|
||||
if( $product_data->get_price() === '' ) :
|
||||
woocommerce::add_error( __('This product cannot be purchased - the price is not yet announced', 'woothemes') );
|
||||
return false;
|
||||
}
|
||||
endif;
|
||||
|
||||
// Stock check - only check if we're managing stock and backorders are not allowed
|
||||
if ( !$product_data->has_enough_stock( $quantity ) ) :
|
||||
woocommerce::add_error( sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woothemes'), $product_data->get_stock_quantity() ));
|
||||
return false;
|
||||
elseif ( !$product_data->is_in_stock() ) :
|
||||
woocommerce::add_error( __('You cannot add that product to the cart since the product is out of stock.', 'woothemes') );
|
||||
return false;
|
||||
endif;
|
||||
|
||||
// Add it
|
||||
if (is_numeric($found_cart_item_key)) :
|
||||
|
||||
$quantity = $quantity + self::$cart_contents[$found_cart_item_key]['quantity'];
|
||||
|
||||
// Stock check - this time accounting for whats already in-cart
|
||||
if ( !$product_data->has_enough_stock( $quantity ) ) :
|
||||
woocommerce::add_error( sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock and you already have %s in your cart.', 'woothemes'), $product_data->get_stock_quantity(), self::$cart_contents[$found_cart_item_key]['quantity'] ));
|
||||
return false;
|
||||
elseif ( !$product_data->is_in_stock() ) :
|
||||
woocommerce::add_error( __('You cannot add that product to the cart since the product is out of stock.', 'woothemes') );
|
||||
return false;
|
||||
endif;
|
||||
|
||||
self::$cart_contents[$found_cart_item_key]['quantity'] = $quantity;
|
||||
|
||||
else :
|
||||
|
@ -172,6 +195,8 @@ class woocommerce_cart {
|
|||
endif;
|
||||
|
||||
self::set_session();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -223,7 +223,7 @@ class woocommerce_product {
|
|||
}
|
||||
|
||||
/** Get the title of the post */
|
||||
function get_title () {
|
||||
function get_title() {
|
||||
$this->get_post_data();
|
||||
return apply_filters('woocommerce_product_title', get_the_title($this->post->ID), $this);
|
||||
}
|
||||
|
@ -297,6 +297,8 @@ class woocommerce_product {
|
|||
/** Returns whether or not the product has enough stock for the order */
|
||||
function has_enough_stock( $quantity ) {
|
||||
|
||||
if (!$this->managing_stock()) return true;
|
||||
|
||||
if ($this->backorders_allowed()) return true;
|
||||
|
||||
if ($this->stock >= $quantity) :
|
||||
|
|
|
@ -53,7 +53,13 @@ class WooCommerce_Widget_Cart extends WP_Widget {
|
|||
if (has_post_thumbnail($cart_item['product_id'])) echo get_the_post_thumbnail($cart_item['product_id'], 'shop_thumbnail');
|
||||
else echo '<img src="'.woocommerce::plugin_url(). '/assets/images/placeholder.png" alt="Placeholder" width="'.woocommerce::get_var('shop_thumbnail_image_width').'" height="'.woocommerce::get_var('shop_thumbnail_image_height').'" />';
|
||||
|
||||
echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product).'</a> '.$cart_item['quantity'].' × '.woocommerce_price($_product->get_price()).'</li>';
|
||||
echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product).'</a>';
|
||||
|
||||
if($_product instanceof woocommerce_product_variation && is_array($cart_item['variation'])) :
|
||||
echo woocommerce_get_formatted_variation( $cart_item['variation'] );
|
||||
endif;
|
||||
|
||||
echo $cart_item['quantity'].' × '.woocommerce_price($_product->get_price()).'</li>';
|
||||
endif;
|
||||
endforeach;
|
||||
else: echo '<li class="empty">'.__('No products in the cart.', 'woothemes').'</li>'; endif;
|
||||
|
|
|
@ -302,12 +302,17 @@ function woocommerce_add_to_cart_action( $url = false ) {
|
|||
|
||||
//single product
|
||||
$quantity = (isset($_POST['quantity'])) ? (int) $_POST['quantity'] : 1;
|
||||
woocommerce_cart::add_to_cart($_GET['add-to-cart'], $quantity);
|
||||
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
// Add to the cart
|
||||
if (woocommerce_cart::add_to_cart($_GET['add-to-cart'], $quantity)) :
|
||||
|
||||
// Output success messages
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
elseif ($_GET['add-to-cart']=='variation') :
|
||||
|
@ -344,12 +349,14 @@ function woocommerce_add_to_cart_action( $url = false ) {
|
|||
if ($all_variations_set && $variation_id > 0) :
|
||||
|
||||
// Add to cart
|
||||
woocommerce_cart::add_to_cart($product_id, $quantity, $variations, $variation_id);
|
||||
if (woocommerce_cart::add_to_cart($product_id, $quantity, $variations, $variation_id)) :
|
||||
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
endif;
|
||||
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
endif;
|
||||
|
||||
else :
|
||||
|
@ -369,12 +376,15 @@ function woocommerce_add_to_cart_action( $url = false ) {
|
|||
|
||||
foreach ($_POST['quantity'] as $item => $quantity) :
|
||||
if ($quantity>0) :
|
||||
woocommerce_cart::add_to_cart($item, $quantity);
|
||||
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
if (woocommerce_cart::add_to_cart($item, $quantity)) :
|
||||
|
||||
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
|
||||
woocommerce::add_message( __('Product successfully added to your basket.', 'woothemes') );
|
||||
else :
|
||||
woocommerce::add_message( sprintf(__('<a href="%s" class="button">View Cart →</a> Product successfully added to your basket.', 'woothemes'), woocommerce_cart::get_cart_url()) );
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
$total_quantity = $total_quantity + $quantity;
|
||||
|
|
|
@ -249,7 +249,6 @@ if (!function_exists('woocommerce_simple_add_to_cart')) {
|
|||
if( $_product->get_price() === '') return;
|
||||
|
||||
if ($availability['availability']) : ?><p class="stock <?php echo $availability['class'] ?>"><?php echo $availability['availability']; ?></p><?php endif;
|
||||
|
||||
?>
|
||||
<form action="<?php echo $_product->add_to_cart_url(); ?>" class="cart" method="post">
|
||||
<div class="quantity"><input name="quantity" value="1" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div>
|
||||
|
|
Loading…
Reference in New Issue