Jump to content

Set default combination programmatically


dhobo

Recommended Posts

Hi,

 

I have some code to insert product combinations, this all works normally. But I noticed that the default combination is never set.

So I decided to create short script that will do just that for me, but even after running this script; in the backend it doesn't show me what is my default combination.

Can anyone see what I am missing here?

 // now get all the products where there are no default combinations
            $sql = "SELECT DISTINCT id_product, count(*) as total FROM `ps_product_attribute` GROUP BY id_product";
            $data = Db::getInstance()->executeS($sql);
            foreach($data as $row){
                $sql = "SELECT id_product_attribute FROM ps_product_attribute WHERE id_product = {$row['id_product']} AND default_on = 0";
                $result = Db::getInstance()->executeS($sql);
                if (count($result) == $row['total']){
                    $sql = "UPDATE ps_product_attribute SET default_on = 1 WHERE id_product_attribute = ".$result[0]['id_product_attribute'];
                    Db::getInstance()->execute($sql);
                    
                    $sql = "UPDATE ps_product_attribute_shop SET default_on = 1 WHERE id_product_attribute = ".$result[0]['id_product_attribute'];
                    Db::getInstance()->execute($sql);
                    
                    $sql = "UPDATE ps_product SET cache_default_attribute = ".$result[0]['id_product_attribute']." WHERE id_product = ".$row['id_product'];
                    Db::getInstance()->execute($sql);
                }
            }
            Search::indexation(1);
Link to comment
Share on other sites

I had done something similar sometime ago, if i remember right (but I don't a have a database to check at the moment) there should be also another table in where the default combination is setted... ps_product_attribute_combinations (and the _shop version) maybe?

as far as I can check, I'll take a look.

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