Jump to content

Add available quantity to cart when more than maxium in stock is added.


mytheory.

Recommended Posts

Hello,

So I'm trying to figure this one out... Currently, we have our show available stock quantity option turned off in the BO. We do not want to reveal this info to the customer, only that it is in stock or out of stock. We utilize a lot of attributes with numerous combinations for most of our products and often times we only hold a couple of each on hand.

So here is the problem...
So lets say for ProductA we have 3 in stock, but the customer adds, lets say, 5... when this qty gets added to the cart this throws an error saying "the product is not available" and nothing is added.

Looking for...
So when this happens I want the maximum qty we have on hand to be added to the cart and to throw a different error message. So for the above example the customer's cart will get added a quantity of 3 and will display a message stating the quantity has been adjusted and they can contact us for more info.

Because our stock levels vary drastically between our different products it would mean a guessing game for the customer to add the maximum on hand qty, which usually leads to frustration and abandoned carts.

Any help is appreciated!

Link to comment
Share on other sites

Try adding the following after $errors = array(); on line 5 of cart.php:

$warnings = array();



and change the /* Check the quantity available */ section to the following:

/* Check the quantity availability */
if ($idProductAttribute AND is_numeric($idProductAttribute))
{
   if (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty)))
   {
       $result = Db::getInstance()->getRow('
       SELECT `quantity`
       FROM `'._DB_PREFIX_.'product_attribute`
       WHERE `id_product_attribute` = '.intval($idProductAttribute));
       $qty = $result['quantity'];
       $warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
   }
}
elseif ($producToAdd->hasAttributes() AND !$delete)
{
   $idProductAttribute = Product::getDefaultAttribute(intval($producToAdd->id), intval($producToAdd->out_of_stock) == 2 ? !intval(Configuration::get('PS_ORDER_OUT_OF_STOCK')) : !intval($producToAdd->out_of_stock));
   if (!$idProductAttribute)
       Tools::redirectAdmin($link->getProductLink($producToAdd));
   elseif (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty)))
   {
       $result = Db::getInstance()->getRow('
       SELECT `quantity`
       FROM `'._DB_PREFIX_.'product_attribute`
       WHERE `id_product_attribute` = '.intval($idProductAttribute));
       $qty = $result['quantity'];
       $warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
   }
}
elseif (!$delete AND !$producToAdd->checkQty(intval($qty)))
{
   $qty = $producToAdd->quantity;
   $warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
}



Then you just need to figure out how to display the warnings in a way that doesn't block the next checkout step like the errors do.

Link to comment
Share on other sites

Hey rocky,

Thanks so much!

So if the qty is over the qty in stock it adds the maximum qty in stock to the cart, which is great! However, I can't get the "warnings" to pop up at all. I'm not sure if I am looking in the right places, but I'm not getting any type of message when this qty is added to the cart. Any extra guidance would be much appreciated.

Also, to round out the edges would it possible to allow the cart to preform exactly the same way (as your code accomplishes) for the following scenario? So, ProductA has 10 qty in stock. If I add 5 to the cart, it adds it to the cart, but if I add another 10 on top of that it states that "the maximum qty is already added" and it doesn't add the maximum on hand qty. It does as before... leaving the initial 5, but ignoring the rest with an error message. For the same scenario would it be possible to add 5 to the initial 5 (which brings the total equal to the max stock on hand) and outputs a warning stating the same as above?

THANKS AGAIN!

Link to comment
Share on other sites

Any advice on how errors work and what files I should be looking at?

So the new "warnings" array collects messages according to the conditions; however, it seems like (referring to errors) that there is another function or something that actually outputs the contents of these arrays. Any direction on this matter is greatly appreciated!

Thanks!

Link to comment
Share on other sites

  • 9 months later...
  • 1 year later...

Hello,

 

Would someone be able to update:

 

Try adding the following after $errors = array(); on line 5 of cart.php:

 

$warnings = array();

 

and change the /* Check the quantity available */ section to the following:

 

/* Check the quantity availability */
if ($idProductAttribute AND is_numeric($idProductAttribute))
{
if (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty)))
{
	$result = Db::getInstance()->getRow('
	SELECT `quantity`
	FROM `'._DB_PREFIX_.'product_attribute`
	WHERE `id_product_attribute` = '.intval($idProductAttribute));
	$qty = $result['quantity'];
	$warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
}
}
elseif ($producToAdd->hasAttributes() AND !$delete)
{
$idProductAttribute = Product::getDefaultAttribute(intval($producToAdd->id), intval($producToAdd->out_of_stock) == 2 ? !intval(Configuration::get('PS_ORDER_OUT_OF_STOCK')) : !intval($producToAdd->out_of_stock));
if (!$idProductAttribute)
	Tools::redirectAdmin($link->getProductLink($producToAdd));
elseif (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty(intval($idProductAttribute), intval($qty)))
{
	$result = Db::getInstance()->getRow('
	SELECT `quantity`
	FROM `'._DB_PREFIX_.'product_attribute`
	WHERE `id_product_attribute` = '.intval($idProductAttribute));
	$qty = $result['quantity'];
	$warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
}
}
elseif (!$delete AND !$producToAdd->checkQty(intval($qty)))
{
$qty = $producToAdd->quantity;
$warnings[] = Tools::displayError('The quantity of "'.$productToAdd->name.'" has been adjusted. Please contact us for more information.');
}

 

Then you just need to figure out how to display the warnings in a way that doesn't block the next checkout step like the errors do.

 

for version 1.5.3 please?

 

Thanks

Link to comment
Share on other sites

  • 9 months later...
  • 6 months later...
  • 2 years later...

Same issue here, except I would like the warning to say "X quantity is in stock, and the rest will be backordered" on PS 1.5.6.1. And it should add the full quantity desired to the cart.

 

Any help is greatly appreciated.

I would need this in 1.6.1.10  :)

