Peter Wilkinson
Peter Wilkinson

Reputation: 80

Opencart - get product option quantity or stock text (option to product module)

I am using Option to Product extension on my Opencart 2.0x website. It all works as expected. I want to have the stock availability of the options in the option select box. For example:

Current:
Blue
Red
Orange

What I want:
Blue (In Stock)
Red (In Stock)
Orange (Out of Stock)

Something like the above. So I need to either get the stock text or the quantity and then manually add the text dependant on the quantity.

I try to modify the code in view/theme/*/template/product/product.tpl to be:

<?php 
foreach ($option['option_value'] as $option_value) {  
if ($option_value['quantity'] == "0") {
$stock = "Out of Stock";
} else {
$stock = "In Stock";
}
?>
<option value="<?php echo $option_value['option_value_id']; ?>"><?php echo $option_value['name']; ?> (<?php echo $stock ?>) </option>
}

In catalog/controller/product/product.php after

'option_value_id'         => $option_value['option_value_id'],

I add

'quantity'         => $option_value['quantity'],

I get nothing!


I have even tried to access the database directly but it doesn't work - I am not sure how to get the value I want from the database - I use

$product_id = $product_id;
$option_id = $option_value['option_value_id'];

$get_otp_id = $this->db->query("SELECT id from oc_otp_option_value where product_id = $product_id AND parent_option_value_id = $option_id");

I know I cannot add this to the product template page as it is a MVC setup but don't know how to add this to the Model and then use the Controller to get the data and show it on the View template as I am a noob with MVC and OpenCart.

Please help - I will love you forever!

Thanks

Upvotes: 0

Views: 2339

Answers (1)

DigitCart
DigitCart

Reputation: 3000

This works for me, Edit this file:

catalog\controller\product\product.php

Find:

foreach ($option['product_option_value'] as $option_value) {

Add after it:

$stock_text = $option_value['quantity'] > 0 ? '(In Stock)' : '(Out Of Stock)';

Find:

'name'                    => $option_value['name'],

Replace with:

'name'                    => $option_value['name'] . $stock_text,

Then clear all your caches (ocmod, vqmod, ...).

Note: If an option value is out of stock, OpenCart will not display that option on the product page, unless you choose "No" to Subtract Stock.

Upvotes: 0

Related Questions