Author Topic: $this-> VS $product->  (Read 45956 times)

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10272
  • VirtueMart Version: 3+
$this-> VS $product->
« on: April 03, 2012, 20:58:22 pm »
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
Code: [Select]
<?php echo $product->product_name ?>
Because you have a foreach loop before 
Code: [Select]
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
Code: [Select]
<?php echo $this->product->product_name ?>because there is only one product available.

I do NOT do development work for hire.

Mandala

  • Beginner
  • *
  • Posts: 1
    • WEOP - a member of the numeric world
Re: $this-> VS $product->
« Reply #1 on: June 06, 2012, 20:37:52 pm »
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

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10272
  • VirtueMart Version: 3+
Re: $this-> VS $product->
« Reply #2 on: June 06, 2012, 22:03:11 pm »
<?php echo $product->category_name ?>

^^ that just worked for me
I do NOT do development work for hire.

huegel-huepfer

  • Beginner
  • *
  • Posts: 7
Re: $this-> VS $product->
« Reply #3 on: July 19, 2012, 16:07:32 pm »
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

Code: [Select]

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

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: $this-> VS $product->
« Reply #4 on: July 21, 2012, 09:29:31 am »
Hi Everyone,

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

Code: [Select]

    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

  • Beginner
  • *
  • Posts: 3
Re: $this-> VS $product->
« Reply #5 on: July 23, 2012, 12:18:27 pm »
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

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: $this-> VS $product->
« Reply #6 on: August 03, 2012, 18:43:29 pm »
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

  • Beginner
  • *
  • Posts: 1
Re: $this-> VS $product->
« Reply #7 on: September 04, 2012, 14:59:27 pm »
Hello , i would like to display additionall product images , on my category page , here is the code i use for this :

Code: [Select]
<?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

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: $this-> VS $product->
« Reply #8 on: September 04, 2012, 15:26:21 pm »
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
         }
      }
   }
   
?>


Code: [Select]

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

Code: [Select]

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

  • Beginner
  • *
  • Posts: 6
    • Kowalsk7 Web Developer
Re: $this-> VS $product->
« Reply #9 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>';}
   }

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10272
  • VirtueMart Version: 3+
Re: $this-> VS $product->
« Reply #10 on: November 28, 2012, 19:08:03 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;}
I do NOT do development work for hire.

Th.Pilegaard

  • Beginner
  • *
  • Posts: 20
Re: $this-> VS $product->
« Reply #11 on: December 15, 2012, 18:34:26 pm »
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

  • Beginner
  • *
  • Posts: 4
Re: $this-> VS $product->
« Reply #12 on: January 14, 2013, 12:18:21 pm »
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)


Code: [Select]
<!-- 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

  • Beginner
  • *
  • Posts: 1
  • Skype Name: Rollin.DD
Re: $this-> VS $product->
« Reply #13 on: April 22, 2013, 06:12:53 am »
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

  • Beginner
  • *
  • Posts: 1
Re: $this-> VS $product->
« Reply #14 on: April 28, 2013, 18:19:20 pm »
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

           
Code: [Select]
// 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.