Author Topic: Child products table view solution for Virtuemart 1.1  (Read 11049 times)

ogosense

  • Beginner
  • *
  • Posts: 3
Child products table view solution for Virtuemart 1.1
« on: March 25, 2011, 19:27:49 pm »
Hi all,

I managed to implement a child products table view (in a way) for VirtueMart 1.1.7 without hacking the component. I had to implement it at http://www.ogosense.com, and since a lot of people asked for this, I decided to share my solution on the forum.

Instructions:

1. Open your theme.php file (components/com_virtuemart/themes/<your theme>/theme.php)

2. add this method to the vmTheme class

Code: [Select]
        /* Function for loading table view of child products
* made by Drazen
*/
function displayTable($product_id) {
global $VM_LANG, $CURRENCY_DISPLAY,$mm_action_url,$sess,$auth;
                global $mainframe;
require_once (CLASSPATH . 'ps_product.php' );
$ps_product = new ps_product;
require_once (CLASSPATH . 'ps_product_attribute.php' );
$ps_product_attribute = new ps_product_attribute;
                require_once(CLASSPATH . 'ps_product_type.php' );
                $ps_product_type = new ps_product_type;
                require_once(CLASSPATH . 'ps_product_category.php' );
                $ps_product_category = new ps_product_category;
$Itemid = JRequest::getInt( 'Itemid', 1 );
$db = new ps_DB;
//output
$html = '';

$children = $ps_product->get_child_product_ids($product_id); //load children ids
//Table header
$headt = '<div><table style="border: 0px solid; width: 100%;"><tr><td class="tblcol3" ><b>Description</b></td>';

//Some control variables
$mdheader = true; //should the table header be generated
$i = 1;           //counter

if (empty($children)) $children[0] = $product_id;

foreach ($children as $cid) {
//Choose a background color (use two colors for rows)
$colorval = '';
if ($i%2==1) $colorval='#f0f0f0';
else $colorval = '#f9f9f9';
$i++;
$html .= '<div style="background-color: '.$colorval.'">';
//adding form
$html .= '<form action="'.$mm_action_url.'index.php" method="post" name="addtocart" id="'.uniqid('addtocart_').'" class="addtocart_form"'.(( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify )?'onsubmit="handleAddToCart( this.id );return false;"':'').'>';
//name
$html .= '<table style="border: 0px solid; width: 100%;"><tr><td class="tblcol3">';
$html .= '<a href="index.php?option=com_virtuemart&Itemid='.$Itemid.'&lang=en&page=shop.product_details&product_id='.$cid.'">'.$ps_product->get_field($cid,'product_name').'</a></td>';
//loading attributes
$padb = $ps_product->attribute_sql($cid,$product_id);
while( $padb->next_record() ) {
if ($mdheader) { $headt .= '<td class="tblcol" ><b>'.$padb->f( "attribute_name" ) . "</b></td>" ; }
$html .= "<td class=\"tblcol\" >" . $padb->f( "attribute_value" ) . "</td>" ;
}
if ($mdheader) {
$headt .= '<td class="tblcol" ><b>Price</b></td><td class="tblcol2" ><b>Quantity</b></td></tr></table></div>';
$mdheader = false;
}
// now get item price
if( $_SESSION['auth']['show_prices'] ) {
$price = $ps_product->get_price( $cid ) ;
$price["product_price"] = $GLOBALS['CURRENCY']->convert( $price["product_price"], $price["product_currency"] ) ;
if( $_SESSION["auth"]["show_price_including_tax"] == 1 ) {
$tax_rate = 1 + $ps_product->get_product_taxrate( $db->f( "product_id" ) ) ;
$price['product_price'] *= $tax_rate ;
}
$html .= '<td class="tblcol" >' . $CURRENCY_DISPLAY->getFullValue( $price["product_price"] ) .'</td>';
}

//show quantity box
$html .= '<td style="width: 175px; text-align: right;" >'.$ps_product_attribute->show_quantity_box( $product_id, $cid, $child = true, $use_parent = 'Y' );

//submit button
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';

if( CHECK_STOCK == '1' && ( $product_in_stock < 1) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
}

$html .= '<input style="text-align: right; width: 70px; height: 35px; cursor: pointer; border: medium none; font-weight: bold; font-family: inherit; background: url(./components/com_virtuemart/shop_image/ps_image/add-to-cart_orange.gif) no-repeat scroll left center transparent; vertical-align: middle; overflow: hidden; float: none;" type="submit" class="'.$button_cls.'" value="" title="'.$button_lbl.'" />';
$html .= '</td></tr></table>';
//get advanced and/or custom attributes
$html .= '<div style="border-top: 1px solid #F6F4ED;"><table style="border: 0px solid; width: 100%; text-align: right;"><tr style="float: right;"><td>';
$atts = $ps_product_attribute->list_advanced_attribute($cid);

//align attributes in one row
// -- this part replaces <br> tags so that the attributes can be displayed in a one row
while (preg_match('/<br.+?>$/',$atts)>0) $atts = preg_replace('/<br.+?>$/','',$atts); //remove <br> on bottom
$atts = preg_replace('/<br.+?>/','</td><td>',$atts);
$html .= $atts.'</td></tr></table></div>';

//other form data
$html .= '    <input type="hidden" name="flypage" value="shop.'.$flypage.'" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="manufacturer_id" value="" />
<input type="hidden" name="category_id" value="" />
<input type="hidden" name="func" value="cartAdd" />
<input type="hidden" name="product_id" value="'.$product_id.'" />
<input type="hidden" name="prod_id[]" value="'.$cid.'" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="Itemid" value="'.$Itemid.'" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />';
$html.='</form></div><br>'; //<br> is used to make some space between rows
}

return $headt.$html;
}

