Change Min Stock Qty From 0

Hi!

How do I change the minimum stock quantity (for all cs-cart products) from 0 to a custom value and subsequently hide products that fall below this value?

Hi!

How do I change the minimum stock quantity (for all cs-cart products) from 0 to a custom value and subsequently hide products that fall below this value?

Hello!

I am afraid, it is not possible by default. But if you are familiar with code, you can disable Show out of stock products in Settings->General and change conditions in code, for example, in app/functions/fn.catalog.php replace

if (Registry::get('settings.General.inventory_tracking') == 'Y' && // FIXME? Registry in model
    Registry::get('settings.General.show_out_of_stock_products') == 'N' &&
    $params['area'] == 'C'
) {
    $condition .= db_quote(
        ' AND (CASE products.tracking' .
        '   WHEN ?s THEN inventory.amount > 0' .
        '   WHEN ?s THEN products.amount > 0' .
        '   ELSE 1' .
        ' END)',
        ProductTracking::TRACK_WITH_OPTIONS,
        ProductTracking::TRACK_WITHOUT_OPTIONS
    );
}

with

if (Registry::get('settings.General.inventory_tracking') == 'Y' && // FIXME? Registry in model
    Registry::get('settings.General.show_out_of_stock_products') == 'N' &&
    $params['area'] == 'C'
) {
    $condition .= db_quote(
        ' AND (CASE products.tracking' .
        '   WHEN ?s THEN inventory.amount > AMOUNT' .
        '   WHEN ?s THEN products.amount > AMOUNT' .
        '   ELSE 1' .
        ' END)',
        ProductTracking::TRACK_WITH_OPTIONS,
        ProductTracking::TRACK_WITHOUT_OPTIONS
    );
}

where AMOUNT is the necessary minimum stock quantity. Please note that this setting can be used in other places, so you can search by Registry::get('settings.General.show_out_of_stock_products') to find out them.

I would add a min_stock field to the products table and then use products.min_stock as the AMOUNT in the query above. You would then be able to set individual min_stock values for each product. Feel free to contact me if you need some help with this.

Fetching these database values using hooks is working great. However, how would I approach updating database data within the same addon?

I have been diligently looking through documentation such as http://docs.cs-cart.com/4.7.x/developer_guide/addons/tutorials/advanced.htmlbut can only find examples of fetching and displaying db-data.

Fetching these database values using hooks is working great. However, how would I approach updating database data within the same addon?

I have been diligently looking through documentation such as http://docs.cs-cart.com/4.7.x/developer_guide/addons/tutorials/advanced.htmlbut can only find examples of fetching and displaying db-data.

Hello!

http://docs.cs-cart.com/4.7.x/developer_guide/core/db/placeholders.html

Here you will find the examples of update and insert data queries.

Thank you, Oleg. It's working as expected!

When managing/editing each individual product, I need to get the product_id value for a database lookup. In my manage.post.php file (absed on views->products->manage.tpl), how do I get the product id of the product I'm managing?

Code snippet from manage.post.php:

$product_stock = db_get_field('SELECT min_stock FROM ?:products WHERE product_id='. $product_id .'');

I suspect that $product_idm will be undefined, as I haven't defined it earlier in my PHP-script.

Hook-function in the addons func.php:

function fn_minimum_stock_get_product_stock_pre($product_id){
	$auth = $_SESSION['auth'];
	if (!empty($auth['user_id'])) {
		$product_stock = db_get_field('SELECT min_stock FROM ?:products WHERE product_id=?i', $product_id);
	}
}

If you mean products.post.php (not manage.post.php), please use

$product_stock = db_get_field('SELECT min_stock FROM ?:products WHERE product_id= ?i', $_REQUEST['product_id']);

As for hook, I cannot find such hook in 4.7.1 version. Where is it located?

If you mean products.post.php (not manage.post.php), please use

$product_stock = db_get_field('SELECT min_stock FROM ?:products WHERE product_id= ?i', $_REQUEST['product_id']);

As for hook, I cannot find such hook in 4.7.1 version. Where is it located?

Inside my products.post.php, my method for updating the stock looks like this:

$product_id = $_REQUEST['product_id'];

$product_stock = db_get_field(‘SELECT min_stock FROM ?:products WHERE product_id=?i’, $_REQUEST[‘product_id’]);

if (!empty($product_stock)) {
Registry::get(‘view’)->assign(‘product_stock’, $product_stock);
}

if(isset ($_POST[‘newProductStock’])){
updateStock((int)$_POST[‘newProductStock’]);
}

function updateStock($value){
$data = array(
‘min_stock’ => $value
);
db_query(“UPDATE ?:products SET ?u WHERE product_id=”.$product_id.“”, $data);
$product_stock = $value;
Registry::get(‘view’)->assign(‘product_stock’, $product_stock);
}

Yet the min_stock when editing a specific product is not altered. Is there an issue with my code? Suspecting it's got to do with my query formulation...

Inside my products.post.php, my method for updating the stock looks like this:

$product_id = $_REQUEST['product_id'];

$product_stock = db_get_field(‘SELECT min_stock FROM ?:products WHERE product_id=?i’, $_REQUEST[‘product_id’]);

if (!empty($product_stock)) {
Registry::get(‘view’)->assign(‘product_stock’, $product_stock);
}

if(isset ($_POST[‘newProductStock’])){
updateStock((int)$_POST[‘newProductStock’]);
}

function updateStock($value){
$data = array(
‘min_stock’ => $value
);
db_query(“UPDATE ?:products SET ?u WHERE product_id=”.$product_id.“”, $data);
$product_stock = $value;
Registry::get(‘view’)->assign(‘product_stock’, $product_stock);
}

Yet the min_stock when editing a specific product is not altered. Is there an issue with my code? Suspecting it's got to do with my query formulation...

Try to move the updateStock function to your func.php and send product_id as parameter to this function like this: function updateStock($value, $product_id){

Your function does not receive $product_id. Please try

$product_id = $_REQUEST['product_id'];

if(isset($_REQUEST[‘newProductStock’])){
$product_stock = (int) $_REQUEST[‘newProductStock’];
db_query(“UPDATE ?:products SET min_stock = ?i WHERE product_id = ?i”, $product_stock, $product_id);
} else {
$product_stock = db_get_field(‘SELECT min_stock FROM ?:products WHERE product_id = ?i’, $product_id);
}

Registry::get(‘view’)->assign(‘product_stock’, $product_stock);

[content removed]