News:

Support the VirtueMart project and become a member

Main Menu

$this-> VS $product->

Started by PRO, April 03, 2012, 20:58:22 PM

Previous topic - Next topic

PRO

When templating product page and category page.


$this->    Refers to the CURRENT object

The current Object is not hard to figure out. It is a VmView and usually defined in the url for example as &view=productdetails.  =>VmViewProductdetails

SO, in the category template. $this-> is going to call variables created in view.html.php for the category view.

In the product details template. $this->   is going to call variables from THAT view.

Product variables are called on the category page with   $product->


For example
On the category page

You call the product name like this
<?php echo $product->product_name ?>

Because you have a foreach loop before  foreach ( $this->products as $product ) {
Therefore you have $product and not $this->product

BUT: On the product page, you call the product name like this
<?php echo $this->product->product_name ?>
because there is only one product available.


Mandala

Hi,

Following your example I tried to put the category name in the browse page.




<p class="prodcat"><?php echo $product->category_name; ?></p>




But nothing appears.

I'm working with J1.5.26 + V1.1.19

Could you please help me ?
Regards

Mike

PRO

<?php echo $product->category_name ?>

^^ that just worked for me

huegel-huepfer

I'm trying to show child's stock level in parent product details.

I work with a "VM Custom, stockable variants" plugin. In the parent product you can choose different child products with a drop down menu.
First try was to insert following code in the stockable.php



// Availability Stock Level
?><p></p><?php
$stato = "";
$statotip = "";
if ($field->child[$child_id]['in_stock'] < 1) {
$stato = "nostock";
$statotip = "Hurry! Very low items stock!";
} else {
if ($field->child[$child_id]['in_stock'] < 2) {
$stato = "lowstock";
$statotip = "Low items in stock!";
} else {
$stato = "normalstock";
$statotip = "Items in stock available!";
}
} ?>

<span class="vmicon vm2-<?php echo $stato ?>" title="<?php echo $statotip ?>"></span>
        <?php


Now I receive correct stock level of child products above category and product view, but no single stock level of the current chosen child within the parent view.

I'd welcome any help to get the result I wanted.

Thanks a lot,
Volker
Joomla 2.5.6

VM 2.0.8e

ivus

Hi Everyone,

If you want to check what available fields you can use, put this before your loop



    echo "<pre>"; print_r( $products ); echo "</pre>"; // CATEGORY PAGE

    echo "<pre>"; print_r( $this->products ); echo "</pre>"; // PRODUCTS PAGE



You can use this to see the contents of any array/variable/object, just put the name inside the "print_r( $HERE );" tags ...

;D

zavier56

Hi everyone,
I'm very interested by this thread because I've a problem to display custom fields in the category page ... reccurent question ...

With the command print_r($products) I can see the differents properties I can use ... but there are the properties.
What happends with the methods ?
I want to use the command $product->customfieldsSorted['normal']. This command runs perfectly in the product page (with $this-> ) but not in the category page ...

A (good) idea to progress to find a solution ?
Thank's
Xavier

ivus

hi zavier56,

read the code in my previous post CAREFULLY.

Pay particular attention to the comments. It's not hard to figure out when the answer is given.

Karudi

Hello , i would like to display additionall product images , on my category page , here is the code i use for this :

<?php



 
if (!empty($product->images) and count ($product->images)>0) {
   

// List all Images

if (count($product->images) > 0) {

    foreach ($product->images as $image)
 {

echo '<div class="floatleft">' $image->displayMediaThumb('class="product-image"'true'class="modal"'truetrue) . '</div>'//'class="modal"'
 
    }

}


}

?>




My problem is it displays only the first image i added , ignoring all the other ones .
P.S.I make changes in ../category/default.php

ivus

Hi Karudi,

Firstly your code has some redundant checks...

<?php

   if (!empty($product->images) and count ($product->images)>0) { <-- CHECKS TO MAKE SURE THE ARRAY IS SET AND COUNT IS GREATER THAN 1... NOT REQUIRED AS THE ARRAY WILL ALWAYS BE SET AND CONTAIN AN IMAGE... YES EVEN THE "NO IMAGE" PLACEHOLDER IMAGE IF NONE IS DEFINED
      // List all Images
      if (count($product->images) > 0) { <-- RECHECKS THAT THE ARRAY COUNT IS GREATER THAN 1... AGAIN! ???
         foreach ($product->images as $image)
         {
            echo '<div class="floatleft">' . $image->displayMediaThumb('class="product-image"', true, 'class="modal"', true, true) . '</div>'; //'class="modal"' <-- USELESS COMMENT
         }
      }
   }
   
?>




// List all Images
foreach ($product->images as $image)
{
    echo '<div class="floatleft">' . $image->displayMediaThumb('class="product-image"', true, 'class="modal"', true, true) . '</div>';
}



The above code should now work... if the array returned MORE THAN 1 IMAGE...

Change "/components/com_virtuemart/views/category/view.html.php" @ line 171.



    // Load the products in the given category
    $products = $productModel->getProductsInCategory($categoryId);
    $productModel->addImages($products,1);
    $this->assignRef('products', $products);



change this line:

$productModel->addImages($products,1);

to

$productModel->addImages($products);

to output all images for each product to the product array.

SAVE. REFRESH. YAY!!!

k0walsky

hello if you want to hide the first picture below on additional image

   foreach ($product->images as $image)
   {   
   if (($product->images[0]->file_url_thumb) == ($image->file_url_thumb))
      {} else {
       echo '<div class="floatleft">' . $image->displayMediaThumb('class="product-image-small"', true, 'class="modal"', true, true);
      echo '</div>';}
   }

PRO

Quote from: k0walsky on November 28, 2012, 12:31:48 PM
hello if you want to hide the first picture below on additional image

   foreach ($product->images as $image)
   {   
   if (($product->images[0]->file_url_thumb) == ($image->file_url_thumb))
      {} else {
       echo '<div class="floatleft">' . $image->displayMediaThumb('class="product-image-small"', true, 'class="modal"', true, true);
      echo '</div>';}
   }


if (($product->images[0]->file_url_thumb) == ($image->file_url_thumb))
      {continue;}

Th.Pilegaard

I want to create a list of child's and their names, availability and price

By adding some code to customfields.php I can read out the names, product_id and availability by reading the $child array. But I can't find an easy way to get the child's salesprice.

Of cause I can get this by an SQL-call, but it can't be true that this i necessary

isn't there an easier method

ThomasR

Great topic,

I am trying to get the parent category name into the category template
(public_html/templates/template/html/com_virtuemart/category/default.php)



<!-- Category title -->
<?php if( !empty($this->category->category_name) ) : ?>
<h1 class="vm-category-title"><?php echo $this->category->category_name?></h1>
<?php endif; ?>


The structure of the categories has got 3 levels.
It needs to be [LEVEL 2] [ $this->category->category_name (LEVEL 3)]| Website name... , so just one level up.
I've searched the forum but couldn't find any solution to get this one level up.

Is there something possible like $previous->category->category_name?

EDIT: Working with the latest version of Virtuemart (2.0.18a)

Rollin45

#13
I was also little confuse about Custom field error in my site page, but now I found the way.. Thanks to being here with solution.  :)

tellur21

Hi Ivus,
I read your answer to Karudi about additional images of product in category page. And I have exact problem. But I'm using Virtuemart 2.0.8c. I couldn't fine this lines

            // Load the products in the given category
    $products = $productModel->getProductsInCategory($categoryId);
    $productModel->addImages($products,1);
    $this->assignRef('products', $products);


in this path /components/com_virtuemart/views/category/view.html.php
Could you please provide solution for VM 2.0.8c. Because they use a different model of viewing. Thank you at advance.