3. open file components/com_virtuemart/themes/<your theme>/templates/product_details/flypage.tpl.php

4. replace
Code: [Select]
<?php echo $addtocart?>with
Code: [Select]
<?php echo $this->displayTable($product_id); ?>

Note that this code uses some inline styles and classes that may not suit your needs. But you're free to modify this code to satisfy your own needs :)

Cheers,

Drazen

[attachment cleanup by admin]

albertopinguino

  • Beginner
  • *
  • Posts: 1
Re: Child products table view solution for Virtuemart 1.1
« Reply #1 on: April 15, 2011, 10:40:00 am »
Hi Drazen,
I'd like to add also the thumbnail image for each child product. Can you please help me?

Templates007

  • Beginner
  • *
  • Posts: 9
Re: Child products table view solution for Virtuemart 1.1
« Reply #2 on: April 21, 2011, 21:52:12 pm »
Hello albertopinguino,
You can get a ready solution at www.templates007.com

mattb123

  • Beginner
  • *
  • Posts: 2
Re: Child products table view solution for Virtuemart 1.1
« Reply #3 on: October 11, 2011, 12:32:51 pm »
Life saver, saved so much work for me.

Thanks!

aashu

  • Beginner
  • *
  • Posts: 2
Re: Child products table view solution for Virtuemart 1.1
« Reply #4 on: November 16, 2011, 07:42:29 am »
thankyou very much ogosense,
you saved me a lot of time.

dangelo

  • Beginner
  • *
  • Posts: 28
Re: Child products table view solution for Virtuemart 1.1
« Reply #5 on: November 17, 2011, 17:15:36 pm »
Thank you so much for this!!!

I've put it on my gold VM list!!!!

one small addition for your class...

