ruisonika Posted March 22, 2022 Share Posted March 22, 2022 Hello guys, I'm with the following issue: i have an external software that sincronizes with prestashop, so i write in prestashop database with success... i'm trying to associate products with categories and all works good in database there is my product (id) and my categories (id) and in backoffice all is show according but in front (website store) isn't... to fix this i have to go in admin in product categories and click in save button at the end (how can i simulate this submit form in my code). $stockOffVar = $_POST["tipodesc"]; // if tipodesc equals Outlet50 then associates to category 3470 if ($stockOffVar == 'Outlet50'){ $sql = "INSERT INTO ps_category_product(id_category, id_product, position) VALUES (3470,".$id_product.",1)"; Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); }elseif ($stockOffVar != 'Outlet50'){ $sql = "DELETE FROM ps_category_product WHERE ps_category_product.id_category = 3470 AND ps_category_product.id_product = $id_product"; Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); }; Link to comment Share on other sites More sharing options...
knacky Posted March 24, 2022 Share Posted March 24, 2022 $product = new Product((int)$id_product); $product->update(); 1 Link to comment Share on other sites More sharing options...
Ress Posted March 25, 2022 Share Posted March 25, 2022 You can call this, and you won't need that save anymore. Cache::clean('Product::getProductCategories_' . (int) $id_product); 1 Link to comment Share on other sites More sharing options...
ruisonika Posted March 28, 2022 Author Share Posted March 28, 2022 On 3/24/2022 at 7:27 AM, knacky said: $product = new Product((int)$id_product); $product->update(); doesn't work Link to comment Share on other sites More sharing options...
knacky Posted March 28, 2022 Share Posted March 28, 2022 (edited) It is better to use Prestashop functions to edit the category association. $product = new Product((int)$id_product); $getCategories = Product::getProductCategories((int)$id_product); $categories = array(); $stockOffVar = $_POST["tipodesc"]; $keeping_current_pos = false; $clean_positions = true; if ($stockOffVar == 'Outlet50'){ $categories[] = '3470'; foreach ($getCategories as $category){ $categories[] = $category['id_category']; } $product->updateCategories($categories, $keeping_current_pos); } else { $product->deleteCategory('3470', $clean_positions); } Edited March 28, 2022 by knacky (see edit history) 2 Link to comment Share on other sites More sharing options...
ruisonika Posted March 30, 2022 Author Share Posted March 30, 2022 On 3/28/2022 at 2:31 PM, knacky said: It is better to use Prestashop functions to edit the category association. $product = new Product((int)$id_product); $getCategories = Product::getProductCategories((int)$id_product); $categories = array(); $stockOffVar = $_POST["tipodesc"]; $keeping_current_pos = false; $clean_positions = true; if ($stockOffVar == 'Outlet50'){ $categories[] = '3470'; foreach ($getCategories as $category){ $categories[] = $category['id_category']; } $product->updateCategories($categories, $keeping_current_pos); } else { $product->deleteCategory('3470', $clean_positions); } Thanks for your reply... Your code works fine but erases all categories associated with the product, and i only wanna add or delete one the id_category 3470... But this isn't the issue...Because your way or my way the results are fine in database (all OK)... The problem is when this code runs (when stockOffVar is equal to Outlet50) should ended with this behavior: -when you are in presta admin area, in product categories associated when you checked something new to associate then you must click in save button, and this is the only thing than i can't accomplish ... I have an invisible category Outlet50 and some product are in, and in promotions catalogue i say all products in that category have 50% discount. My website (prestashop) is populated by other logistics software and between both is another realtime synchronize api that connects prestashop database to logistics database. So all my products and categories the origin is in logistics software, and prestashop orders at the end sync to the logistics... So in my code that writes de category product from logistics to the prestashop database all works well, but in front (shop) my product doesn't shows 50% discount unless i go to the backoffice prestashop produc categories associations and click save button at the end... Understand my issue? Thank you all Link to comment Share on other sites More sharing options...
knacky Posted March 30, 2022 Share Posted March 30, 2022 I understand. And you can't edit what I wrote you? Do you only use this code? Did you add $product->update(); to the end ? The $product->update() function is what Prestashop does when you click the "Update" button in the administration. Explore ./classes/Product.php You can also use Hook :: exec. Hook::exec('actionProductUpdate', ['id_product' => (int) $product->id, 'product' => $product]); Link to comment Share on other sites More sharing options...
knacky Posted March 30, 2022 Share Posted March 30, 2022 Do you use any cache module? Link to comment Share on other sites More sharing options...
Ress Posted March 30, 2022 Share Posted March 30, 2022 Did you try to call this function after updating product categories from code? Cache::clean('Product::getProductCategories_' . (int) $id_product); Link to comment Share on other sites More sharing options...
knacky Posted March 30, 2022 Share Posted March 30, 2022 The clean cache function is built into the updateCategories and deleteCategory functions. See Product.php Link to comment Share on other sites More sharing options...
Ress Posted March 30, 2022 Share Posted March 30, 2022 Yes you are right. However, now I read more carefully, and the problem with it is that the discount of the added category does not apply. From what I see in the code,in the function for deleting the category is called SpecificPriceRule :: applyAllRules ([(int) $ this-> id]), and in the function of adding categories, it is not called. Maybe the problem is from here, and he would call SpecificPriceRule :: applyAllRules ([(int) $ this-> id]) after add categories to product, to refresh specific prices. 2 Link to comment Share on other sites More sharing options...
knacky Posted March 30, 2022 Share Posted March 30, 2022 Yes, and he mentioned the discount only now. Categories are being addressed all the time, but the problem is elsewhere. Everyone thinks we see all the code or our own functions and we can reveal what they think but don't write. 1 Link to comment Share on other sites More sharing options...
ruisonika Posted March 31, 2022 Author Share Posted March 31, 2022 12 hours ago, knacky said: Yes, and he mentioned the discount only now. Categories are being addressed all the time, but the problem is elsewhere. Everyone thinks we see all the code or our own functions and we can reveal what they think but don't write. Sorry guys ... for that and by the way thanks for all you hep. So starting i'm explain what i wanna achieve. In prestashop admin area i have several products and associated in several categories. I've created a special category named Outlet50 (is not active) so customer doesn't see. I've created a catalogue rule that gives 50% discount all the product associated in category Outlet50. When i associated the product do that category and click in save button, in font (shop) the product has 50% discount and all works well. So i'm trying to achieve this with code because my products come from another logistics software. And i with success insert products, categories, prices ...everything with my file that connects prestashop database with logistics database. So in my file i started with this code <?php include './config/settings.inc.php'; include './config/defines.inc.php'; include './config/config.inc.php'; require_once(_PS_MODULE_DIR_.'ptsverticalmenu/classes/BtVerticalMegamenu.php'); include './init.php'; include './classes/Product.php'; and after with the code in image because it blocks when i put the code here. Link to comment Share on other sites More sharing options...
Ress Posted March 31, 2022 Share Posted March 31, 2022 Can you attach the image again? It doesn't load. Link to comment Share on other sites More sharing options...
ruisonika Posted March 31, 2022 Author Share Posted March 31, 2022 2 hours ago, Ress said: Can you attach the image again? It doesn't load. code.pdf Link to comment Share on other sites More sharing options...
Ress Posted March 31, 2022 Share Posted March 31, 2022 I looked. So, if the discounts don't apply to that category, try to run this line of code after you add the category to the product. SpecificPriceRule::applyAllRules([(int)$id_product]); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now