Since we don’t have a feature in MultiMerch that would allow the store owner to define custom seller account fields, here’s a quick tutorial on adding them manually in about 5 simple steps. A separate vQmod xml file is included in the end of the tutorial – use it instead of overwriting MultiMerch and OpenCart core files!
Let’s say we want add a new text field called Phone to make it possible for our sellers to specify their phone number and display it in their profile.
1. Modifying the database
First we need to modify the database to include our new field. MultiMerch stores seller information in the ms_seller table, so we’ll use phpMyAdmin to create the new field. Alternatively, we can also do it manually by using the following SQL command:
ALTER TABLE `ms_seller` ADD `phone` VARCHAR(35) NOT NULL ;
In case we ever want to reinstall MultiMerch and want this field to get created automatically during installation, we’ll need to modify MultiMerch installation model and add our new field to the ms_seller CREATE statement:
admin/model/multiseller/install.php ~65
$this->db->query(" CREATE TABLE `" . DB_PREFIX . "ms_seller` ( `seller_id` int(11) NOT NULL AUTO_INCREMENT, <..> `phone` VARCHAR(35) NOT NULL DEFAULT '', <..> `commission_id` int(11) DEFAULT NULL, PRIMARY KEY (`seller_id`)) default CHARSET=utf8");
This will create the new phone field will be created when MultiMerch is installed.
2. Adding the new field to the seller model
We’ve modified the database, but MultiMerch doesn’t know about it yet, so we need to modify the seller model to read and write information to our new field.
There are 4 functions we’re interested in: getSeller, getSellers, createSeller & editSeller. We simply add the new field to all of the functions:
system/library/msseller.php
public function getSeller($seller_id, $data = array()) { $sql = "SELECT CONCAT(c.firstname, ' ', c.lastname) as name, c.email as 'c.email', ms.seller_id as 'seller_id', ms.phone as 'ms.phone',
public function getSellers($data = array(), $sort = array(), $cols = array()) { <..> $sql = "SELECT <..> // default columns ." CONCAT(c.firstname, ' ', c.lastname) as 'c.name', c.email as 'c.email', ms.phone as 'ms.phone',
public function createSeller($data) { <..> $sql = "INSERT INTO " . DB_PREFIX . "ms_seller SET seller_id = " . (int)$data['seller_id'] . ", <..> nickname = '" . $this->db->escape($data['nickname']) . "', phone = '" . (isset($data['phone']) ? $this->db->escape($data['phone']) : '') . "',
public function editSeller($data) { <..> $sql = "UPDATE " . DB_PREFIX . "ms_seller SET description = '" . $this->db->escape($data['description']) . "', <..> nickname = '" . $this->db->escape($data['nickname']) . "', phone = '" . (isset($data['phone']) ? $this->db->escape($data['phone']) : '') . "',
That’s it! MultiMerch now knows about our new field and will store and retrieve the new data from the database.
3. Displaying the field in the account template
We can now proceed to modifying the seller account template to tell our sellers about the new phone field they can use. We’ll add the new phone field before the company field.
catalog/view/theme/default/template/multiseller/account-profile.tpl ~60
<div class="form-group"> <label class="col-sm-2 control-label"><?php echo $ms_account_sellerinfo_phone; ?></label> <div class="col-sm-10"> <input type="text" class="form-control" name="seller[phone]" value="<?php echo $seller['ms.phone']; ?>" /> <p class="ms-note"><?php echo $ms_account_sellerinfo_phone_note; ?></p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label"><?php echo $ms_account_sellerinfo_company; ?></label>
As you can see, we’re using language variables $ms_account_sellerinfo_phone and $ms_account_sellerinfo_phone_note to display field name and description. Let’s add them to our language file:
catalog/language/english/multiseller/multiseller.php ~420
$_['ms_account_sellerinfo_phone'] = 'Phone'; $_['ms_account_sellerinfo_phone_note'] = 'Your phone number (optional)'; $_['ms_account_sellerinfo_company'] = 'Company'; $_['ms_account_sellerinfo_company_note'] = 'Your company (optional)';
Try refreshing the seller profile account page now – the new field should be visible.
4. Adding some validation to the account controller
Now let’s add just a little validation. OpenCart (and MultiMerch) follows the MVC pattern that uses controllers to pass data from template to models. MultiMerch uses the jxSaveSellerInfo function in the account-profile.php controller to perform things like form validation, so we’ll modify it a little.
catalog/controller/seller/account-profile.php ~90
if (mb_strlen($data['seller']['phone']) > 35 ) { $json['errors']['seller[phone]'] = $this->language->get('ms_error_sellerinfo_phone_length'); } if (mb_strlen($data['seller']['company']) > 50 ) {
We’ll also add the error message to the language file to make it translatable.
catalog/language/english/multiseller/multiseller.php ~237
$_['ms_error_sellerinfo_phone_length'] = 'Phone number cannot be longer than 35 characters'; $_['ms_error_sellerinfo_company_length'] = 'Company name cannot be longer than 50 characters';
This simple check will warn the seller if he tries to enter too much information in the phone field. You can use any of the phone number regular expressions available in the web instead for a more advanced validation.
5. Showing the new data in the public seller profile
Now our sellers can specify their phone number, so let’s display it on their profile in store.
catalog/controller/seller/catalog-seller.php ~233
$this->data['seller']['phone'] = $seller['ms.phone']; $this->data['seller']['nickname'] = $seller['ms.nickname'];
catalog/view/theme/default/template/multiseller/catalog-seller-profile.tpl ~71
<?php if (isset($seller['phone']) && $seller['phone']) { ?><li><?php echo $ms_catalog_seller_profile_phone; ?> <?php echo $seller['phone']; ?></li><?php } ?> <?php if (isset($seller['company']) && $seller['company']) { ?><li><?php echo $ms_catalog_seller_profile_company; ?> <?php echo $seller['company']; ?></li><?php } ?>
catalog/language/english/multiseller/multiseller.php ~658
$_['ms_catalog_seller_profile_phone'] = 'Phone:'; $_['ms_catalog_seller_profile_company'] = 'Company:';
That’s pretty much it! Your sellers can now specify their phone number and display it in their public profile.
You can use this guide to create all kinds of extra seller fields until we implement a separate feature to do this. Also, we strongly suggest you use vQmod to keep the changes out of Multimerch core files so that you don’t lose them during upgrade. For this matter, we’ve created a ready-made vQmod file based on this tutorial for you to use – grab it here!
P.S. We also have our very own MultiMerch Community Forums – drop by and share your thoughts about this article and the rest of MultiMerch!
The post Creating custom seller account fields in MultiMerch appeared first on MultiMerch Marketplace.