VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: Genius WebDesign on July 16, 2012, 22:03:34 PM

Title: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on July 16, 2012, 22:03:34 PM
Hi,

Does anyone know how to echo different product values of the "related products" on the Product Details page?

I can see that $this->product->customfieldsRelatedProducts  points to the related products, but I don´t know how to echo specific product information  e.g. prices, product-ID and so on..

In my default.php (product details) file the following code echoes the related products:

      <?php
      foreach ($this->product->customfieldsRelatedProducts as $field){
         ?>
<div style="display:inline-block;" class="product-field product-field-type-<?php echo $field->field_type ?>" >
         <div class="product-field-display" style="width: 7em; overflow: hidden; line-height: 1.2em; max-height: 8.3em;position: relative;top: 0px; text-align: center;margin-left: 5px;
margin-right: 15px;"><?php echo $field->display ?></div>


Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: jefchenko on July 17, 2012, 04:57:10 AM
Hi fabelmik,

If you would put this somewhere in that file:
print_r($this->product->customfieldsRelatedProducts);

It will output all the fields virtuemart is getting for you. Unfortunately, no price is included in the array of related products.
The related products are in fact just linked to a (system generated) customfield (customfield id=1) of the product.
You would have to write your own script/query to get the price of those related products from the custom_value field (which represents the virtuemart_product_id of the related product).

I would also like this... Can anyone help us write a short code for this?
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on July 18, 2012, 14:22:39 PM
Hi jefchenko,

Thanks for the answer.

I guess we have to hope for someone to help us with a usable script.
What I really want to achieve is to show price and have an "add to cart" function on each related product.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on July 25, 2012, 17:25:30 PM
@jefchenko

It seems noone´s answering this post, so I have now made a "Commercial jobs" request instead:
http://forum.virtuemart.net/index.php?topic=105701.0

I will post the solution if or when someone applies for the job.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: dennis.g on August 07, 2012, 16:48:17 PM
There is no easy answer to this. You have to override the viewer for related products and add some code in multiple places.

Find the file /components/com_virtuemart/view/productdetails/tmpl/default_relatedproducts.php
Copy it to /templates/<your template>/html/com_virtuemart/productdetails/default_relatedproducts.php
That will create an override.

Now open that file and add
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();

after this line:
defined ( '_JEXEC' ) or die ( 'Restricted access' );

Then locate the line
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc?></span>

and replace it with
    <span class="product-field-desc"><?php

echo jText::_($field->custom_field_desc);

$product $model->getProductSingle($field->custom_value,false);
$price $calculator -> getProductPrices($product);
echo 
$currency->priceDisplay($price['salesPriceWithDiscount']);

?>
</span>


Not exactly a piece of cake :)
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on August 08, 2012, 03:21:33 AM
Thanks, Dennis

I will look into this once i have the time.
The code looks promising, though
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Menace on August 09, 2012, 14:28:37 PM
Wow, Dennis!

If this works, this is great! My entire site is based on making this possible and I'm looking for a solution for about 4 weeks now. I'll try this when I'm back home.

Will this also make it possible to echo the other values of the related product to the related product field on details page?


Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on August 09, 2012, 15:27:10 PM
I can confirm that the code works!:)

Great help, Dennis

I also want to add an "Add to cart" button and it seems that your method could maybe also achieve that.
I have the code for the "Add to cart" button, as it is in my category browse view:

<form method="post" class="product" action="index.php" id="addtocartproduct<?php echo $product->virtuemart_product_id ?>">
<div class="addtocart-bar">
<?php // Display the quantity box ?>
<!-- <label for="quantity<?php echo $this->product->virtuemart_product_id;?>" class="quantity_box"><?php echo JText::_('COM_VIRTUEMART_CART_QUANTITY'); ?>: </label> -->
<span class="quantity-box">
<input style="display:none;" type="text" class="quantity-input" name="quantity[]" value="1" />
</span>

<?php // Display the quantity box END ?>

