Author Topic: Module "latest product" with description products  (Read 20625 times)

euforico

  • Beginner
  • *
  • Posts: 6
Module "latest product" with description products
« on: December 20, 2009, 08:54:53 am »
Hi.

I'm using the "mod_virtuemart_latestprod" of Virtuemart, it's perfect but i want to show the product description on the module. Somebody know how can do this?

Thanks a lots :D

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #1 on: December 20, 2009, 18:11:24 pm »
The following hack will allow you to dispaly a product's description in the "Latest Products" module.  Since the "Latest Products" module uses the common "product snapshot" template, this hack will also cause all other usages of "product snapshot" to show the description.  If you do not want this behavior, do not install this hack.

There are two tasks involved in this hack.  The first is to modify the "product snapshot" template and the second is to modify the class "ps_product" with a user class.

To modify the snapshot, first make a copy of, then edit this file (assuming you're using the Default theme; if not, substitute your theme's name for Default):

     components/com_virtuemart/themes/default/templates/common/productsnapshot.tpl.php

Let's say you want to put the Description after the price.  Find these lines:
Code: [Select]
<!-- The product price DIV. -->
<div style="width: 100%;float:left;text-align:center;">
<?php
if( !empty($price) ) {
echo $price;
}
?>

</div>

Right after that, add these lines:
Code: [Select]
<!-- The product Description DIV. -->
<div style="width: 100%;float:left;text-align:center;">
<?php
if( !empty($product_desc) ) {
echo $product_desc;
}
?>

</div>

Save the file and you're done with task one.

Next we need to change the function (method) called "product_snapshot" in the class called "ps_product". Instead of changing the core class, this fix uses VM's "Extended User Class" functionality which became available with VM 1.1.4.
There are 4 steps involved in this task.

Step 1 - copy the following code and save it into a file named "ps_product.php" (but don't overwrite your existing ps_product.php file).
Code: [Select]
<?php
if( !defined'_VALID_MOS' ) && !defined'_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

class 
ps_product extends vm_ps_product {

/**
* Alternative product_snapshot method which reads the product's description and
* puts it into the template
*/
/**
 * Returns HTML code for a snapshot of a product based on the product sku.
 * This was written to provide a quick way to display a product inside of modules
 *
 * @param string $product_sku The SKU identifying the product
 * @param boolean $show_price Show the product price?
 * @param boolean $show_addtocart Show the add-to-cart link?
 * @param boolean $show_product_name Show the product name?
 */
function product_snapshot$product_sku$show_price=true$show_addtocart=true$show_product_name true ) {

global $sess$mm_action_url;

$db = new ps_DB;

require_once(CLASSPATH.'ps_product_category.php');
$ps_product_category = new ps_product_category;

$q "SELECT product_id, product_name, product_parent_id, product_thumb_image, product_desc FROM #__{vm}_product WHERE product_sku='$product_sku'";
$db->query$q );

if ($db->next_record()) {
$product_id $db->f("product_id" );
$tpl = new $GLOBALS['VM_THEMECLASS']();

$cid $ps_product_category->get_cid$product_id );

$tpl->set'product_id'$product_id);
$tpl->set'product_name'$db->f("product_name") );
$tpl->set'product_desc'$db->f("product_desc") );
$tpl->set'show_product_name'$show_product_name );

if ($db->f("product_parent_id")) {
$url "?page=shop.product_details&category_id=$cid&flypage=".$this->get_flypage($db->f("product_parent_id"));
$url .= "&product_id=" $db->f("product_parent_id");
} else {
$url "?page=shop.product_details&category_id=$cid&flypage=".$this->get_flypage($db->f("product_id"));
$url .= "&product_id=" $db->f("product_id");
}
$product_link $sess->url($mm_action_url"index.php" $url);
$tpl->set'product_link'$product_link );
$tpl->set'product_thumb_image'$db->f("product_thumb_image"), "alt=\"".$db->f("product_name")."\"");

if (_SHOW_PRICES == '1' && $show_price) {
// Show price, but without "including X% tax"
$price $this->show_price$db->f("product_id"), true );
$tpl->set'price'$price );
}
if (USE_AS_CATALOGUE != && $show_addtocart 
&& isset( $GLOBALS['product_info'][$product_id]['price']['product_price_id'] ) // Product must have a price to add it to cart
&& !$this->product_has_attributes($product_idtrue)  // Parent Products and Products with attributes can't be added to cart this way
) {
$url "?page=shop.cart&func=cartAdd&product_id=" .  $db->f("product_id");
$addtocart_link $sess->url($mm_action_url"index.php" $url);
$tpl->set'addtocart_link'$addtocart_link );
}
return $tpl->fetch'common/productsnapshot.tpl.php');
}

return '';

}

}
?>

Step 2 - Create a directory in
          components/com_virtuemart/themes/your_theme_name/
and call it user_class (unless you already have one)

Step 3 - Move the file from Step 1 into this new directory so the file path looks like this:
          components/com_virtuemart/themes/your_theme_name/user_class/ps_product.php

Step 4 - Admin->Configuration->Security tab->Security Settings box
          enable "Allow the inclusion of extended classes from the theme-directory"
       
