Jump to content

Why I can't print a value found with the debuger?


Graphics Back Label

Recommended Posts

Hi Prestashoppers,

I use the debuger to find some values to print out on my template (I use Smarty).

The debuger find a value (the material) of my product and when I try with the code below doesn't work.

Product name:

<h5 itemprop="name">
{if isset($product.pack_quantity) && $product.pack_quantity}{$product.pack_quantity|intval|cat:' x '}{/if}
<a class="product-name" href="{$product.link|escape:'html':'UTF-8'}" title="{$product.name|escape:'html':'UTF-8'}" itemprop="url" >
{$product.name|truncate:45:'...'|escape:'html':'UTF-8'}{$features.value}

// THERE IS THE CODE //
<p>{$products->features}</p> 

// I TRIED WITH THIS TOO //
<p>{features->value}</p>

</a>
</h5>

Please find the debuger result attached: I want to display the "Belize Cotton & Cashmere" value 

 

Best regards

 

 

post-1146742-0-77609000-1479312628_thumb.png

Link to comment
Share on other sites

in Smarty syntax {$product->features} - doesn`t work. You need use this syntax {$product.features}

$product.features - is two-dimensions array()

So, you need to use iteration method to get the properties of array. For example: foreach, while...

 

I have tried this, but I'm not so good in PHP, can you please teach me how to deal with this, using this problem to teach? 

 

For example the following code doesn't work too:

    {if isset($features) && $features}
        <!-- Data sheet -->
        <section class="page-product-box">
            <h3 class="page-product-heading">{l s='Data sheet'}</h3>
            <table class="table-data-sheet">
                {foreach from=$features item=feature}
                <tr class="{cycle values="odd,even"}">
                    {if isset($feature.value)}
                    <td>{$feature.name|escape:'html':'UTF-8'}</td>
                    <td>{$feature.value|escape:'html':'UTF-8'}</td>
                    {/if}
                </tr>
                {/foreach}
            </table>
        </section>
        <!--end Data sheet -->
    {/if}

Best regards,

Edited by Graphics Back Label (see edit history)
Link to comment
Share on other sites

Task for example: I`ll print feature name and value after product name on the product page.
Let`s go - open file product.tpl and find var {$product.name ....}
Next - i`ll try to print features array for understanding how it looks. After <h1> i print features var...

<h1 itemprop="name">{$product->name|escape:'html':'UTF-8'}</h1>
{$features|@var_dump}

 
and we see this result:
140.jpg
 
Next, i print one feature name and value after product name.

<h1 itemprop="name">{$product->name|escape:'html':'UTF-8'} | {$features[0].name} - {$features[0].value} </h1>

 
$features - it`s simple array(), use Smarty documentation for understanding how work with arrays.
 
144.jpg

Link to comment
Share on other sites

Task for example: I`ll print feature name and value after product name on the product page.

Let`s go - open file product.tpl and find var {$product.name ....}

Next - i`ll try to print features array for understanding how it looks. After <h1> i print features var...

<h1 itemprop="name">{$product->name|escape:'html':'UTF-8'}</h1>
{$features|@var_dump}

 

and we see this result:

140.jpg

 

Next, i print one feature name and value after product name.

<h1 itemprop="name">{$product->name|escape:'html':'UTF-8'} | {$features[0].name} - {$features[0].value} </h1>

 

