how to get stock status in opencart product page - php

I want to show
out of stock
on my product page and I change the status from admin and is updated in admin but on my product page there is also same
In Stock
and product page code is
if ($product_info['quantity'] <= 0) {
$this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
$this->data['stock'] = $product_info['quantity'];
} else{
$this->data['stock'] = $this->language->get('text_instock');
}
and language page is
$_['text_outstock'] = 'Out of Stock';
Now what condition is used there?

As far as I understand your issue, you can set it directly in the Admin to show 'out of stock' message.
You need to set it both in the general settings AND in the invidividual product.
Currently, because you did not set your Admin settings correctly, $product_info['stock_status'] does not display 'out of stock'.
However, if you insist of modifying the code, you need to use the following:
if ($product_info['quantity'] <= 0) {
//out of stock message
$this->data['stock'] = $this->language->get('text_outstock');
} elseif ($this->config->get('config_stock_display')) {
//in stock, and displaying exact quantity
$this->data['stock'] = $product_info['quantity'];
} else{
//in stock, but just display a message not exact quantity
$this->data['stock'] = $this->language->get('text_instock');
}
Hope this helps!

Related

i want to increase cart quantity php every time add to cart is clicked. I checked few provided solutions but none of them worked

<?php
session_start();
if (isset($_GET) & !empty($_GET)) {
$id = $_GET['id'];
$url = $_GET['sourceurl'];
if (isset($_GET['quant']) & !empty($_GET['quant'])) {
$quant = $_GET['quant'];
} else {
$quant = 1;
}
$_SESSION['cart'][$id] = array("quantity" => $quant);
//header('location:' . $url);
//header('location:index.php?message=1');
} else {
//header('location:' . $url);
}
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
I have common php page addtocart.php to add product directly to cart from homepage and also from single.php where user can add quantity. If quantity is not given(i.e adding to cart from index page) quantity is directly set to 1. Here I want to increase quanity every time user clicks on addtocart. Above is the code of addtocart.php
Replace $_SESSION['cart'][$id] = array("quantity" => $quant); for this oneliner.
$_SESSION['cart'][$id]['quantity'] = ((isset($_SESSION['cart'][$id]['quantity'])) && ($_SESSION['cart'][$id]['quantity'] > 0)) ? ++$_SESSION['cart'][$id]['quantity'] : $quant;

Add the same idProduct to SESSION in cart php

I´m making a cart in php that get the data of the products from a MySQL Database.
The problem is: I can´t add the same product to the SESSION.
I need that, because the user may want one product with a picture and another one with another picture;
I need that so much.
This is the code I'm using to add the item to the cart:
if(isset($_GET['action'])){
//ADD TO THE CART
if($_GET['action'] == 'add'){
$id = intval($_GET['idProduct']);
if(!isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id] = 1;
}else{
$_SESSION['cart'][$id] += 1;
}
}
//UPDATE CART
if($_GET['action'] == 'up'){
if(is_array($_POST['prod'])){
foreach($_POST['prod'] as $id => $qtd){
$id = intval($id);
$qtd = intval($qtd);
if(!empty($qtd) || $qtd <> 0){
$_SESSION['cart'][$id] = $qtd;
}else{
unset($_SESSION['cart'][$id]);
}
}
}
}
I don't know how to add the same product. I tried using an Array and getting the KEY from it, but I didn't know how to use it;

Magento: Is a certain product in the cart?

On my list.phtml page, I want my PHP script to be able to tell whether any product is in the cart, based on it's SKU.
So my conditional would theoretically be like this:
$_sku = 123;
if($_sku->isInBasket() == true){
echo 'Product: ' . $_sku . ' is in the cart';
}
How can this be achieved realistically?
Fetch all data from checkout session and check your product exits in Current session
$quote = Mage::getSingleton('checkout/session')->getQuote();
$foundInCart = false;
foreach($quote->getAllVisibleItems() as $item) {
if ($item->getData('sku') == $_sku) {
$foundInCart = true;
break;
}
}
it really bad to Check by sku.
Because of whenever configurable product cart then simple product sku in db.
So you need to check using product id.for this case you need find the id of $sku by Mage::getModel('catalog')->loadBySku($sku); before start of products foreach loop.
$_skuPId='';
$matchPro=Mage::getModel('catalog')->loadBySku($sku);
if($matchPro->getId()){
$_skuPId=$matchPro->getId();
}
$quote = Mage::getSingleton('checkout/session')->getQuote();
$foundInCart = false;
foreach($quote->getAllVisibleItems() as $item) {
if ($item->getData('prodduct_id') == $_skuPId) {
$foundInCart = true;
break;
}
}

