Jump to content

Assign product to categories into DB using php script/sql (PS 1.6)


Pier

Recommended Posts

Hi everyone.

My company is migrating its shop on PrestaShop 1.6.1.6.

We have about 1500 products. I created a category "discountable" in wich I assigned the products that can have specific prices, the others can't be and will always be sold the same price. So we have 2 status for our products: discountable and net price.

To update products informations we could have used PrestaShop's native import function but we have differences between descriptions etc so I created a PHP script to update products prices and status directly in DB using a CSV file.

It's working "fine" except for the category association. In Back-Office the product is correctly assigned, but when I visit my shop, the product sometimes is discountable when it should not or not discountable when it should. When I edit my product in BO and save it, it seems to "update" the categories and then it's working as expected.

I tried to clear cache, rebuild index etc.
I'd like to know wich is the function that "update" the categories when a product is saved, I haven't found it yet. Would it be possible to call it in my script?

Ex of product assigned in the "discountable" (remisables) category but no discount is applied:

- Back-Offfice:

bo1.jpg.2c5ead79461d507ab34d1873db6a1c4c.jpg

- Front:

front1.jpg.44356231d8535a97169994c62c34a402.jpg

- Just after pressing "save" in BO:

front2.jpg.c7cb046e793a29a548650bd20e2e58ed.jpg

Portion of my script related to this category (id 60):

//if file status != db status
if ($remisable != $inDbRemisable) {
	if ($remisable == 1) {
		//Generating "position" field value
		$getPosSql = "SELECT position from "._DB_PREFIX_."category_product WHERE id_category = 60 ORDER BY position DESC LIMIT 1";
		$getPosRES = $db->ExecuteS($getPosSql);
		$position = $getPosRES[0]['position'] + 1;

		//Adding category to product
		$addRemisSQL = "INSERT INTO "._DB_PREFIX_."category_product (id_category, id_product, position) VALUES (60, ".$id_product.", ".$position.")";
		$db->ExecuteS($addRemisSQL);
	}
	elseif ($remisable == 0) {
		//removing category to product
		$delRemisSQL = "DELETE FROM "._DB_PREFIX_."category_product WHERE id_category = 60 AND id_product = ".$id_product."";
		$db->ExecuteS($delRemisSQL);
	}
}

I don't know if i'm clear enough, sorry for my English ;)

 

edit: I found a fix, problem is related to the ps_specific_price table wich is generated when you create the rule OR updated when you save a product. Just edit a rule and save it and it will "update" all the products.

Edited by Pier (see edit history)
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...