Zen Cart Logo
Forums / Zen Cart Code Suggestions / products master_category_id = 0

products master_category_id = 0

Views: 363

Results 1 to 6 of 6
14 Sep 2016, 14:21
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

products master_category_id = 0

I have come across this a few times over the years.
Of course it should never happen and most likely it is due to something I have done or some mod I have used.
Nonetheless there are a few threads mentioning this and for those with nothing better to do, a quick sort of the products table in phpmyadmin on master_categories_id may be of academic interest to see if you have any products with this problem.

In order to spot this when it happens and identify the action that causes it, I have stuck this in the admin/includes/header.php.
It may be of use to others who find they have some products with a missing master category and would like to know why.

"Caveat Emptor" as always/on a development server only etc...

//steve check that master_categories_id !=0
$master_categories_bad_sql = "SELECT products_id, products_model FROM " . TABLE_PRODUCTS . " WHERE master_categories_id = 0";
$master_categories_bad = $db->Execute($master_categories_bad_sql);
if ($master_categories_bad->count() > 0) {
    foreach ($master_categories_bad as $product_row) {
        $categories_linked_sql = "SELECT categories_id FROM " . TABLE_PRODUCTS_TO_CATEGORIES . " WHERE products_id = " . $product_row['products_id'];
        $categories_linked = $db->Execute($categories_linked_sql);
        $category_urls = '';
        $cat_count = 1;
        foreach ($categories_linked as $category_row) {
            $category_linked_name = zen_output_generated_category_path($category_row['categories_id']);
            $category_linked_path = implode("_", array_reverse(explode("_", zen_get_generated_category_path_ids
            ($category_row['categories_id']))));
            $category_urls .= '<br />' . $cat_count . ' <a href="categories.php?cPath=' . $category_linked_path . '" target="_blank" title="Open Category in a new tab">' . $category_linked_name . '</a>';
            $cat_count++;
        }
        $master_category_bad_message = '<b>' . $product_row['products_id'] . ' ' . $product_row['products_model'] . '</b>' . $category_urls;
        $messageStack->add('Master Categories Id=0 for: ' . $master_category_bad_message, 'error');
    }
}
//eof

// display alerts/error messages, if any

and example output in the messageStack header:
Attachment 16659

14 Sep 2016, 16:50
#2
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

Re: products master_category_id = 0

i have encountered this problem.

unfortunately, it rarely, if ever, happens on a development server. its always on a live server and who knows what happened....

i like this idea, but instead of adding it to a messageStack, why not use the ZC logger and log it to a file? that way you can include the code, users are none-too-wiser and perhaps we can track down what the heck the user is doing?

i will look into implementing as well....

14 Sep 2016, 17:55
#3
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Re: products master_category_id = 0

I meant to try out the code on a dev server first (assuming a current db copy is loaded) to show up the problem if there is one.

I assume that this issue occurs after some imperfect import/something I am doing and not a user action and so I was interested in an in-my-face report. A debug log would be more useful if these things happen for other reasons at other times.

26 Sep 2016, 05:29
#5
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

Re: products master_category_id = 0

so you are saying that this problem occurs because you modified some code in the product creation?

i have not experienced it of late, but i did not make those changes....

at some point perhaps i will track down how it is happening on my clients sites....

26 Sep 2016, 07:33
#6
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Re: products master_category_id = 0

Ok, just to spell it out, but it is circumstantial information to this thread.

Edit a product.
Pass to the Preview page.
Save/Pass the Preview page.
Do something else in the admin for a few pages.
Go BACK using the browser navigation intending to re-edit product BUT land on the Preview page instead by mistake: at this point the browser should warn you that the form needs to be re-submitted, which should give you a heads up that you are not in the right place.
Certainly that was the case in my testing, but I know I have landed on that page without the warning in the past.
At this point all the product info that should be contained in the POST variable is missing and so if you save/accept the page, you will get an empty product. But that should be obvious as you can see in the Preview page that there is no data.
But, if you have a hack in place to autosubmit the preview page as you are too lazy to scroll and click every time, like me, then the product data gets overwritten with nothing in its fields.

Since this is at a tangent to this thread I referenced the post to the relevant thread.

This is one potential cause on my site, but still not the one I am looking out for.
I have had products with all the data fields intact but master categories zero so that does not come from this issue unless the form POST data is intact and the GET parameters are missing the cPath.