Jump to content

How to make the default combination when out of stock display one of the available combination ?


Recommended Posts

Hello, I'm trying to figure out how to solve the out of stock combination products when opening the product page.

 

Now when the customer is on the product page and if the default variant-product is out of stock it doesn't change automatically to the available stock which adds some confusion for some customers that this product including the variant products is as well out of stock!

Can anybody help resolving this issue?

 

 

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

  • 1 month later...
  • 1 month later...

Hello, I've found some way to do it (PS 1.6) via php and mysql.

Simple script to change default attribute, for testing change line:

$st = $db->prepare("select *  from ps_product_attribute WHERE quantity=0 and default_on=1");

to:

$st = $db->prepare("select *  from ps_product_attribute WHERE quantity=0 and default_on=1 and id_product=_SOME_ID_");

//connect with mysql
try {
$db = new PDO(_HERE_YOUR_PARAMETERS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e) {
	die('ERR database');
}

//get defaults attributes without quantity	
$st = $db->prepare("select *  from ps_product_attribute WHERE quantity=0 and default_on=1");
try { $st->execute(); } catch ( Exception $e ) { die($e); }
$defki = $st->fetchAll(PDO::FETCH_ASSOC);
//for each default attribute without quantity
foreach ($defki as $def) {
	
	//check if product has some attribute with quantity>0
	$st = $db->prepare("select *  from ps_product_attribute WHERE quantity>0 and id_product=:idp LIMIT 1");
	$st->bindValue(':idp',$def['id_product']);
	try { $st->execute(); } catch ( Exception $e ) { die($e); }
	$jest = $st->fetch(PDO::FETCH_ASSOC);
	
	//if there is product with quantity
	if ($jest['id_product_attribute'] > 0) {
		
		//update old default_on to NULL in ps_product_attribute and ps_product_attribute_shop
		$st = $db->prepare("update ps_product_attribute set default_on=NULL where id_product_attribute=:idpa");
		$st->bindValue(':idpa',$def['id_product_attribute']);
		$st = $db->prepare("update ps_product_attribute_shop set default_on=NULL where id_product_attribute=:idpa");
		$st->bindValue(':idpa',$def['id_product_attribute']);
		try { $st->execute(); } catch ( Exception $e ) { die($e); }
		//update default_on to 1 in ps_product_attribute and in ps_product_attribute_shop for attribute with quantity>0
		$st = $db->prepare("update ps_product_attribute set default_on=1 where id_product_attribute=:idpa");
		$st->bindValue(':idpa',$jest['id_product_attribute']);
		$st = $db->prepare("update ps_product_attribute_shop set default_on=1 where id_product_attribute=:idpa");
		$st->bindValue(':idpa',$jest['id_product_attribute']);
		try { $st->execute(); } catch ( Exception $e ) { die($e); }
	}
	
}

 

Link to comment
Share on other sites

  • 1 year later...
  • 2 months later...

Thanks a lot. Could you please add cron support. ))) 

This issue often firing when importing large catalogs from ERP`s, Accounting softwares, direct DB uploads. 

So it will be very useful for many users i think. 

Products Uploaded, Then Runs Cron-Task. Like at Installation step. Scanning all products. 

 

 

Link to comment
Share on other sites

  • 5 months later...
  • 5 months later...
  • 3 years later...

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...