Jump to content

Notice: Trying To Get Property Of Non-Object


Stéphane1405
 Share

Recommended Posts

Hello everyone,

 

Here is my code :


define('PRESTASHOP_S_EXTERNAL_SCRIPT', true);
include(dirname(__FILE__).'\..\prestashop_1.6.1.0\prestashop\config\config.inc.php');


echo Configuration::get('PS_LANG_DEFAULT');


$id_category = 123;

$category = new Category($id_category, Configuration::get('PS_LANG_DEFAULT'));

//product count
echo $category->getProducts(1,1,1000,null,null,1,1);


I get the default language id , the number of products of category 123 but in between I get a notice "trying to get property of non objet" even though PHP is_object($category) returns TRUE.

 

Here is the screen I get :

 

 

 

1

( ! ) Notice: Trying to get property of non-object in prestashop_1.6.1.0\prestashop\classes\Category.php on line 671
Call Stack
#    Time    Memory    Function    Location
1    0.0006    247584    {main}( )    ..\mon_script.php:0
2    0.0849    12151800    CategoryCore->getProducts( )    ..\mon_script.php:25


8

 

 

 

 

I have got not clue, so your help will be greatly appreciated.

 

Thanks for reading,

 

Stéphane

Edited by Stéphane1405 (see edit history)

Share this post


Link to post
Share on other sites

I will first try to isolate it to determine if its caused by:

$category = new Category($id_category, Configuration::get('PS_LANG_DEFAULT'));

or by:

echo $category->getProducts(1,1,1000,null,null,1,1);

 

So comment one of them and run the script again.

 

I would change the 1st one to:

$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'));

and the 2nd one to:

echo $category->getProducts(1,1,1000);

 

Look at the getProducts function:

public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)

 

What is the purpose of '1,1' after the null? if you are not changing default values don't call it.

Share this post


Link to post
Share on other sites

Thanks a lot for your precise answer.

I tried what you suggested and I still get the notice.

 

I also have a problem with the getName() property that returns just 1 letter :

define('PRESTASHOP_S_EXTERNAL_SCRIPT', true);
include(dirname(__FILE__).'\..\prestashop_1.6.1.0\prestashop\config\config.inc.php');


echo Configuration::get('PS_LANG_DEFAULT');
echo "<br/>";

$id_category = 123;

$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'),(int)Configuration::get('PS_SHOP_DEFAULT'));


//that works
echo $category->name;
echo "<br/>";


//returns one letter
echo $category->getName(1);
echo "<br/>";


//returns number of products but with PHP notice
echo $category->getProducts(1,1,1000,null,null,1,1);
echo "<br/>";




returns

1
category name
a

( ! ) Notice: Trying to get property of non-object in prestashop_1.6.1.0\prestashop\classes\Category.php on line 671
Call Stack
#    Time    Memory    Function    Location
1    0.0006    248448    {main}( )    ..\presta_desactive_cat_vides.php:0
2    0.0962    12152432    CategoryCore->getProducts( )    ..\presta_desactive_cat_vides.php:34
8

Any suggestion would be greatly appreciated.

Edited by Stéphane1405 (see edit history)

Share this post


Link to post
Share on other sites

Thanks yaniv for your time.

 

