Attrexx Posted October 21, 2017 Share Posted October 21, 2017 (edited) Hello all. I am trying to set up a feed for the Facebook Product Catalog. My problem is that my current feed lists products based on their stock only (>0). But this is not very good for clients that retire products from the store by disabling them in Back End, without setting their stock to 0 (for example out-of-season products). I want to mix the two conditions so only products with Stock > 0 and Status = Active are listed in the resulting CSV file. Below is my current markup. The second condition is not working at all. Also, I know that description_short and description are the same. That is intentional ... foreach ($products as $product) { if( StockAvailable::getQuantityAvailableByProduct($product['id_product']) >0 && $product->active = 1){ $p = new Product($product['id_product']); $line = []; ... Edited October 21, 2017 by Attrexx Shorten the code snippet for legibility (see edit history) Link to comment Share on other sites More sharing options...
hakeryk2 Posted October 21, 2017 Share Posted October 21, 2017 (edited) Could You tell how did you obtain $products? Is it by $category->getNewProducts() or is it by Product::getProducts() Both of them have params to select only active products. For example: $products = Product::getProducts($id_lang, $start, $limit, $order_by, $order_way, $id_category = false, $only_active = false, Context $context = null) So as You see there is a param $only_active which You need to set to true. For example like this $products = Product::getProducts($this->context->cookie->id_lang, 0, 5000, 'name', 'ASC', 50, true); which is taking 5000 products, sorted by name ascending, from category id 50 and the last true param is only for active ones. Othervise You should see what your $product in foreach loop looks like by this: foreach ($products as $product) { ddd($product); // this will stop your loop and show you content of your variable if( StockAvailable::getQuantityAvailableByProduct($product['id_product']) >0 && $product->active = 1){ $p = new Product($product['id_product']); $line = []; Edited October 21, 2017 by hakeryk2 (see edit history) 2 Link to comment Share on other sites More sharing options...
Attrexx Posted October 21, 2017 Author Share Posted October 21, 2017 I get them like this: $products = Product::getSimpleProducts($id_language); PHP is not my strong suit. Link to comment Share on other sites More sharing options...
hakeryk2 Posted October 21, 2017 Share Posted October 21, 2017 (edited) getSimpleProducts is getting You only id_product and name of product in array so that is why Your $product->active is not working because You should have in result active record which You dont have. Otherwise: you are using $product['id_product'] which is refer to array but few moments later You are using object $product->active which is not present until You did not create new product from information that You provided. Edited October 21, 2017 by hakeryk2 (see edit history) Link to comment Share on other sites More sharing options...
Attrexx Posted October 21, 2017 Author Share Posted October 21, 2017 I can see where I made the mistakes but to avoid a messy correction, could you please help me verify it? I get $products like this now: $products = Product::getProducts($this->context->cookie->id_lang, 0, null, 'name', 'ASC', 50, true); And I removed this from below in the file: && $product->active = 1 How can I get all categories in the first array (where I have the id 50 now)? Link to comment Share on other sites More sharing options...
hakeryk2 Posted October 21, 2017 Share Posted October 21, 2017 Just give it false parameter $products = Product::getProducts($this->context->cookie->id_lang, 0, null, 'name', 'ASC', false, true); Link to comment Share on other sites More sharing options...
Attrexx Posted October 21, 2017 Author Share Posted October 21, 2017 I get error 500 no csv generated. This line breaks it. The whole file is here (live edit): https://codeshare.io/29pMdM Link to comment Share on other sites More sharing options...
Attrexx Posted October 21, 2017 Author Share Posted October 21, 2017 Above that line I have $id_language = (int) Context::getContext()->language->id; so I tried this instead $products = Product::getProducts($id_language, 0, null, 'name', 'ASC', false, true); but the CSV is corrupted Link to comment Share on other sites More sharing options...
hakeryk2 Posted October 23, 2017 Share Posted October 23, 2017 (edited) In Your code this should do the trick beause You are not using this module in context. This is kinda bad but it will works for You. function getAllProducts($only_active = false) { if ($only_active) { $active = ' WHERE active = 1'; } $sql = ''; $sql = 'SELECT id_product FROM '._DB_PREFIX_.'product '.$active; return Db::getInstance()->executeS($sql); } $context = Context::getContext()->getContext(); $products = getAllProducts(true); Edited October 23, 2017 by hakeryk2 (see edit history) 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