Skip to content

How to make an extension to virtuemart which will allow you to add distributors and then chose distributor for each product?

First I’ll try to explain what needs to be achieved. If you have a big online store and you try to remember all your distributors (people and companies which you take your goods from and then you sell these goods to the customers) it won’t be easy. Especially if there are more people working in your online shop. So the issue is that virtuemart doesn’t have distributors support. What needs to be done is write an extension to virtuemart which will add distributors to the menu of modules (manufacturers, tax rates, vendors, ets.).

This extension will allow you to add distributors (like you add manufacturers) and when you are in the product details page you will have the ability to choose the distributor from which this product comes from, the same way like you choose manufacturer of the product.

So I used the manufacturers extension for base to create this distributors extension. And since I have no idea how to make a real extension which will install automatically I will write the steps here:First I’ll try to explain what needs to be achieved. If you have a big online store and you try to remember all your distributors (people and companies which you take your goods from and then you sell these goods to the customers) it won’t be easy. Especially if there are more people working in your online shop. So the issue is that virtuemart doesn’t have distributors support. What needs to be done is write an extension to virtuemart which will add distributors to the menu of modules (manufacturers, tax rates, vendors, ets.).

This extension will allow you to add distributors (like you add manufacturers) and when you are in the product details page you will have the ability to choose the distributor from which this product comes from, the same way like you choose manufacturer of the product.

So I used the manufacturers extension for base to create this distributors extension. And since I have no idea how to make a real extension which will install automatically I will write the steps here:

1. Add one more menu item to the virtumart menu (virtuemart modules)

To do this go to the your-site/administrator/components/com_virtuemart/header.php and add one more menu item.

Find this code: case ‘manufacturer’:

and paste this code before it:

case ‘distributor’:
?>
<h3 class=”title-smenu” title=”distributor” onclick=”SwitchMenu(’<?php echo $modCount ?>’)”>
<?php echo Distributor
?>
</h3>
<div class=”section-smenu”>
<ul>
<li class=”item-smenu vmicon vmicon-16-content”>
<a href=”<?php $sess->purl($_SERVER[‘PHP_SELF’].”?pshop_mode=admin&amp;page=distributor.distributor_list”) ?>”><?php echo “Distributor list” ?></a>
</li>
<li class=”item-smenu vmicon vmicon-16-editadd”>
<a href=”<?php $sess->purl($_SERVER[‘PHP_SELF’].”?pshop_mode=admin&amp;page=distributor.distributor_form”) ?>”><?php echo “Add distributor” ?></a>
<hr />
</li>
</ul>
</div>
<?php
$modCount++;
break;


2. Create a new table in the DB for example called jos_vm_distributor

I have created a table similar to the on for the manufacturers

Field Type Collation Attributes Null Default Extra Action
int(11) No auto_increment
varchar(64) utf8_general_ci Yes NULL
varchar(255) utf8_general_ci Yes NULL
text utf8_general_ci Yes NULL

3. Add the record for the distributors in the jos_vm_module table

Add a record to the DB like this one in the jos_vm_module table. Choose some ID and write the name “distributor”, And don’t forget to add Y to published.

It is possible to do this 3-th step from the virtuemart backend -> admin -> list modules,  but I’m not sure.


4. Add some functions to the newly published module.

Go to virtuemart backend -> admin -> list modules. Go to function list for the distributor module.

Add this functions: distributorAdd, distributorDelete, distributorUpdate (see virtuemart manual for help with adding new functions)

5. Now create the files for the distributor.

– /administrator/components/com_virtuemart/html/distributor.distributor_form.php

– /administrator/components/com_virtuemart/html/distributor.distributor_list.php

-/administrator/components/com_virtuemart/classes/ps_distributor.php

You could download my files from here above.

6. Add the code to support distributors in the product details page.

(this is a helpfull article – Add a New Field to Products in Joomla! VirtueMart)

6.1. First of all we have to ctreate a new table in the database which will link the distributors to the products. In my case it’s called jos_vm_product_dist_xref.

You should do it the same way the  jos_vm_product_mf_xref is fone. Add the two fields and the two indexes to the table.

6.2. Open ps_product.html and add these lines:

6.2.1. After these lines :

if (empty($d[‘manufacturer_id’])) {
$d[‘manufacturer_id’] = “1″;

}

add

if (empty($d[‘distributor_id’])) {
$d[‘distributor_id’] = “1″;
}

6.2.2. Before:

