News:

Support the VirtueMart project and become a member

Main Menu

Child Products

Started by Heikner, September 02, 2014, 00:08:50 AM

Previous topic - Next topic

Heikner

Hi,

i am just getting mad trying to find a way to show child product fields on parrent's deatils view.
I'm working on a catalogue VM and would like to show fields of child products like the product length. I did this a long time ago with VM1 but i don't know how to do it with VM2.

Didt't came closer yet then finding out that the child product listbox is implemented by $this->product->customfieldsCart as $field ... $field->display

Is there an object with information and fileds of child products? At least the SKU? I would like to avoid any core hacks.

I would be so happy to get a small hint to solve this problem.

Thanks
Heikner

Heikner

Hi,

as I didn't found a solution I coded my own one. It gets the data directly from the db, but it could be worse. It's done for custom fields only, but it shouldn't be that complicated to extend it:


<?php
 
class child_object{
var $child_id;
var $dbprefix;
var $db;

public function custom_field_by_name($field_name) {
$this->db->setQuery("SELECT virtuemart_custom_id FROM ".$this->dbprefix ."virtuemart_customs WHERE custom_title = '".$field_name."';");
$this->db->query();
$field_id $this->db->loadResult();
return($this->custom_field_by_id($field_id));
}

public function custom_field_by_id($field_id) {

$this->db->setQuery("SELECT custom_value FROM ".$this->dbprefix ."virtuemart_product_customfields WHERE virtuemart_product_id ='".$this->child_id."' AND virtuemart_custom_id = '".$field_id."';");
$this->db->query();
return ($this->db->loadResult());
}

function __construct($p_child_id) {
$this->db JFactory::getDBO();
$this->child_id $p_child_id;
$app JFactory::getApplication(); 
$this->dbprefix $app->getCfg('dbprefix');
}
}

 
 class 
children_products{
var $dbprefix;
var $parrent_id;
public $child = array();
var $db;

function __construct($pparrent_id) {
$app JFactory::getApplication(); 
$this->dbprefix $app->getCfg('dbprefix');
$this->parrent_id $pparrent_id;
$this->db JFactory::getDBO();
$this->getchildren();
}
 
function getchildren()
{
$this->db->setQuery("SELECT virtuemart_product_id FROM `".$this->dbprefix."virtuemart_products` WHERE `product_parent_id` ="$this->parrent_id .";");
$C_IDs $this->db->loadResultArray(0);
foreach ($C_IDs as $C_ID) {
$child_o = new child_object($C_ID);
array_push($this->child,$child_o);
}
}
 }
 
?>



You can instantiate the clases like this:

<?php
$children 
= new children_products($this->product->virtuemart_product_id);
?>


$this->product->virtuemart_product_id is the id of the parent product.

And for example echo the value of the custom field "my_field" for all children:


<?php
foreach ($children->child as $child) {
echo 
$child->custom_field_by_name("my_field");

}
?>



Alternativly you can use $child->custom_field_by_id($my_field_id);

tazcabe


Hi, I just read what you did and is very similar to something I need to do in a page I'm doing, generate a query which I get the information I need to show in the view of product, but Tegmo trouble generating the button purchase for each of them, quiisera to do so by reference to the virtuemart_product_id.

You will put your Products bonton buy?

If so and hopefully you can share the method you used to solve this probleba stop.

Greetings and thanks.