Test it.  If you have any problems, just delete the file user_class/ps_product.php

Important note: if you actually wanted to show the "short description" instead of the full product description, then in both tasks, change all occurrences of "product_desc" to "product_s_desc".


Updated with correction identified below by coolissimo.

coolissimo

  • Beginner
  • *
  • Posts: 26
Re: Module "latest product" with description products
« Reply #2 on: June 30, 2010, 19:38:27 pm »
Hello,

I'm using the last virtuemart version 1.1.4

I try to apply this hack but i have an error :

Parse error: syntax error, unexpected ';', expecting T_FUNCTION in .../components/com_virtuemart/themes/default/user_class/ps_product.php  on line 73

The line 73 seems to be the last one of this other ps_product.php code.

I'm using the "1pixeloutplayer" (mp3 player)in the products description.. Dont know if it is because of that.
My goal is to have the mp3 player showing in the latest product module.

Thanks by advance for your help !

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #3 on: June 30, 2010, 20:08:03 pm »
It appears there's a missing closing right brace at the end.  Insert a line in your copy of ps_product.php so that the end of the file looks like this:
Code: [Select]
}
?>
Let me know if that fixes it and if so, I'll correct the above post.

coolissimo

  • Beginner
  • *
  • Posts: 26
Re: Module "latest product" with description products
« Reply #4 on: June 30, 2010, 21:00:25 pm »
Yes it works.

But it is not showing the 1pixeloutplayer , only the code..
looks like : {audio}media/extrait/song2010.mp3{/audio}


rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #5 on: June 30, 2010, 23:00:03 pm »
Thanks for the feedback on the missing closing right brace.  I have corrected the code above.

It looks like your other issue may be due to that "1pixeloutplayer" being a plugin.  The default "product snapshot template" doesn't process plugins.  To modify it so it does, try changing this line in productsnapshot.tpl.php
Code: [Select]
echo $product_desc;to
Code: [Select]
echo vmCommonHTML::ParseContentByPlugins( $product_desc );
I haven't tried this so I'm not sure if it will work, but give it a try.

coolissimo

  • Beginner
  • *
  • Posts: 26
Re: Module "latest product" with description products
« Reply #6 on: July 01, 2010, 07:46:38 am »
it works perfectly !! Thank you very much rb

Hawaii

  • Beginner
  • *
  • Posts: 3
Re: Module "latest product" with description products
« Reply #7 on: August 04, 2010, 16:25:23 pm »


Important note: if you actually wanted to show the "short description" instead of the full product description, then in both tasks, change all occurrences of "product_desc" to "product_s_desc".


Thank you for your hack, It works very well. Just one thing: is there a way to cut the displayed description until the first "X" characters? Because also using the short one, sometimes happens to be too long and doesn't fit.

Marco.


rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #8 on: August 04, 2010, 16:35:50 pm »
See the PHP substring function called substr:
http://www.php.net/manual/en/function.substr.php

joely666

  • Beginner
  • *
  • Posts: 11
Re: Module "latest product" with description products
« Reply #9 on: August 13, 2010, 02:34:08 am »
I'm try to do something similar with mod_virtuemart_featureprod. I want to display the category name as as a link for each featured product. I tested first with "product_s_desc" as described, which worked perfectly, and then hoped using "category_name" (not sure if that is the correct variable) would work too... but the mod came up blank. Is there a modification to the above "ps_product.php" that will make this work?
 Thanks
Joely

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #10 on: August 13, 2010, 14:52:15 pm »
Putting this is productsnapshot.tpl will show the first category that a product belongs to:
Code: [Select]
<?php echo ps_product_category::get_name$product_id ); ?>

joely666

  • Beginner
  • *
  • Posts: 11
Re: Module "latest product" with description products
« Reply #11 on: August 13, 2010, 15:39:33 pm »
Thank you. I would like it to be a link to the relevant category. I have tried:

Code: [Select]
<div style="width: 100%;float:left;text-align:left;">
<a title="<?php echo ps_product_category::get_name$product_id ); ?>" href="<?php $sess->purl(URL."index.php?option=com_virtuemart&amp;page=shop.browse&amp;category_id=".$category["category_id"]) ?>">
<?php echo ps_product_category::get_name$product_id ); ?></a>

But it doesn't put the category id in the string.

rb

  • Advanced
  • Full Member
  • *****
  • Posts: 1503
Re: Module "latest product" with description products
« Reply #12 on: August 13, 2010, 15:49:40 pm »
To get the category_id, use this:
Code: [Select]
ps_product_category::get_cid( $product_id )

joely666

  • Beginner
  • *
  • Posts: 11
Re: Module "latest product" with description products
« Reply #13 on: August 13, 2010, 16:14:28 pm »
Perfect. Many thanks

joely666

  • Beginner
  • *
  • Posts: 11
Re: Module "latest product" with description products
« Reply #14 on: August 14, 2010, 12:01:23 pm »
When the product is in a subcategory I need to display the parent category... I have tried to modify get_name and get_cid functions in ps_product_categories so that if the product is in a sub category they return the respective name and id for the parent category of the $product_id. It was a bit of a calamity... is there an existing function to do this?