$features - it`s simple array(), use Smarty documentation for understanding how work with arrays.

 

144.jpg

 

 

I have tried, and unfortunately this doesn't work (at least, on the product list, where you can see all the products together) 

 

But I learned how to display something in a specific array.  

 

I can't figure out why with the debuger I can find the value, but I can't display it! 

 

The code {$features|@var_dump} give a "Null" output.. 

Link to comment
Share on other sites

In the product_list.tpl var $products contains product array.
Structure of the array is next:

array (size=7)
  0 => 
    array (size=83)
      'id_product' => string '1' (length=1)
      'id_supplier' => string '1' (length=1)
      'id_manufacturer' => string '1' (length=1)
      'id_category_default' => string '5' (length=1)
      'id_shop_default' => string '1' (length=1)
      'id_tax_rules_group' => string '1' (length=1)
      'on_sale' => string '0' (length=1)
      'online_only' => string '0' (length=1)
      'ean13' => string '0' (length=1)
      'upc' => string '' (length=0)
      'ecotax' => string '0.000000' (length=8)
      'quantity' => int 299
      'minimal_quantity' => string '1' (length=1)
      'price' => float 19.2
      'wholesale_price' => string '5.000000' (length=8)
      'unity' => string '' (length=0)
      'unit_price_ratio' => string '0.000000' (length=8)
      'additional_shipping_cost' => string '0.00' (length=4)
      'reference' => string 'demo_1' (length=6)
      'supplier_reference' => string '' (length=0)
      'location' => string '' (length=0)
      'width' => string '0.000000' (length=8)
      'height' => string '0.000000' (length=8)
      'depth' => string '0.000000' (length=8)
      'weight' => string '0.000000' (length=8)
      'out_of_stock' => string '2' (length=1)
      'quantity_discount' => string '0' (length=1)
      'customizable' => string '0' (length=1)
      'uploadable_files' => string '0' (length=1)
      'text_fields' => string '0' (length=1)
      'active' => string '1' (length=1)
      'redirect_type' => string '404' (length=3)
      'id_product_redirected' => string '0' (length=1)
      'available_for_order' => string '1' (length=1)
      'available_date' => string '0000-00-00' (length=10)
      'condition' => string 'new' (length=3)
      'show_price' => string '1' (length=1)
      'indexed' => string '1' (length=1)
      'visibility' => string 'both' (length=4)
      'cache_is_pack' => string '0' (length=1)
      'cache_has_attachments' => string '0' (length=1)
      'is_virtual' => string '0' (length=1)
      'cache_default_attribute' => string '1' (length=1)
      'date_add' => string '2015-11-16 14:34:38' (length=19)
      'date_upd' => string '2016-09-09 11:16:11' (length=19)
      'advanced_stock_management' => string '0' (length=1)
      'pack_stock_type' => string '3' (length=1)
      'id_shop' => string '1' (length=1)
      'id_lang' => string '1' (length=1)
      'description' => string '<p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including sho'... (length=541)
      'description_short' => string '<p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p>' (length=161)
      'link_rewrite' => string 'faded-short-sleeve-tshirts' (length=26)
      'meta_description' => string '' (length=0)
      'meta_keywords' => string '' (length=0)
      'meta_title' => string '' (length=0)
      'name' => string 'Faded Short Sleeve T-shirts' (length=27)
      'available_now' => string 'In stock' (length=8)
      'available_later' => string '' (length=0)
      'id_image' => string '1-1' (length=3)
      'legend' => string '' (length=0)
      'manufacturer_name' => string 'Fashion Manufacturer' (length=20)
      'id_product_attribute' => int 1
      'new' => string '0' (length=1)
      'product_attribute_minimal_quantity' => string '1' (length=1)
      'allow_oosp' => int 0
      'category' => string 'tshirts' (length=7)
      'link' => string 'http://someURL/1-faded-short-sleeve-tshirts.html' (length=73)
      'attribute_price' => float 0
      'price_tax_exc' => float 16
      'price_without_reduction' => float 19.2
      'reduction' => float 0
      'specific_prices' => boolean false
      'quantity_all_versions' => int 1799
      'features' => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...
          2 => 
            array (size=3)
              ...
      'attachments' => 
        array (size=0)
          empty
      'virtual' => int 0
      'pack' => int 0
      'packItems' => 
        array (size=0)
          empty
      'nopackprice' => int 0
      'customization_required' => boolean false
      'rate' => float 20
      'tax_name' => string '20%' (length=10)
      'color_list' => string

Features are also ARRAY! So, you need write like this: {$product.features[0].name} {$product.features[0].value} for example.

See in my picture bellow - Product Name (Feature Name: Feature Value)
199.jpg

Link to comment
Share on other sites

In the product_list.tpl var $products contains product array.

Structure of the array is next:

array (size=7)
  0 => 
    array (size=83)
      'id_product' => string '1' (length=1)
      'id_supplier' => string '1' (length=1)
      'id_manufacturer' => string '1' (length=1)
      'id_category_default' => string '5' (length=1)
      'id_shop_default' => string '1' (length=1)
      'id_tax_rules_group' => string '1' (length=1)
      'on_sale' => string '0' (length=1)
      'online_only' => string '0' (length=1)
      'ean13' => string '0' (length=1)
      'upc' => string '' (length=0)
      'ecotax' => string '0.000000' (length=8)
      'quantity' => int 299
      'minimal_quantity' => string '1' (length=1)
      'price' => float 19.2
      'wholesale_price' => string '5.000000' (length=8)
      'unity' => string '' (length=0)
      'unit_price_ratio' => string '0.000000' (length=8)
      'additional_shipping_cost' => string '0.00' (length=4)
      'reference' => string 'demo_1' (length=6)
      'supplier_reference' => string '' (length=0)
      'location' => string '' (length=0)
      'width' => string '0.000000' (length=8)
      'height' => string '0.000000' (length=8)
      'depth' => string '0.000000' (length=8)
      'weight' => string '0.000000' (length=8)
      'out_of_stock' => string '2' (length=1)
      'quantity_discount' => string '0' (length=1)
      'customizable' => string '0' (length=1)
      'uploadable_files' => string '0' (length=1)
      'text_fields' => string '0' (length=1)
      'active' => string '1' (length=1)
      'redirect_type' => string '404' (length=3)
      'id_product_redirected' => string '0' (length=1)
      'available_for_order' => string '1' (length=1)
      'available_date' => string '0000-00-00' (length=10)
      'condition' => string 'new' (length=3)
      'show_price' => string '1' (length=1)
      'indexed' => string '1' (length=1)
      'visibility' => string 'both' (length=4)
      'cache_is_pack' => string '0' (length=1)
      'cache_has_attachments' => string '0' (length=1)
      'is_virtual' => string '0' (length=1)
      'cache_default_attribute' => string '1' (length=1)
      'date_add' => string '2015-11-16 14:34:38' (length=19)
      'date_upd' => string '2016-09-09 11:16:11' (length=19)
      'advanced_stock_management' => string '0' (length=1)
      'pack_stock_type' => string '3' (length=1)
      'id_shop' => string '1' (length=1)
      'id_lang' => string '1' (length=1)
      'description' => string '<p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including sho'... (length=541)
      'description_short' => string '<p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p>' (length=161)
      'link_rewrite' => string 'faded-short-sleeve-tshirts' (length=26)
      'meta_description' => string '' (length=0)
      'meta_keywords' => string '' (length=0)
      'meta_title' => string '' (length=0)
      'name' => string 'Faded Short Sleeve T-shirts' (length=27)
      'available_now' => string 'In stock' (length=8)
      'available_later' => string '' (length=0)
      'id_image' => string '1-1' (length=3)
      'legend' => string '' (length=0)
      'manufacturer_name' => string 'Fashion Manufacturer' (length=20)
      'id_product_attribute' => int 1
      'new' => string '0' (length=1)
      'product_attribute_minimal_quantity' => string '1' (length=1)
      'allow_oosp' => int 0
      'category' => string 'tshirts' (length=7)
      'link' => string 'http://someURL/1-faded-short-sleeve-tshirts.html' (length=73)
      'attribute_price' => float 0
      'price_tax_exc' => float 16
      'price_without_reduction' => float 19.2
      'reduction' => float 0
      'specific_prices' => boolean false
      'quantity_all_versions' => int 1799
      'features' => 
        array (size=3)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...
          2 => 
            array (size=3)
              ...
      'attachments' => 
        array (size=0)
          empty
      'virtual' => int 0
      'pack' => int 0
      'packItems' => 
        array (size=0)
          empty
      'nopackprice' => int 0
      'customization_required' => boolean false
      'rate' => float 20
      'tax_name' => string '20%' (length=10)
      'color_list' => string

Features are also ARRAY! So, you need write like this: {$product.features[0].name} {$product.features[0].value} for example.

See in my picture bellow - Product Name (Feature Name: Feature Value)

199.jpg

 

 

Good it works now! 

 

Can I do ask you why written $product instead of $productS?

 

The last question: what is the difference between -> and the dot (.) ? I can't find the right documentation

 

Regards,

Link to comment
Share on other sites

$products - it`s multidimensional array(), it contains many arrays in single array.