Link to comment
Share on other sites

I found out a way to create a solution to this. The only problem is that this does not work with ajax so a manual page refresh is needed when customer changes product quantities in cart. Would be great if someone could continue with the development or help point me in the direction of how to add ajax functionality to this!

 

Here's a solution to show availability warning when customer orders more products than there is in stock. This is needed because I want to allow ordering products that are out of stock.

 

Pardon my quick and dirty translations with paint in the screenshot. Stock quantity is not visible for customers although I wrote it in the screenshot for you to see how this works. This allows the competitors to use cart to spy how many products you have in stock if they test adding products to the cart for as long as the availability status will change. This does not concern me as I care more about customer satisfaction so that they know how many of their ordered products will de delivered immediately and how many of them will be backordered.

 

Screenshot:

post-1350412-0-08702600-1484219504_thumb.png

 

Needed modifications:

 

shopping-cart-product-line.tpl modify the corresponding part:

{if $PS_STOCK_MANAGEMENT}
<td class="cart_avail">

{if $product.quantity_available < $product.cart_quantity && $product.quantity_available >= 0}<span class="backorderedown">{l s='%d backordered' sprintf=$product.cart_quantity - $product.quantity_available}</span>{else}

{if $product.quantity_available <= 0}<span class="notavailableown">{if $product.allow_oosp}{if isset($product.available_later) && $product.available_later}{$product.available_later}</span>{else}<span class="availableown">{l s='In Stock'}</span>{/if}{else}<span class="backorderedown">{l s='Backordered'}</span>{/if}{else}<span class="availableown">{if isset($product.available_now) && $product.available_now}{$product.available_now}</span>{else}<span class="availableown">{l s='In Stock'}</span>{/if}{/if}{hook h="displayProductDeliveryTime" product=$product}
</td>
{/if}

global.css add these:

.availableown {
  background-color: #5cb85c;
  display: inline;
  padding: .1em .4em .2em;
  font-size: 88%;
  font-weight: bold;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: .25em; }

.backorderedown {
  background-color: #f0ad4e;
  display: inline;
  padding: .2em .6em .3em;
  font-size: 75%;
  font-weight: bold;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: .25em; }

.notavailableown {
  background-color: #d9534f;
  display: inline;
  padding: .2em .6em .3em;
  font-size: 75%;
  font-weight: bold;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: .25em; }

Please tell me how can I modify the code to make availability status change immediately using ajax when customer changes product quantities in cart?

Edited by kamppa (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...
  • 5 months later...

Hello. He came to the state-of-the-art upgrade solution.

In the themes / your_theme / js / cart-summary.js file, find function upQuantity (id, qty) at line 578

For this function, complete the following function after the closed race:

function updateCartAvailability(json)
{
    var i;
    var product_list = new Array();

    if (typeof json == 'undefined')
        return;
        
    $('div.error').fadeOut();        

    for (i=0;i<json.products.length;i++)
        product_list[json.products[i].id_product + '_' + json.products[i].id_product_attribute + '_' + json.products[i].id_address_delivery] = json.products[i];
        
    for (i in product_list)
    {
        var key_for_blockcart_nocustom = product_list[i].id_product + '_' + product_list[i].id_product_attribute + '_' + ((product_list[i].quantity_without_customization != product_list[i].quantity)? 'nocustom' : '0') + '_' + product_list[i].id_address_delivery;
        
        if (product_list[i].quantity_available - product_list[i].cart_quantity >= 0) {
                $('#product_' + key_for_blockcart_nocustom + ' .cart_ref').html('<span class="availableown">{l s='In Stock'}</span>');
            } else if (product_list[i].quantity_available - product_list[i].cart_quantity < 0 && product_list[i].allow_oosp) {
                $('#product_' + key_for_blockcart_nocustom + ' .cart_ref').html('<span class="backorderedown">{l s='%d backordered' sprintf=$product.cart_quantity - $product.quantity_available}</span>');
            } else if (product_list[i].quantity_available - product_list[i].cart_quantity < 0 && !product_list[i].allow_oosp) {
                $('#product_' + key_for_blockcart_nocustom + ' .cart_ref').html('<span class="availableown">{l s='Out of stock'}</span>');
            }
    }

}

I´m sorry for ma english.

Link to comment
Share on other sites

Excellent! Thank you! Working good. It is not AJAX because it makes the cart page refresh with each change but it works great nevertheless!

 

Edit: Unfortunately I am no longer able to download PDF invoice from customer's my account. Also, my payment module does not get back to the shop after purchase without a refresh. Disabling the modification to the .js file fixes these bugs. So currently it is unusable.

Edited by kamppa (see edit history)
Link to comment
Share on other sites

I've just tried all the possibilities of the business and everything works perfectly without problems. Please describe your problem in more detail and I will try to edit the feature. Just add that I tested everything in version 1.6.1.11

I know it's not just about Ajax, but I needed to fix it. That's why I used this solution. Just to get something out of it.

 

I´m sorry for my english.

Link to comment
Share on other sites

  • 1 year later...

Just tried your solution, everything works great. Prestashop 1.6.18

Only one question. If the product does not have a backorder and adding quantity in cart more then instock, then an error message. Is it possible to make a pop-up window, as it was before this modification?

Capture.JPG

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...