$q = “INSERT INTO #__{vm}_product_mf_xref VALUES (”;
$q .= “‘”.$d[‘product_id’].”‘, ‘”.vmRequest::getInt(’manufacturer_id’).”‘)”;
$db->setQuery($q); $db->query();

add:

$q = “INSERT INTO #__{vm}_product_dist_xref VALUES (”;
$q .= “‘”.$d[‘product_id’].”‘, ‘”.vmRequest::getInt(’distributor_id’).”‘)”;
$db->setQuery($q); $db->query();

6.2.3. After:

$q = “UPDATE #__{vm}_product_mf_xref SET “;
$q .= ‘manufacturer_id=’.vmRequest::getInt(’manufacturer_id’).’ ‘;
$q .= ‘WHERE product_id = ‘.$d[‘product_id’];
$db->query($q);

add:

$q = “UPDATE #__{vm}_product_dist_xref SET “;
$q .= ‘distributor_id=’.vmRequest::getInt(’distributor_id’).’ ‘;
$q .= ‘WHERE product_id = ‘.$d[‘product_id’];
$db->query($q);

6.2.4. After:

/* Delete product – manufacturer xref */
$q = “DELETE FROM #__{vm}_product_mf_xref WHERE product_id=’$product_id’”;
$db->setQuery($q); $db->query();

add:

/* Delete product – distributor xref */
$q = “DELETE FROM #__{vm}_product_dist_xref WHERE product_id=’$product_id’”;
$db->setQuery($q); $db->query();

6.2.5. After this function :

function get_manufacturer_id($product_id) {}

add:

/**
* Function to get the distributor id the product $product_id is assigned to
* @author soeren
* @param int $product_id
* @return int The distributor id
*/
function get_distributor_id($product_id) {
$db = new ps_DB;

$q = “SELECT distributor_id FROM #__{vm}_product_dist_xref “;
$q .= “WHERE product_id=’$product_id’ “;

$db->query($q);
$db->next_record();
if ($db->f(”distributor_id”)) {
return $db->f(”distributor_id”);
}
else {
return false;
}
}

6.2.6. After function get_mf_name($product_id) {}

add

/**
* Functon to get the name of the distributor this product is assigned to
*
* @param int $product_id
* @return string the distributor name
*/
function get_d_name($product_id) {
$db = new ps_DB;

$q = “SELECT d_name,#__{vm}_distributor_kefi.distributor_id FROM #__{vm}_product_dist_xref,#__{vm}_distributor_kefi “;
$q .= “WHERE product_id=’$product_id’ “;
$q .= “AND #__{vm}_distributor_kefi.distributor_id=#__{vm}_product_dist_xref.distributor_id”;

$db->query($q);
$db->next_record();
if ($db->f(”d_name”)) {
return $db->f(”d_name”);
}
else {
return “”;
}
}

/**
* Functon to get the id of the distributor this product is assigned to
*
* @param int $product_id
* @return string the distributor id
*/
function get_d_id($product_id) {
$db = new ps_DB;

$q = “SELECT d_name,#__{vm}_distributor_kefi.distributor_id FROM #__{vm}_product_dist_xref,#__{vm}_distributor_kefi “;
$q .= “WHERE product_id=’$product_id’ “;
$q .= “AND #__{vm}_distributor_kefi.distributor_id=#__{vm}_product_dist_xref.distributor_id”;

$db->query($q);
$db->next_record();
if ($db->f(”distributor_id”)) {
return $db->f(”distributor_id”);
}
else {
return “”;
}
}

6.3. Open product.product_form.php and modify:

6.3.1.After:

require_once( CLASSPATH.’ps_manufacturer.php’ );

add:

require_once( CLASSPATH.’ps_distributor.php’ );

6.3.2. After :