$products = array(
  0 => 
    array (
      'id_product' => 1,
      'name' => 'name1',
      ....),
  1 => 
    array (
      'id_product' => 2,
      'name' => 'name2',
      ....),
  ....
);

So, you can access to item 'name' by array index: $products[1]['name'] –> display 'name2'

Or:

foreach($products as $product) {
    $product_name = $product['name];
}

Variable $product_name will contains values 'name' –> name1, name2... ect...

Presta use second way :) but in this case PHP create the copy of array... sometimes :)

 

Your second questions about Object Oriented Programming.

"–>" - It`s like a special variable and it refers to the member variable inside object. The official PHP name is "object operator".

It`s came from C++ and how works with memory in direct.

"." - it`s almost same, but in JAVA style :)

 

In C++ you can create object in stack or heap. And you can work with memory directly. PHP can`t work with memory directly.

 

For example:

// C++
#include <iostream>

class Point {
    private:
        double x;
        double y;
    public:
        Point(double x=0, double y=0) {
           this->x = x;
           this->y = y;
        }
        ~Point();

        double getX() const {
           return this->x;
        }
        double getY() const {
           return this->x;
        }
};

int main() {
    Point* pointA = new Point(3.00, 5.89);

    std::cout << "Point A: x = " << (*pointA).getX() << std::cout << std::endl;

    delete pointA;

    return 0;
}