i have modified so the prices wont show up on my unregistered clients :D
Code: [Select]
/* Function for loading table view of child products
* made by Drazen
*/
function displayTable($product_id) {
global $VM_LANG, $CURRENCY_DISPLAY,$mm_action_url,$sess,$auth;
                global $mainframe;
require_once (CLASSPATH . 'ps_product.php' );
$ps_product = new ps_product;
require_once (CLASSPATH . 'ps_product_attribute.php' );
$ps_product_attribute = new ps_product_attribute;
                require_once(CLASSPATH . 'ps_product_type.php' );
                $ps_product_type = new ps_product_type;
                require_once(CLASSPATH . 'ps_product_category.php' );
                $ps_product_category = new ps_product_category;
$Itemid = JRequest::getInt( 'Itemid', 1 );
$db = new ps_DB;
//output
$html = '';

$children = $ps_product->get_child_product_ids($product_id); //load children ids
//Table header
$headt = '<div style="margin-left:8px; margin-right:8px; border-radius:5px 5px 0 0; background-color:#038273; color:#fff;"><table style="border: 0px solid; width: 100%;"><tr><td class="tblcol3" ><b>Περιγραφή</b></td>';

//Some control variables
$mdheader = true; //should the table header be generated
$i = 1;           //counter

if (empty($children)) $children[0] = $product_id;
if ($_SESSION['auth']['user_id'] < 1) {
foreach ($children as $cid) {
//Choose a background color (use two colors for rows)
$colorval = '';
if ($i%2==1) $colorval='#E9E6DC';
else $colorval = '#c2c1be';
$i++;
$html .= '<div style="background-color: '.$colorval.'; margin-left:8px; margin-right:8px;">';
//adding form
$html .= '<form action="'.$mm_action_url.'index.php" method="post" name="addtocart" id="'.uniqid('addtocart_').'" class="addtocart_form"'.(( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify )?'onsubmit="handleAddToCart( this.id );return false;"':'').'>';
//name
$html .= '<table style="border: 0px solid; width: 100%;"><tr><td class="tblcol3">';
$html .= '<span>'.$ps_product->get_field($cid,'product_name').'</span></td>';
//loading attributes
$padb = $ps_product->attribute_sql($cid,$product_id);
while( $padb->next_record() ) {
if ($mdheader) { $headt .= '<td class="tblcol" ><b>'.$padb->f( "" ) . "</b></td>" ; }
$html .= "<td class=\"tblcol\" >" . $padb->f( "attribute_value" ) . "</td>" ;
}
if ($mdheader) {
$headt .= '<td class="tblcol" ></td><td class="tblcol2" ></td></tr></table></div>';
$mdheader = false;
}
// now get item price
if( $_SESSION['auth']['show_prices'] ) {
$price = $ps_product->get_price( $cid ) ;
$price["product_price"] = $GLOBALS['CURRENCY']->convert( $price["product_price"], $price["product_currency"] ) ;
if( $_SESSION["auth"]["show_price_including_tax"] == 1 ) {
$tax_rate = 1 + $ps_product->get_product_taxrate( $db->f( "product_id" ) ) ;
$price['product_price'] *= $tax_rate ;
}
$html .= '<td class="tblcol"  style="color:#000; font-weight:bold;">' . $CURRENCY_DISPLAY->getFullValue( $price["product_price"] ) .'</td>';
}

//show quantity box
$html .= '<td class="tblcol2">';

//submit button
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';

if( CHECK_STOCK == '1' && ( $product_in_stock < 1) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
}

$html .= '';
$html .= '</td></tr></table>';
//get advanced and/or custom attributes
$html .= '';
$atts = '';

//align attributes in one row
// -- this part replaces <br> tags so that the attributes can be displayed in a one row
while (preg_match('/<br.+?>$/',$atts)>0) $atts = preg_replace('/<br.+?>$/','',$atts); //remove <br> on bottom
$atts = preg_replace('/<br.+?>/','</td><td>',$atts);
$html .= $atts.'';

//other form data
$html .= '    <input type="hidden" name="flypage" value="shop.'.$flypage.'" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="manufacturer_id" value="" />
<input type="hidden" name="category_id" value="" />
<input type="hidden" name="func" value="cartAdd" />
<input type="hidden" name="product_id" value="'.$product_id.'" />
<input type="hidden" name="prod_id[]" value="'.$cid.'" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="Itemid" value="'.$Itemid.'" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />';
$html.='</form></div>'; //<br> is used to make some space between rows
}

return $headt.$html;

} else {

foreach ($children as $cid) {
//Choose a background color (use two colors for rows)
$colorval = '';
if ($i%2==1) $colorval='#E9E6DC';
else $colorval = '#c2c1be';
$i++;
$html .= '<div style="background-color: '.$colorval.'; margin-left:8px; margin-right:8px;">';
//adding form
$html .= '<form action="'.$mm_action_url.'index.php" method="post" name="addtocart" id="'.uniqid('addtocart_').'" class="addtocart_form"'.(( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify )?'onsubmit="handleAddToCart( this.id );return false;"':'').'>';
//name
$html .= '<table style="border: 0px solid; width: 100%;"><tr><td class="tblcol3">';
$html .= '<span>'.$ps_product->get_field($cid,'product_name').'</span></td>';
//loading attributes
$padb = $ps_product->attribute_sql($cid,$product_id);
while( $padb->next_record() ) {
if ($mdheader) { $headt .= '<td class="tblcol" ><b>'.$padb->f( "" ) . "</b></td>" ; }
$html .= "<td class=\"tblcol\" >" . $padb->f( "attribute_value" ) . "</td>" ;
}
if ($mdheader) {
$headt .= '<td class="tblcol" ><b>Αξία</b></td><td class="tblcol2" ><b>Ποσότητα</b></td></tr></table></div>';
$mdheader = false;
}
// now get item price
if( $_SESSION['auth']['show_prices'] ) {
$price = $ps_product->get_price( $cid ) ;
$price["product_price"] = $GLOBALS['CURRENCY']->convert( $price["product_price"], $price["product_currency"] ) ;
if( $_SESSION["auth"]["show_price_including_tax"] == 1 ) {
$tax_rate = 1 + $ps_product->get_product_taxrate( $db->f( "product_id" ) ) ;
$price['product_price'] *= $tax_rate ;
}
$html .= '<td class="tblcol"  style="color:#000; font-weight:bold;">' . $CURRENCY_DISPLAY->getFullValue( $price["product_price"] ) .'</td>';
}

//show quantity box
$html .= '<td class="tblcol2">'.$ps_product_attribute->show_quantity_box( $product_id, $cid, $child = true, $use_parent = 'Y' );

//submit button
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';

if( CHECK_STOCK == '1' && ( $product_in_stock < 1) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
}

$html .= '<input style="text-align: right; width: 50px; height: 50px; cursor: pointer; border: medium none; font-weight: bold; font-family: inherit; background: url(./components/com_virtuemart/shop_image/ps_image/add-to-cart_orange.png) no-repeat scroll left center transparent; vertical-align: middle; overflow: hidden; float: none;" type="submit" class="'.$button_cls.'" value="" title="'.$button_lbl.'" />';
$html .= '</td></tr></table>';
//get advanced and/or custom attributes
$html .= '<div style="border-top: 1px solid #038273;"><table style="border: 0px solid; width: 100%; text-align: right;"><tr style="float: right;"><td>';
$atts = $ps_product_attribute->list_advanced_attribute($cid);

//align attributes in one row
// -- this part replaces <br> tags so that the attributes can be displayed in a one row
while (preg_match('/<br.+?>$/',$atts)>0) $atts = preg_replace('/<br.+?>$/','',$atts); //remove <br> on bottom
$atts = preg_replace('/<br.+?>/','</td><td>',$atts);
$html .= $atts.'</td></tr></table></div>';

//other form data
$html .= '    <input type="hidden" name="flypage" value="shop.'.$flypage.'" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="manufacturer_id" value="" />
<input type="hidden" name="category_id" value="" />
<input type="hidden" name="func" value="cartAdd" />
<input type="hidden" name="product_id" value="'.$product_id.'" />
<input type="hidden" name="prod_id[]" value="'.$cid.'" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="Itemid" value="'.$Itemid.'" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />';
$html.='</form></div>'; //<br> is used to make some space between rows
}

return $headt.$html;
}
}

