How to echo the price and ID value of related products on Product Details page

Started by Genius WebDesign, July 16, 2012, 22:03:34 PM

Previous topic - Next topic

Genius WebDesign

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>



jefchenko

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?

Genius WebDesign

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.

Genius WebDesign

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

dennis.g

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 :)
Wasted enough time in blogs, forums, chats? Hire me! :)

Genius WebDesign

Thanks, Dennis

I will look into this once i have the time.
The code looks promising, though

Menace

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?



Genius WebDesign

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.

Menace

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

Genius WebDesign

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.

Genius WebDesign

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.



dennis.g

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.
Wasted enough time in blogs, forums, chats? Hire me! :)

Genius WebDesign

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

el_carl0s

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?


Genius WebDesign

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.