Unfortunately the name of the category is not 'o'  :-(

 

I haved added getName() and var_dump ($category) here is my code and HTML output.

 

PHP code

<?php


define('PRESTASHOP_S_EXTERNAL_SCRIPT', true);
include(dirname(__FILE__).'\..\prestashop_1.6.1.0\prestashop\config\config.inc.php');


echo Configuration::get('PS_LANG_DEFAULT');
echo "<br/>";



$id_category = 117;

$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'),(int)Configuration::get('PS_SHOP_DEFAULT'));


//works
echo $category->name;
echo "<br/>";

//returns just one letter
echo $category->getName(1);
echo "<br/>";

//returns the correct number bu with PHP notice
echo $category->getProducts(1,1,1000,null,null,1,1);
echo " products<br/>";





var_dump($category);



HTML output

1
right category name
a

( ! ) Notice: Trying to get property of non-object in C:\wamp\www\NBS\preprod\prestashop_1.6.1.0\prestashop\classes\Category.php on line 671
Call Stack
#TimeMemoryFunctionLocation
10.0007250160{main}( )..\presta_desactive_cat_vides.php:0
20.096612154384CategoryCore->getProducts( )..\presta_desactive_cat_vides.php:36

8 products



object(Category)[20]
public 'id' => int 117
public 'id_category' => string '117' (length=3)
public 'name' => string 'Haut Médoc' (length=11)
public 'active' => string '1' (length=1)
public 'position' => string '25' (length=2)
public 'description' => string '' (length=0)
public 'id_parent' => string '36' (length=2)
public 'id_category_default' => null
public 'level_depth' => string '4' (length=1)
public 'nleft' => string '55' (length=2)
public 'nright' => string '56' (length=2)
public 'link_rewrite' => string 'haut-medoc' (length=10)
public 'meta_title' => string '' (length=0)
public 'meta_keywords' => string '' (length=0)
public 'meta_description' => string '' (length=0)
public 'date_add' => string '2016-01-06 20:29'... (length=19)
public 'date_upd' => string '2016-01-06 20:29'... (length=19)
public 'is_root_category' => string '0' (length=1)
public 'id_shop_default' => string '1' (length=1)
public 'groupBox' => null
public 'id_image' => boolean false
protected 'webserviceParameters' =>
array (size=4)
'objectsNodeName' => string 'categories' (length=10)
'hidden_fields' =>
array (size=3)
0 => string 'nleft' (length=5)
1 => string 'nright' (length=6)
2 => string 'groupBox' (length=8)
'fields' =>
array (size=3)
'id_parent' =>
array (size=1)
'xlink_resource' => string 'categories' (length=10)
'level_depth' =>
array (size=1)
'setter' => boolean false
'nb_products_recursive' =>
array (size=2)
'getter' => string 'getWsNbProductsR'... (length=24)
'setter' => boolean false
'associations' =>
array (size=2)
'categories' =>
array (size=2)
'getter' => string 'getChildrenWs' (length=13)
'resource' => string 'category' (length=8)
'products' =>
array (size=2)
'getter' => string 'getProductsWs' (length=13)
'resource' => string 'product' (length=7)
protected 'id_lang' => int 1
protected 'id_shop' => int 1
public 'id_shop_list' => null
protected 'get_shop_from_context' => boolean false
protected 'table' => string 'category' (length=8)
protected 'identifier' => string 'id_category' (length=11)
protected 'fieldsRequired' =>
array (size=1)
0 => string 'active' (length=6)
protected 'fieldsSize' =>
array (size=0)
empty
protected 'fieldsValidate' =>
array (size=9)
'nleft' => string 'isUnsignedInt' (length=13)
'nright' => string 'isUnsignedInt' (length=13)
'level_depth' => string 'isUnsignedInt' (length=13)
'active' => string 'isBool' (length=6)
'id_parent' => string 'isUnsignedInt' (length=13)
'id_shop_default' => string 'isUnsignedId' (length=12)
'is_root_category' => string 'isBool' (length=6)
'date_add' => string 'isDate' (length=6)
'date_upd' => string 'isDate' (length=6)
protected 'fieldsRequiredLang' =>
array (size=2)
0 => string 'name' (length=4)
1 => string 'link_rewrite' (length=12)
protected 'fieldsSizeLang' =>
array (size=5)
'name' => int 128
'link_rewrite' => int 128
'meta_title' => int 128
'meta_description' => int 255
'meta_keywords' => int 255
protected 'fieldsValidateLang' =>
array (size=6)
'name' => string 'isCatalogName' (length=13)
'link_rewrite' => string 'isLinkRewrite' (length=13)
'description' => string 'isCleanHtml' (length=11)
'meta_title' => string 'isGenericName' (length=13)
'meta_description' => string 'isGenericName' (length=13)
'meta_keywords' => string 'isGenericName' (length=13)
protected 'tables' =>
array (size=0)
empty
protected 'image_dir' => string 'C:\wamp\www\NBS\'... (length=60)
protected 'image_format' => string 'jpg' (length=3)
protected 'def' =>
array (size=7)
'table' => string 'category' (length=8)
'primary' => string 'id_category' (length=11)
'multilang' => boolean true
'multilang_shop' => boolean true
'fields' =>
array (size=16)
'nleft' =>
array (size=2)
'type' => int 1
'validate' => string 'isUnsignedInt' (length=13)
'nright' =>
array (size=2)
'type' => int 1
'validate' => string 'isUnsignedInt' (length=13)
'level_depth' =>
array (size=2)
'type' => int 1
'validate' => string 'isUnsignedInt' (length=13)
'active' =>
array (size=3)
'type' => int 2
'validate' => string 'isBool' (length=6)
'required' => boolean true
'id_parent' =>
array (size=2)
'type' => int 1
'validate' => string 'isUnsignedInt' (length=13)
'id_shop_default' =>
array (size=2)
'type' => int 1
'validate' => string 'isUnsignedId' (length=12)
'is_root_category' =>
array (size=2)
'type' => int 2
'validate' => string 'isBool' (length=6)
'position' =>
array (size=1)
'type' => int 1
'date_add' =>
array (size=2)
'type' => int 5
'validate' => string 'isDate' (length=6)
'date_upd' =>
array (size=2)
'type' => int 5
'validate' => string 'isDate' (length=6)
'name' =>
array (size=5)
'type' => int 3
'lang' => boolean true
'validate' => string 'isCatalogName' (length=13)
'required' => boolean true
'size' => int 128
'link_rewrite' =>
array (size=5)
'type' => int 3
'lang' => boolean true
'validate' => string 'isLinkRewrite' (length=13)
'required' => boolean true
'size' => int 128
'description' =>
array (size=3)
'type' => int 6
'lang' => boolean true
'validate' => string 'isCleanHtml' (length=11)
'meta_title' =>
array (size=4)
'type' => int 3
'lang' => boolean true
'validate' => string 'isGenericName' (length=13)
'size' => int 128
'meta_description' =>
array (size=4)
'type' => int 3
'lang' => boolean true
'validate' => string 'isGenericName' (length=13)
'size' => int 255
'meta_keywords' =>
array (size=4)
'type' => int 3
'lang' => boolean true
'validate' => string 'isGenericName' (length=13)
'size' => int 255
'classname' => string 'Category' (length=8)
'associations' =>
array (size=1)
'l' =>
array (size=3)
'type' => int 2
'field' => string 'id_category' (length=11)
'foreign_field' => string 'id_category' (length=11)
protected 'update_fields' => null
public 'force_id' => boolean false

I hope somebody will have an idea.

Share this post


Link to post
Share on other sites

thanks yaniv,

 

from Category.php :

    public function getName($id_lang = null)
    {
        if (!$id_lang)
        {
            if (isset($this->name[Context::getContext()->language->id]))
                $id_lang = Context::getContext()->language->id;
            else
                $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
        }
        return isset($this->name[$id_lang]) ? $this->name[$id_lang] : '';
    }
Edited by Stéphane1405 (see edit history)

Share this post


Link to post
Share on other sites

I think I understand where is the problem.

You are trying to instance category with:

$category = new Category($id_category, (int)Configuration::get('PS_LANG_DEFAULT'),(int)Configuration::get('PS_SHOP_DEFAULT'));

and I think you need to instance it only by ID, there is no need for lang and shop because this is only for the object.

 

can you try:

$category = new Category($id_category);

Share this post


Link to post
Share on other sites

  • 3 years later...

I have the same problem when I try to retrieve the contact email in the .tpl

prestashop\admin\themes\default\template\controllers\customer_threads\helpers\view\view.tpl

the .php file should be

prestashop\controllers\admin\AdminCustomerThreadsController.php

 

{if $contact->email == '[email protected]' }
......
...
...
{else}
{/if}

 

from debugging this error comes out and therefore "if" has no effect

[8] Trying to get property 'email' of non-object

 

Can someone kindly help me?

Thanks to those who will respond.

 

 

 

 

 

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