Jump to content

CSV Products export doesn't export Category ID


tumil

Recommended Posts

Hi

https://github.com/PremiumPresta/export-products-for-csv-import-prestashop-1.6/blob/master/override/controllers/admin/AdminProductsController.php

 

Using the code from the link above I managed to get some additional info in the csv file I export through B/O>Products.

Everything is working fine except this fragment:

        // Categories (x,y,z...)
        $this->fields_list['name_category'] = array(
            'title' => $this->l('Categories (x,y,z...)'),
            'callback' => 'exportAllProductCategories', // categories name
            // 'callback' => 'exportAllProductCategoriesId' // categories id
        );

Here it is commented out but in my version I just want the ids to be exported, not the names.

And it works fine if a product has multiple categories but if it has one, it exports nothing.

Here's a more detailed code:

public static function exportAllProductCategoriesId($defaultCategory, $row, $delimiter = ',')
    {
        if (empty($row) || empty($row['id_product'])) return;
        $id_product = (int) $row['id_product'];
        $id_shop = Context::getContext()->shop->id;
        $query = new DbQuery();
        $query->select('c.id_category, p.id_category_default')->from('category', 'c');
        $query->leftJoin('category_shop', 'cs', 'c.id_category = cs.id_category AND cs.id_shop = ' . $id_shop);
        $query->leftJoin('category_product', 'cp', 'c.id_category = cp.id_category AND cp.id_product = ' . $id_product);
        $query->leftJoin('product', 'p', 'cp.id_product = p.id_product');
        $query->where('p.id_category_default != c.id_category');
        $categories = array();
        foreach (Db::getInstance()->executeS($query) as $category) {
            if (!count($categories)) {
                $categories[] = $category['id_category_default']; // the first category is the default one
            }
            $categories[] = $category['id_category'];
        }
        return implode($delimiter, $categories);
    }

Any ideas how to fix it?

 

Thanks for help!

Link to comment
Share on other sites

You should leave out 

$query->where('p.id_category_default != c.id_category');

You should also leave out

        if (!count($categories)) {
                $categories[] = $category['id_category_default']; // the first category is the default one
            }

And before the Foreach line you should add

$categories[] = $category['id_category_default'];
  • Like 1
Link to comment
Share on other sites

Thank you for your reply, but unfortunetaly it didn't solve the problem

Now the field for EVERY product looks like this:

,1,2,218,219,220,221,222,223,224,421,434,497,537,555,561,33,35,36,37,38,42,44,190,225,226,227,228,229,230,231,232,233,234,235,236,237,238,355,356,357,358,359,360,361,362,363,387,388,389,390,391,392,393,394,395,396,397,398,399,400,412,419,420,422,423,424,425,426,427,428,435,436,437,438,439,440,441,442,504,511,512,521,538,539,540,541,542,543,544,547,548,550,551,562,563,564,566,567,568,569,570,571,50,51,191,192,193,194,195,196,197,198,199,239,240,241,242,243,244,245,246,247,248,249,250,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,333,334,335,336,337,338,339,340,341,342,343,345,346,347,348,349,350,351,352,353,354,364,365,370,371,383,384,385,386,401,402,403,404,405,406,407,408,409,413,414,415,416,417,418,429,431,433,443,444,445,446,447,448,449,451,452,453,454,456,469,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,499,500,501,502,503,505,506,507,508,509,510,513,514,515,516,517,518,519,520,522,523,524,525,526,527,528,529,530,531,532,534,535,536,545,546,549,552,554,556,557,558,559,560,572,573,574,575,576,577,201,202,203,204,366,367,368,369,372,373,374,375,376,377,378,379,380,381,382

which is pretty much all the categories in the store. What's interesting - field starts with delimiter now

Link to comment
Share on other sites

I see.

The main problem in the original code is this line:

$query->where('p.id_category_default != c.id_category');

The effect is that if the default category is the only category nothing is selected. And if nothing is selected you never come in the foreach loop and the default isn't added.

However, when we delete that everything is selected. I am not exactly sure how to solve that. My suggestion is to add the product once again in the where clause:

where cp.id_product = ' . $id_product

My line before the foreach line wasn't well thought of. It copies from an empty categories array on itself.

Instead we need to adapt the foreach loop so that it puts the default category first and doesn't repeat it. So it will become something like this:

        foreach (Db::getInstance()->executeS($query) as $category) {
            if (!count($categories)) {
                $categories[] = $defcat = $category['id_category_default']; // the first category is the default one
            }
            if($category['id_category'] != $defcat)
               $categories[] = $category['id_category'];
        }
  • Like 1
Link to comment
Share on other sites

So... you suggest not to delete that line

$query->where('p.id_category_default != c.id_category');

but to add the product in the where clause later on?

I'm not exactly sure what you mean. PHP isn't really my thing and I'm acting kind of blindly

Link to comment
Share on other sites

12 minutes ago, tumil said:

So... you suggest not to delete that line


$query->where('p.id_category_default != c.id_category');

but to add the product in the where clause later on?

I'm not exactly sure what you mean. PHP isn't really my thing and I'm acting kind of blindly

No. You should delete that line. However, you should replace it with the other "where" line that I showed.

This is more Mysql than php and I recommend you strongly to take some time to study the code - trying to understand te logic.

 

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