Thank you again for this EXELENT piece of code!!!


dangelo

  • Beginner
  • *
  • Posts: 28
Re: Child products table view solution for Virtuemart 1.1
« Reply #6 on: November 24, 2011, 14:52:54 pm »
I have used your code and modified as i said on earlier posts...

But the code has weakness... it does not display correct values when applying discount to child products...

so i have added the following to your code to fill it in correct...

there is an if to check wheter the product has discount or not and an extra variable ($discount) to fetch the discounted price from the ps_product

-----------------------------------------------------------------
// now get item price
         if ( $_SESSION['auth']['show_prices'] ) {
            $price = $ps_product->get_price( $cid ) ;
            $discount = $ps_product->get_adjusted_attribute_price( $cid ) ;
            if ($price == $discount ) {
            $price["product_price"] = $GLOBALS['CURRENCY']->convert( $price["product_price"], $price["product_currency"] ) ;
            if( $_SESSION["auth"]["show_price_including_tax"] == 1 ) {
               $tax_rate = 1 + $ps_product->get_product_taxrate( $db->f( "product_id" ) ) ;
               $price['product_price'] *= $tax_rate ;
            }
            $html .= '<td class="tblcol"  style="color:#000; font-weight:bold;">' . $CURRENCY_DISPLAY->getFullValue( $price["product_price"] ) .'</td>';
            } else {
            $price["product_price"] = $GLOBALS['CURRENCY']->convert( $price["product_price"], $price["product_currency"] ) ;
            $discount["product_price"] = $GLOBALS['CURRENCY']->convert( $discount["product_price"], $discount["product_currency"] ) ;
            if( $_SESSION["auth"]["show_price_including_tax"] == 1 ) {
            $tax_rate = 1 + $ps_product->get_product_taxrate( $db->f( "product_id" ) ) ;
            $price['product_price'] *= $tax_rate ;
            }
            $html .= '<td class="tblcol"  style="color:#000; font-weight:bold;"><span style="text-decoration:line-through; color:#D60606; margin-right:10px;">' .$CURRENCY_DISPLAY->getFullValue( $price["product_price"] ).'</span>' .$CURRENCY_DISPLAY->getFullValue( $discount["product_price"] ) . '</td>';
            }

----------------------------------------------------------------------

thats it..

cheers

dorac

  • Beginner
  • *
  • Posts: 6
Re: Child products table view solution for Virtuemart 1.1
« Reply #7 on: December 07, 2011, 08:01:52 am »
Hi,

Excellent code!!

I'm trying to use this on my product list page, i only have a small number of child products per parent product so using only the product list browse page would be far beneficial to me than using the product details page.

When using this code in my product list page, it works to some extent. it grabs the last product id instead of every products id.

totally confused about this :( where am I going wrong?

Code: [Select]

        <h3 style="text-align:center;">
            <?php echo $product_name ?>
        </h3>
       
        <div style="text-align:center;">
       
        <img src="<?php echo $product_thumb_image ?>" />

        </div>
        <div style="text-align:center;">
     
            <?php echo $product_s_desc ?>&nbsp;
            <!--<a href="<?php echo $product_flypage ?>" title="<?php echo $product_details ?>"><br />
<?php echo $product_details ?>...</a> -->
        </div>
        <br />
        <?php echo $this->displayTable($product_id); ?>
        <br /><?php echo $product_id ?>


Limonbay

  • Jr. Member
  • **
  • Posts: 54
  • Piercing life
    • Piercings
Re: Child products table view solution for Virtuemart 1.1
« Reply #8 on: December 07, 2011, 19:06:03 pm »
Hi,

Excellent code!!

Thank you so much!  :)
Piercings y complementos
http://www.limonbay.com
Joomla 2.5.14
Virtuemart 2.0.24

saag_dreams@yahoo.com

  • Beginner
  • *
  • Posts: 4
Re: Child products table view solution for Virtuemart 1.1
« Reply #9 on: January 05, 2012, 23:00:00 pm »
Can you please explain,,how can i show mine attributes to the short description column or align the attributes in 1 straight line.

martinewg

  • Beginner
  • *
  • Posts: 1
Re: Child products table view solution for Virtuemart 1.1
« Reply #10 on: March 02, 2012, 01:29:31 am »
This code show incorrect price and tax value in WM 1.1.9. Any sugestion how fix it?