same in PHP:

<?php    
    class Point {
        private $x;
        private $y;
        
        public function __construct ($x = 0, $y = 0) {
            $this->x = $this->validate($x);
            $this->y = $this->validate($y);
        }
        
        public function __destruct() {}
        
        private function validate($num) {
            if(is_numeric($num)) {
                return $num;
            }
            throw new Exception('Invalid param');
        }
        
        public function __get($name) {
            if ( property_exists($this, $name) ) {
                return $this->$name;
            }
            throw new Exception("Attribute error: attribute $name not found");
        }
        
        public function __set($name, $value) {
            if ( property_exists($this, $name) ) {
                $this->$name = $this->validate($value);
            }
            throw new Exception("Attribute error: attribute $name not found");
        }
        
        public function __toString() {
            return sprintf("(%g, %g)", $this->x, $this->y);
        }
        public function doSomething() {
            echo 'Something strange....';
        }

    }
    
    $pointA = new Point(3.0, 5.89);
    
    echo 'Point A:' . $pointA . PHP_EOL;
    $pointA->doSomething();
    
    unset($pointA);
?>

Right documentation: php.net

Edited by AlexNDR.mac (see edit history)
Link to comment
Share on other sites

debug show you variable and it`s values. Of course variable has named as $products.

But... to access the value, you need write like this - $products[0]['product_id'] or use cycle (iteration).

In this case we use $products

for ($i = 0; $ < $endOfProducts; $i++) {
   echo $products[$i]['name'];
}

In templates use other constructions: foreach ($names as $name)

foreach ($products as $product) {
    echo $product['name'];
}

see more info about this construction in php.net

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