// Get the Manufacturer ID
$db2->query(”SELECT manufacturer_id FROM #__{vm}_product_mf_xref WHERE product_id=’$product_id’”);
$db2->next_record();
$manufacturer_id = $db2->f(”manufacturer_id”);

add:

// Get the Distributor ID
$db2->query(”SELECT distributor_id FROM #__{vm}_product_dist_xref WHERE product_id=’$product_id’”);
$db2->next_record();
$distributor_id = $db2->f(”distributor_id”);

6.3.3. After :

<tr class=”row0″>
<td width=”21%”><div style=”text-align:right;font-weight:bold;”> <?php echo $VM_LANG->_(’PHPSHOP_PRODUCT_FORM_VENDOR’) ?>:</div></td>
<td width=”79%” ><?php ps_vendor::list_vendor($db->sf(”vendor_id”));  ?></td>
</tr>

add:

<tr class=”row1″>
<td width=”21%” ><div style=”text-align:right;font-weight:bold;”> <?php echo “Distributor” ?>:</div></td>
<td width=”79%” ><?php ps_distributor::list_distributor(@$distributor_id);  ?></td>
</tr>

That’s it…

1. Add one more menu item to the virtumart menu (virtuemart modules)

To do this go to the your-site/administrator/components/com_virtuemart/header.php and add one more menu item.

Find this code: case ‘manufacturer’:

and paste this code before it:

case ‘distributor’:
?>
<h3 class=”title-smenu” title=”distributor” onclick=”SwitchMenu(’<?php echo $modCount ?>’)”>
<?php echo Distributor
?>
</h3>
<div class=”section-smenu”>
<ul>
<li class=”item-smenu vmicon vmicon-16-content”>
<a href=”<?php $sess->purl($_SERVER[‘PHP_SELF’].”?pshop_mode=admin&amp;page=distributor.distributor_list”) ?>”><?php echo “Distributor list” ?></a>
</li>
<li class=”item-smenu vmicon vmicon-16-editadd”>
<a href=”<?php $sess->purl($_SERVER[‘PHP_SELF’].”?pshop_mode=admin&amp;page=distributor.distributor_form”) ?>”><?php echo “Add distributor” ?></a>
<hr />
</li>
</ul>
</div>
<?php
$modCount++;
break;


2. Create a new table in the DB for example called jos_vm_distributor

I have created a table similar to the on for the manufacturers

Field Type Collation Attributes Null Default Extra Action
int(11) No auto_increment
varchar(64) utf8_general_ci Yes NULL
varchar(255) utf8_general_ci Yes NULL
text utf8_general_ci Yes NULL

3. Add the record for the distributors in the jos_vm_module table

Add a record to the DB like this one in the jos_vm_module table. Choose some ID and write the name “distributor”, And don’t forget to add Y to published.

It is possible to do this 3-th step from the virtuemart backend -> admin -> list modules,  but I’m not sure.


4. Add some functions to the newly published module.

Go to virtuemart backend -> admin -> list modules. Go to function list for the distributor module.

Add this functions: distributorAdd, distributorDelete, distributorUpdate (see virtuemart manual for help with adding new functions)

5. Now create the files for the distributor.

– /administrator/components/com_virtuemart/html/distributor.distributor_form.php

– /administrator/components/com_virtuemart/html/distributor.distributor_list.php

-/administrator/components/com_virtuemart/classes/ps_distributor.php

You could download my files from here above.

6. Add the code to support distributors in the product details page.

(this is a helpfull article – Add a New Field to Products in Joomla! VirtueMart)

6.1. First of all we have to ctreate a new table in the database which will link the distributors to the products. In my case it’s called jos_vm_product_dist_xref.

You should do it the same way the  jos_vm_product_mf_xref is fone. Add the two fields and the two indexes to the table.

6.2. Open ps_product.html and add these lines:

6.2.1. After these lines :

if (empty($d[‘manufacturer_id’])) {
$d[‘manufacturer_id’] = “1″;

}

add

if (empty($d[‘distributor_id’])) {
$d[‘distributor_id’] = “1″;
}

6.2.2. Before:

$q = “INSERT INTO #__{vm}_product_mf_xref VALUES (”;
$q .= “‘”.$d[‘product_id’].”‘, ‘”.vmRequest::getInt(’manufacturer_id’).”‘)”;
$db->setQuery($q); $db->query();

add:

$q = “INSERT INTO #__{vm}_product_dist_xref VALUES (”;
$q .= “‘”.$d[‘product_id’].”‘, ‘”.vmRequest::getInt(’distributor_id’).”‘)”;
$db->setQuery($q); $db->query();

6.2.3. After:

$q = “UPDATE #__{vm}_product_mf_xref SET “;
$q .= ‘manufacturer_id=’.vmRequest::getInt(’manufacturer_id’).’ ‘;
$q .= ‘WHERE product_id = ‘.$d[‘product_id’];
$db->query($q);

add:

$q = “UPDATE #__{vm}_product_dist_xref SET “;
$q .= ‘distributor_id=’.vmRequest::getInt(’distributor_id’).’ ‘;
$q .= ‘WHERE product_id = ‘.$d[‘product_id’];
$db->query($q);

