PrestaShop 1.7前端客户创建制造商可行吗?如何实现?
Absolutely feasible! PrestaShop 1.7’s flexible extension system makes this totally doable—you just need to build a front-end workflow that lets authorized customer groups submit manufacturer details, while keeping your store’s data safe and following core PrestaShop practices. Let’s break this down step by step:
First, a quick recap to ground the work:
- Manufacturers in PrestaShop are managed via the core
Manufacturerclass—always use this instead of direct database queries to avoid breaking core logic (like multi-shop sync or SEO handling). - Note the ID of the customer group you want to authorize: Grab this from your backend’s Customer Groups page (e.g., let’s say our target group ID is
3).
Modifying core files is a bad practice (it breaks updates), so we’ll create a self-contained module called frontmanufactureradd. Here’s the structure:
modules/ └── frontmanufactureradd/ ├── frontmanufactureradd.php ├── controllers/ │ └── front/ │ └── add.php └── views/ └── templates/ ├── front/ │ └── add_form.tpl └── hook/ └── customer_account_link.tpl
2.1 Module Main File (frontmanufactureradd.php)
This registers the module and sets up basic hooks:
<?php if (!defined('_PS_VERSION_')) exit; class FrontManufacturerAdd extends Module { public function __construct() { $this->name = 'frontmanufactureradd'; $this->tab = 'front_office_features'; $this->version = '1.0.0'; $this->author = 'Your Name'; $this->need_instance = 0; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Frontend Manufacturer Submission'); $this->description = $this->l('Let specific customer groups submit manufacturers from the frontend for admin review.'); $this->ps_versions_compliancy = [ 'min' => '1.7.0.0', 'max' => _PS_VERSION_ ]; } public function install() { return parent::install() && $this->registerHook('displayCustomerAccount'); } // Add a link to the customer account page for authorized users public function hookDisplayCustomerAccount($params) { $allowed_group_id = 3; // Replace with your target group ID if (in_array($allowed_group_id, $this->context->customer->getGroups())) { return $this->display(__FILE__, 'views/templates/hook/customer_account_link.tpl'); } return ''; } }
Create controllers/front/add.php—this controls access, shows the form, and processes submissions:
<?php class FrontManufacturerAddAddModuleFrontController extends ModuleFrontController { public function initContent() { parent::initContent(); // Lock access to authorized customers only $allowed_group_id = 3; if (!$this->context->customer->isLogged() || !in_array($allowed_group_id, $this->context->customer->getGroups())) { $this->redirectWithNotifications( $this->context->link->getPageLink('my-account'), $this->l('You don\'t have permission to access this page.') ); return; } // Process form if submitted if (Tools::isSubmit('submit_add_manufacturer')) { $this->processManufacturerSubmission(); } // Pass form action URL to template $this->context->smarty->assign([ 'action_url' => $this->context->link->getModuleLink('frontmanufactureradd', 'add'), ]); // Load the form template $this->setTemplate('module:frontmanufactureradd/views/templates/front/add_form.tpl'); } private function processManufacturerSubmission() { // Grab sanitized form data $name = Tools::getValue('manufacturer_name'); $description = Tools::getValue('manufacturer_description'); $meta_title = Tools::getValue('manufacturer_meta_title'); $meta_desc = Tools::getValue('manufacturer_meta_description'); $meta_keys = Tools::getValue('manufacturer_meta_keywords'); // Validate input $errors = []; if (empty($name)) $errors[] = $this->l('Manufacturer name is required.'); if (Manufacturer::manufacturerExists($name)) $errors[] = $this->l('This manufacturer already exists.'); if (!empty($errors)) { $this->context->smarty->assign('errors', $errors); return; } // Create new manufacturer (set to inactive for admin review) $manufacturer = new Manufacturer(); $manufacturer->name = $name; $manufacturer->description = $description; $manufacturer->meta_title = $meta_title; $manufacturer->meta_description = $meta_desc; $manufacturer->meta_keywords = $meta_keys; $manufacturer->active = 0; // Critical: Let admins approve before it goes live $manufacturer->add(); // Link to current shop (if multi-shop is enabled) if (Shop::isFeatureActive()) { $manufacturer->addToShop($this->context->shop->id); } // Redirect with success message $this->redirectWithNotifications( $this->context->link->getModuleLink('frontmanufactureradd', 'add'), $this->l('Manufacturer submitted successfully! Our team will review it shortly.') ); } }
Create views/templates/front/add_form.tpl for the user-facing form:
{extends file=$layout} {block name='content'} <div class="manufacturer-submit-form"> <h1>{l s='Submit a New Manufacturer' mod='frontmanufactureradd'}</h1> {if isset($errors)} <div class="alert alert-danger"> {foreach from=$errors item=error} <p>{$error}</p> {/foreach} </div> {/if} <form action="{$action_url}" method="post"> <div class="form-group"> <label for="manufacturer_name">{l s='Manufacturer Name' mod='frontmanufactureradd'}</label> <input type="text" id="manufacturer_name" name="manufacturer_name" class="form-control" required> </div> <div class="form-group"> <label for="manufacturer_description">{l s='Description' mod='frontmanufactureradd'}</label> <textarea id="manufacturer_description" name="manufacturer_description" class="form-control"></textarea> </div> <div class="form-group"> <label for="manufacturer_meta_title">{l s='SEO Title' mod='frontmanufactureradd'}</label> <input type="text" id="manufacturer_meta_title" name="manufacturer_meta_title" class="form-control"> </div> <div class="form-group"> <label for="manufacturer_meta_description">{l s='SEO Description' mod='frontmanufactureradd'}</label> <textarea id="manufacturer_meta_description" name="manufacturer_meta_description" class="form-control"></textarea> </div> <div class="form-group"> <label for="manufacturer_meta_keywords">{l s='SEO Keywords' mod='frontmanufactureradd'}</label> <input type="text" id="manufacturer_meta_keywords" name="manufacturer_meta_keywords" class="form-control"> </div> <button type="submit" name="submit_add_manufacturer" class="btn btn-primary">{l s='Submit for Review' mod='frontmanufactureradd'}</button> </form> </div> {/block}
Create views/templates/hook/customer_account_link.tpl to add an entry in the user’s account menu:
<li> <a href="{$link->getModuleLink('frontmanufactureradd', 'add')}"> {l s='Submit Manufacturer' mod='frontmanufactureradd'} </a> </li>
- Add XSS Protection: Use
Tools::cleanHtml()on user-submitted text fields to prevent cross-site scripting attacks. - Logo Upload: If you want users to upload manufacturer logos, add a file input field and use
Tools::fileUpload()+$manufacturer->saveImage()to handle it safely. - Admin Alerts: Add a mail notification in the
processManufacturerSubmission()method to alert admins when a new manufacturer is submitted. - Test Thoroughly: Validate edge cases (empty fields, duplicate names, unauthorized access) in a staging environment before deploying.
内容的提问来源于stack exchange,提问作者fakejack

