Jump to content

[PHP] How to select only products which are set as Active?


Attrexx
 Share

Recommended Posts

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 by Attrexx
Shorten the code snippet for legibility (see edit history)

Share this post


Link to post
Share on other sites

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 by hakeryk2 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

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 by hakeryk2 (see edit history)

Share this post


Link to post
Share on other sites

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)?

Share this post


Link to post
Share on other sites

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 :(

Share this post


Link to post
Share on other sites

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 by hakeryk2 (see edit history)

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More