<?php // Add the button
$button_lbl JText::_('COM_VIRTUEMART_CART_ADD_TO');
$button_cls ''//$button_cls = 'addtocart_button';
if (VmConfig::get('check_stock') == '1' && !$this->product->product_in_stock) {
$button_lbl JText::_('COM_VIRTUEMART_CART_NOTIFY');
$button_cls 'notify-button';
?>


<?php // Display the add to cart button ?>
<span class="addtocart-button">
<input type="submit" name="addtocart"  class="addtocart-button" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
</span>

<div class="clear"></div>
</div>

<?php // Display the add to cart button END ?>
<input type="hidden" class="pname" value="<?php echo $product->product_name ?>">
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="view" value="cart" />
<noscript><input type="hidden" name="task" value="add" /></noscript>
<input type="hidden" name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id ?>" />
<?php /** @todo Handle the manufacturer view */ ?>
<input type="hidden" name="virtuemart_manufacturer_id" value="<?php echo $product->virtuemart_manufacturer_id ?>" />
<input type="hidden" name="virtuemart_category_id[]" value="<?php echo $product->virtuemart_category_id ?>" />
</form>



The code: $calculator -> getProductPrices($product)  is the equivalent to $this->product->prices for the main product in the product details page, as I understand it.
If that is true can you then also make a code that is the equivalent to the following?;
$this->product->virtuemart_product_id
$this->product->product_name
$this->product->virtuemart_manufacturer_id
$this->product->virtuemart_category_id

If so, then I guess it would be possible to modify the "Add to cart" code to work on the related products.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Menace on August 09, 2012, 18:50:54 PM
Quote from: fabelmik on August 09, 2012, 15:27:10 PM
If so, then I guess it would be possible to modify the "Add to cart" code to work on the related products.

Hey Fablemik, I just applied the code and you're right, it works! This is just amazing! I am so happy  ;D

I just played with the code a little (I'm the biggest php noob ever!) and I mentioned, it is already possible to call several productinformation from the related products in the provided code.

For example I added the line echo $product->virtuemart_category_id
below echo $currency->priceDisplay($price['salesPrice']); and it did in fact give me the correct output on frontend.

First try for you could be to paste the code of the button just there and eliminate every this->

Just to make sure again: I have absolutely no skills in coding! I'm just guessing like I did my whole way through VM  ;)


EDIT:
Couldn't wait to test it. For me it works!
I added product details button, manufacturer description and logo.

thx again, Dennis!!!
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on August 09, 2012, 20:02:38 PM
Hey Menace,

Thanks for the input!
It works!!!

I don´t know why I didn´t think of that. Guess it´s because I´m an even bigger noob at PHP than you;)

I have now successfully added an "Add to cart" button on each "related product", and it simply works 100% with modal popups and everything. This is absolutely great!
I wonder why the devs didn´t add an option to enable "add to cart" in the related products view out of the box, though.. It´s a major convenience for the customer to be able to quickly add a shiny related product to the cart, without having to leave the current product details page.. Maybe I should put this up as a suggestion on the forum-board.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on August 09, 2012, 20:13:48 PM
For others, and myself, here´s a sum up of what I did in order to add final product price and an "Add to cart" button on the related products in the product details page:

Find the file /components/com_virtuemart/view/productdetails/tmpl/default_relatedproducts.php
Copy it to /templates/<your template>/html/com_virtuemart/productdetails/default_relatedproducts.php
That will create an override.

Now open that file and add:
$model = new VirtueMartModelProduct();
$calculator = calculationHelper::getInstance();
$currency = CurrencyDisplay::getInstance();


after this line:
defined ( '_JEXEC' ) or die ( 'Restricted access' );


Then locate the line
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc?></span>


and replace it with
    <span class="product-field-desc"><?php

echo jText::_($field->custom_field_desc);

$product $model->getProductSingle($field->custom_value,false);
$price $calculator -> getProductPrices($product);
echo 
$currency->priceDisplay($price['salesPrice']);
?>

</span>

<?php // This is the beginning of "Add to cart" ?>

<form method="post" class="product" action="index.php" id="addtocartproduct<?php echo $product->virtuemart_product_id ?>">
<div class="addtocart-bar">
<?php // Display the quantity box ?>
<!-- <label for="quantity<?php echo $product->virtuemart_product_id;?>" class="quantity_box"><?php echo JText::_('COM_VIRTUEMART_CART_QUANTITY'); ?>: </label> -->
<span class="quantity-box">
<input style="display:none;" type="text" class="quantity-input" name="quantity[]" value="1" />
</span>

<?php // Display the quantity box END ?>

<?php // Add the button
$button_lbl JText::_('COM_VIRTUEMART_CART_ADD_TO');
$button_cls ''//$button_cls = 'addtocart_button';
if (VmConfig::get('check_stock') == '1' && !$product->product_in_stock) {
$button_lbl JText::_('COM_VIRTUEMART_CART_NOTIFY');
$button_cls 'notify-button';
?>


<?php // Display the add to cart button ?>
<span class="addtocart-button">
<input type="submit" name="addtocart"  class="addtocart-button" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
</span>

<div class="clear"></div>
</div>

<?php // Display the add to cart button END ?>
<input type="hidden" class="pname" value="<?php echo $product->product_name ?>">
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="view" value="cart" />
<noscript><input type="hidden" name="task" value="add" /></noscript>
<input type="hidden" name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id ?>" />
<?php /** @todo Handle the manufacturer view */ ?>
<input type="hidden" name="virtuemart_manufacturer_id" value="<?php echo $product->virtuemart_manufacturer_id ?>" />
<input type="hidden" name="virtuemart_category_id[]" value="<?php echo $product->virtuemart_category_id ?>" />
</form>



You may want to wrap up the "form" containing the "Add to cart" -button in a div, so that you can style the layout and placement etc.


Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: dennis.g on August 22, 2012, 21:09:00 PM
Hi guys. I am quite new to this forum and apparently I am doing something wrong. I did not receive notifications for any of your replies. Thank you for your kind words. I will look to your questions as my time permits.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on August 22, 2012, 21:54:51 PM
Hi Dennis,

Yea, had the same problem. Took me a while to find out that "subscribing" to your own posts is not done per default, so you have to remember clicking "notify" to recieve email notifications when people post replies..
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: el_carl0s on September 01, 2012, 01:40:18 AM
hey guys, thank you dennis and fabel for your help i had been looking for this solution for some time now

my problem still is that i would like to display the images of the related products with the same format as the ones of the main product, i've tried with

$this->product->images[0]->displayMediaFull(......

but i cant get access to the related products images, i know that the line

echo $field->display

displays an image and that's ok but it also shows the name of the product linked to the product details page, i would like to not have that link and to display the image or images of the product alone without the product name, is this possible? and if so, how do i do it?

Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on September 01, 2012, 02:33:44 AM
Hi,

After this code:
echo jText::_($field->custom_field_desc);
$product = $model->getProductSingle($field->custom_value,false);
$price = $calculator -> getProductPrices($product);


You should try to add this:
echo $product->images[0]->displayMediaFull('class="product-image"',false,"class='modal'",true);

So instead of:
$this->product->images[0]->displayMediaFull(......

You should use:
$product->images[0]->displayMediaFull(......

I havent been able to test this yet, but it should work.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on September 01, 2012, 03:05:27 AM
..Continued

If the only things you´re after are to "remove" the product descriptions and "disable" the image-links, then that can actually all be accomplished by only using CSS.

Use this guide to disable the image-link:
http://stackoverflow.com/questions/2091168/disable-a-link-using-css

and apply either an "opacity:0;" or a "display:none;" to the product description text.

Being a CSS-guy myself, I find it more satisfying when the solution is purely based on CSS..



Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: el_carl0s on September 04, 2012, 00:03:52 AM
that's pretty helpful, but wbat about when a product has more than one image and the user wants to browse through them what do i do then?
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on September 04, 2012, 00:12:56 AM
If you really want to you can actually have the related products look just like the main product, with all the pictures etc., you just have to apply the code in the "for each" loop.
However I did myself find a problem with not being able to echo custom fields for the related products in the loop..


Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: wbauer74 on November 01, 2012, 21:13:11 PM
Hi I tried this with version 2.0.12b and it isn't working. I think they changed some things in it. Anyone have this working in the newer version?
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: Genius WebDesign on November 01, 2012, 21:46:48 PM
I´m sorry, I can´t help you there.

I´m staying with 2.0.6 on my current project. Too many mods to the core for me to upgrade now.

Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: toad78 on December 06, 2012, 09:19:43 AM
Has there been an update for this method to work with 2.0.14?
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: naoko15 on December 22, 2012, 12:57:27 PM
Hi, i'm using  Virtuemart 2.0.12f. Has anyone already made this work in this version? I really need it

Thanks.
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: LuukDriessen on January 18, 2013, 15:57:40 PM
Hello People,

I was trying to edit the related categories. Thanks for all the info on this forum and especially this topic.

But I have a question.

I succesfully found this line: "defined ( '_JEXEC' ) or die ( 'Restricted access' );"  and added the suggested code.

But when I am trying to find the next line, I'm stuck!

I can't locate: "<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>" in default_relatedproducts.php

So when I add the buttons and prices it doesn't work.

What am I doing wrong?

I am using joomla 2.5.8 and Virtuemart 2.0.18a

Thanks in advance



Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: baggiesmad on March 05, 2013, 18:20:02 PM
Works great thanks!
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: kratzi on May 04, 2013, 22:56:38 PM
Hi

is there a solution for the new versions of VM2.

I am using 2.0.18a and the default_relatedproducts.php was changed to this code wherefore the described solution does not work any more:


<?php

defined 
'_JEXEC' ) or die ( 'Restricted access' );
?>

        <div class="product-related-products">
    <h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>

    <?php
    
foreach ($this->product->customfieldsRelatedProducts as $field) {
    if(!empty($field->display)) {
?>
<div class="product-field product-field-type-<?php echo $field->field_type ?>">
    <span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-fields-title" ><b><?php echo JText::_($field->field->custom_title?></b></span>

</div>
<?php }
    } ?>

        </div>


Regards

Simon
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: old_fritz on May 10, 2013, 11:45:47 AM
hey there,

thanks a lot for the solutions you worked out.
that works realy good for me.


but i run in kind of same problems, when it comes up to custome fields, which are to insert in the related products:

in detail the problem is, that a vendor logo (gustini) is not shown.
just an option box with the file selected.

what it should show is actually the gustini logo like in the attached detailed view.

i generated that with the custom field "custom title" with the default value "shipping_icon".
for the "custom_value" i set the default value to 43 that´s the id for the logo.

so that works pretty good for the details view. i don´t have any idea why the input of the exact same custome code to the related products result an other appearance.
i guess there´s just something in the code, that tells the related view to show an option box instead of an image.
hopefully not a big thing.

here´s the custome code i insert in the "related_products.php":


<div><?php $custom_title null
if (!empty(
$product->customfields)) {
foreach (
$product->customfields as $field) {
if (
$field->is_hidden //OSP http://forum.virtuemart.net/index.php?topic=99320.0
continue;
if (
$field->display) { ?>

<div><span class="product-field-display"><?php echo $field->display ?></span></div>
<?php ?> <?php ?> <?php }  ?>
</div>



may anybody have got an idea?!
thanks a lot in advance!!!

[attachment cleanup by admin]
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: MarioP on March 04, 2014, 08:36:39 AM
I refresh the thread because I can't find any solution for the newest VM release. Does anybody has an idea how to place prices in related products for the code below ( default_relatedproducts.php VM 2.0.27 )?
defined ( '_JEXEC' ) or die ( 'Restricted access' );
?>
        <div class="product-related-products">
    <h4><?php echo JText::_('COM_VIRTUEMART_RELATED_PRODUCTS'); ?></h4>

    <?php
    
foreach ($this->product->customfieldsRelatedProducts as $field) {
    if(!empty($field->display)) {
?>
<div class="product-field product-field-type-<?php echo $field->field_type ?>">
    <span class="product-field-display"><?php echo $field->display ?></span>
</div>
<?php }
    } ?>

        </div>
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: AH on March 04, 2014, 09:09:39 AM
Related products seems to be one aspect of vm2 where the customer experience is a step backwards

I thought the aim of related products was to promote additional spend on items that are related to the item being viewed and possibly added to basket

But without the add to cart and price, this is a slow process that is less likely to succeed and making users "jump" to a single item to add to cart means that the potential for adding multiple related items to cart is based on many circular related item referrences!!

Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: MarioP on March 04, 2014, 11:59:26 AM
Yes I agree, lack of price and add to cart button for related products in the product details view is definitely a shortcoming that causes the customers don't have possibility to instant review and decision making. If there was a possibility to add a solution in the next release of VM would be great.
Thanks
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: MarioP on March 04, 2014, 20:16:20 PM
How to draw VM team (Milbo?) attention to this issue? I see that they rarely visit this forum section - product creation, at least they reply seldomly. ;). The feature would be a great thing and it probably will not take too much time to elaborate appropriate code...So please the VM team or anybody who knows the solution for any feedback - maybe there's some plugin ....
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: csho on May 15, 2014, 13:50:05 PM
Quote from: fabelmik on September 01, 2012, 02:33:44 AM
Hi,

After this code:
echo jText::_($field->custom_field_desc);
$product = $model->getProductSingle($field->custom_value,false);
$price = $calculator -> getProductPrices($product);


You should try to add this:
echo $product->images[0]->displayMediaFull('class="product-image"',false,"class='modal'",true);

So instead of:
$this->product->images[0]->displayMediaFull(......

You should use:
$product->images[0]->displayMediaFull(......

I havent been able to test this yet, but it should work.

Didn't worked on my case, it got bugged and removed all the css-like styles in my page.
It usually seems that is because of an php error.
Any other guess?
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: illusiondrmr on July 01, 2014, 12:24:09 PM
Just popped in to say thanks to Genius WebDesign for the solution in page 1 !!
Title: Re: How to echo the price and ID value of related products on Product Details page
Post by: camble on September 04, 2014, 17:46:43 PM
Quote from: csho on May 15, 2014, 13:50:05 PM
Quote from: fabelmik on September 01, 2012, 02:33:44 AM
Hi,

After this code:
echo jText::_($field->custom_field_desc);
$product = $model->getProductSingle($field->custom_value,false);
$price = $calculator -> getProductPrices($product);


You should try to add this:
echo $product->images[0]->displayMediaFull('class="product-image"',false,"class='modal'",true);

So instead of:
$this->product->images[0]->displayMediaFull(......

You should use:
$product->images[0]->displayMediaFull(......

I havent been able to test this yet, but it should work.

Didn't worked on my case, it got bugged and removed all the css-like styles in my page.
It usually seems that is because of an php error.
Any other guess?

I've managed to customise my Related Products in VM 2.6.6, however I just want to be able to pull through the main image for each product.

I tried the code (above) and it doesn't work, so do you have any ideas?


Kind regards
Liam