6.2.4. After:

/* Delete product – manufacturer xref */
$q = “DELETE FROM #__{vm}_product_mf_xref WHERE product_id=’$product_id’”;
$db->setQuery($q); $db->query();

add:

/* Delete product – distributor xref */
$q = “DELETE FROM #__{vm}_product_dist_xref WHERE product_id=’$product_id’”;
$db->setQuery($q); $db->query();

6.2.5. After this function :

function get_manufacturer_id($product_id) {}

add:

/**
* Function to get the distributor id the product $product_id is assigned to
* @author soeren
* @param int $product_id
* @return int The distributor id
*/
function get_distributor_id($product_id) {
$db = new ps_DB;

$q = “SELECT distributor_id FROM #__{vm}_product_dist_xref “;
$q .= “WHERE product_id=’$product_id’ “;

$db->query($q);
$db->next_record();
if ($db->f(”distributor_id”)) {
return $db->f(”distributor_id”);
}
else {
return false;
}
}

6.2.6. After function get_mf_name($product_id) {}

add

/**
* Functon to get the name of the distributor this product is assigned to
*
* @param int $product_id
* @return string the distributor name
*/
function get_d_name($product_id) {
$db = new ps_DB;

$q = “SELECT d_name,#__{vm}_distributor_kefi.distributor_id FROM #__{vm}_product_dist_xref,#__{vm}_distributor_kefi “;
$q .= “WHERE product_id=’$product_id’ “;
$q .= “AND #__{vm}_distributor_kefi.distributor_id=#__{vm}_product_dist_xref.distributor_id”;

$db->query($q);
$db->next_record();
if ($db->f(”d_name”)) {
return $db->f(”d_name”);
}
else {
return “”;
}
}

/**
* Functon to get the id of the distributor this product is assigned to
*
* @param int $product_id
* @return string the distributor id
*/
function get_d_id($product_id) {
$db = new ps_DB;

$q = “SELECT d_name,#__{vm}_distributor_kefi.distributor_id FROM #__{vm}_product_dist_xref,#__{vm}_distributor_kefi “;
$q .= “WHERE product_id=’$product_id’ “;
$q .= “AND #__{vm}_distributor_kefi.distributor_id=#__{vm}_product_dist_xref.distributor_id”;

$db->query($q);
$db->next_record();
if ($db->f(”distributor_id”)) {
return $db->f(”distributor_id”);
}
else {
return “”;
}
}

6.3. Open product.product_form.php and modify:

6.3.1.After:

require_once( CLASSPATH.’ps_manufacturer.php’ );

add:

require_once( CLASSPATH.’ps_distributor.php’ );

6.3.2. After :

// Get the Manufacturer ID
$db2->query(”SELECT manufacturer_id FROM #__{vm}_product_mf_xref WHERE product_id=’$product_id’”);
$db2->next_record();
$manufacturer_id = $db2->f(”manufacturer_id”);

add:

// Get the Distributor ID
$db2->query(”SELECT distributor_id FROM #__{vm}_product_dist_xref WHERE product_id=’$product_id’”);
$db2->next_record();
$distributor_id = $db2->f(”distributor_id”);

6.3.3. After :

<tr class=”row0″>
<td width=”21%”><div style=”text-align:right;font-weight:bold;”> <?php echo $VM_LANG->_(’PHPSHOP_PRODUCT_FORM_VENDOR’) ?>:</div></td>
<td width=”79%” ><?php ps_vendor::list_vendor($db->sf(”vendor_id”));  ?></td>
</tr>

add:

<tr class=”row1″>
<td width=”21%” ><div style=”text-align:right;font-weight:bold;”> <?php echo “Distributor” ?>:</div></td>
<td width=”79%” ><?php ps_distributor::list_distributor(@$distributor_id);  ?></td>
</tr>

That’s it…

Published inCoding

2 Comments

  1. juraj

    Hello, I just read your blog (https://www.kavdesign.net/blog/programming/how-to-make-an-extension-to-virtuemart-which-will-allow-you-to-add-distributors-to-the-products/).
    Could you please send me the scripts:
    /administrator/components/com_virtuemart/html/distributor.distributor_form.php
    /administrator/components/com_virtuemart/html/distributor.distributor_list.php
    /administrator/components/com_virtuemart/classes/ps_distributor.php
    I need this solution very much.
    Thanks.

  2. admin

    This was a long time ago on a test server and I don’t have the real codes anymore. If you can understand something from the above, that’s all I can help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*