Quantity in Opencart

Dear stackoverflow code experts. I have a query relating to opencart stock.
The frontend product page has option of displaying Stock, either in terms of availability (Available or Out of Stock) or in terms of quantity in actual numbers.
Is it possible to display it in some other way? eg. I want that if the stock quantity is less than or equal to 5, then it should display quantity, else display the text: Available.
Or in a more sophisticated manner, if product quantity is greater than 5 or zero, then display text, else display quantity in number.
I understand it may have to do something with ../catalog/controller/product/product.php file.
I am not an expert in coding. Please help me.
Thank you.
It's simple.
First set "display stock" to Yes
admin panel>system>setting>options set display stock to "YES"
now
//catalog>controller>product>product.php
Find (around line 282)
if ($product_info['quantity'] <= 0) {
$this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
$this->data['stock'] = $product_info['quantity'];
} else {
$this->data['stock'] = $this->language->get('text_instock');
}
Replace With
if ($product_info['quantity'] <= 0) {
$this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display') && $product_info['quantity'] <= 5) {
$this->data['stock'] = $product_info['quantity'];
} else {
$this->data['stock'] = $this->language->get('text_instock');
}
Hope this helps
Edit the file catalog/controller/product/category.php
Step : 1
Find the code:
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
Add the following just below the above code :
if ($result['quantity'] <= 0) {
$rstock = $result['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
$rstock = "Stoc: " . $result['quantity'];
} else {
$rstock = "In stoc";
}
Step : 2
Find :
'thumb' => $image,
Add the following just after the above line
'stoc' => $rstock,
Step 3
Edit the file catalog/view/theme/yourtheme/template/product/category.tpl
Find :
<div class="cart">
Add the following :
<?php echo $product['stoc']; ?>
And now the stock will appear for products in category page.
You can do the same for search (the files would be search.php and search.tpl - in the same folders as the category)
You can see a more detailed tutorial at :
http://www.aleixcortadellas.com/main/2009/09/09/742/
http://forum.opencart.com/viewtopic.php?t=27137
http://forum.opencart.com/viewtopic.php?t=66506
All of the above uses the same idea, but implemented in different ways. But this should help you solve your problem.
Hope this helps.

Allow customers to buy out-of-stock items in OpenCart

I have been looking for a module to allow a client to be able to still purchase an item if the stock level is 0. Is this feature available in OpenCart 1.5.x?
I have set the product to 2-3 days, however on the site front-end it still shows the product as out of stock. Is there away to alert the client of the 2-3 day delay, and still allow the client to purchase?
First you need to change the function that prevents an out of stock item from going to checkout. Go to catalog/controller/checkout/checkout.php and change
public function index() {
// Validate cart has products and has stock.
if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
$this->redirect($this->url->link('checkout/cart'));
}
to
public function index() {
// Validate cart has products and has stock.
if (!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) {
$this->redirect($this->url->link('checkout/cart'));
}
I don't remember if it blocks you from adding it to the cart in the first place so let me know. Good luck David!
Update
To change "Out of Stock" on the product page, I have changed it myself with the settings in the store so if that isn't working for you then you can go into catalog/controller/product/product.php and where you see
if ($product_info['quantity'] <= 0) {
$this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
$this->data['stock'] = $product_info['quantity'];
} else {
$this->data['stock'] = $this->language->get('text_instock');
}
Change to:
if ($product_info['quantity'] <= 0) {
$this->data['stock'] = "2-3 Days";
} elseif ($this->config->get('config_stock_display')) {
$this->data['stock'] = $product_info['quantity'];
} else {
$this->data['stock'] = $this->language->get('text_instock');
}
Change the text within those brackets to whichever phrase works for you.
This is a feature built into OpenCart as standard. The setting should be on the "Option" tab in your settings page
First find
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
and replace with
if (1==1 || !$option_value['subtract'] || ($option_value['quantity'] > 0)) {

Categories