Additional field in the product card OpenCart

An article by our developer from his personal blog.





While developing a module for OpenCart, it became necessary to make an arbitrary field in the product card, which should be visible  only in the admin panel . This should be a field with the  boolean value  "marked or not". So in the end:





Additional field in the product card OpenCart
Additional field in the product card OpenCart

After briefly digging into the admin panel, I found out that  out of the box  OpenCart does not support custom fields, but there are paid modules, for example, here . So you need to make a custom field in OpenCart yourself. 





Options for solving the problem :





  • use unused fields  (sku, upc, ean, jan, isbn, mpn) -  almost instantly , but maybe one of our clients will use the field we have occupied.





  • interfering with the engine code  is  quick and little code , but then the solution is not portable and when the engine is updated, the solution itself will have to be updated, since the changes will be lost when the engine is updated.





  •  - , , OpenCart,  ( ).





ProductMarkedField. :





  •   .





  •   OpenCart.





  •  





     .





opencart , .





  admin/controller/extension/module/productmarkedfield.php. "" ( /)   admin/language/ru-ru/extension/module/productmarkedfield.php  :





<?php $_['heading_title'] = '     ""';
      
      



install    product: 





$this->db->query("ALTER TABLE `".DB_PREFIX."product` ADD `marked` TINYINT UNSIGNED NOT NULL DEFAULT '0';");
      
      



ocStore 2.3.x , ocStore 3.0.2.0 MySQL 8, :





date_available:





$this->db->query("ALTER TABLE `".DB_PREFIX."product` CHANGE `date_available` `date_available` DATE NOT NULL;");
      
      



  ,       ( ocStore 2.3.x):





$this->load->model('extension/event');
 
// "   " -      (   )
$this->model_extension_event->addEvent(
  'productmarkedfield', //,     
  'admin/view/catalog/product_form/after', // 
  'extension/module/productmarkedfield/eventProductFormAfter' //
);
 
// "  " -    
$this->model_extension_event->addEvent(
  'productmarkedfield', 
  'admin/model/catalog/product/editProduct/after', 
  'extension/module/productmarkedfield/eventProductEditAfter'
);
      
      



ocStore 3.0.x :





$this->load->model('setting/event');
      
      



model_extension_event model_setting_event .





 admin/view/template/catalog/product_form.twig. 3 :





public function eventProductFormAfter(
&$route,
&$args, //    
&$output//html  
)
      
      



&$output, .





 Simple HTML DOM,   . system/library,  (@ , ):





@$this->load->library('simple_html_dom');
      
      



id . ( , id $args ):





preg_match("/product_id=(\d+)/", $args["action"], $aMatch);
$idProdict = $aMatch[1];
      
      



 ( product product_description):





$this->load->model('catalog/product');
$aProduct = $this->model_catalog_product->getProduct($idProdict);
      
      



, - .





, , , id . :





$isMarked = false;
if(preg_match("/product_id=(\d+)/", $args["action"], $aMatch))
{
    $idProduct = $aMatch[1];
    $this->load->model('catalog/product');
    $aProduct = $this->model_catalog_product->getProduct($idProduct);
    $isMarked = $aProduct["marked"];
}
      
      



isMarked, false id , isMarked .





Simple HTML DOM "" , gui  admin/view/template/catalog/product_form.twig ( ocStore 2.3.x tpl , Twig):





$html = str_get_html($output);
$html->find('div#tab-data', 0)->innertext = 
'<div class="form-group">
    <label class="col-sm-2 control-label"></label>
    <div class="col-sm-10">
        <label class="radio-inline">
            <input type="radio" name="marked" value="1" '.($aProduct["marked"] ? 'checked="checked"' : "").'>
        </label>
        <label class="radio-inline">
            <input type="radio" name="marked" value="0" '.(!$aProduct["marked"] ? 'checked="checked"' : "").'>
        </label>
    </div>
</div>' . $html->find('div#tab-data', 0)->innertext;
      
      



:





public function eventProductFormAfter(&$route, &$args, &$output)
{
    @$this->load->library('simple_html_dom');
    $isMarked = false;
    if(preg_match("/product_id=(\d+)/", $args["action"], $aMatch))
    {
        $idProduct = $aMatch[1];
        $this->load->model('catalog/product');
        $aProduct = $this->model_catalog_product->getProduct($idProduct);
        $isMarked = $aProduct["marked"];
    }
     
    $html = str_get_html($output);
    $html->find('div#tab-data', 0)->innertext = 
    '<div class="form-group">
        <label class="col-sm-2 control-label"></label>
        <div class="col-sm-10">
            <label class="radio-inline">
                <input type="radio" name="marked" value="1" '.($isMarked ? 'checked="checked"' : "").'>
            </label>
            <label class="radio-inline">
                <input type="radio" name="marked" value="0" '.(!$isMarked ? 'checked="checked"' : "").'>
            </label>
        </div>
    </div>' . $html->find('div#tab-data', 0)->innertext;
    $output = $html->outertext;
}
      
      



"",  ( )  , catalog/product  (ModelCatalogProduct::editProduct)  , .





" ":





public function eventProductEditAfter(&$route, &$args)
{
  // $args[0]  id 
  $sSql = "UPDATE " . DB_PREFIX . "product SET marked = " . $this->db->escape($args[1]['marked']) . " WHERE product_id = '" . (int)$args[0] . "'";
  $this->db->query($sSql);
}
      
      



marked product , . uninstall.





:





$this->db->query("ALTER TABLE `".DB_PREFIX."product` DROP `marked`");
      
      



( ocStore 2.3.x):





$this->load->model('extension/event');
$this->model_extension_event->deleteEvent('productmarkedfield');
      
      



(ocStore 3.0.x):





$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('productmarkedfield');
      
      



In general, it is not so difficult, but it seems a bit strange that the layout should be changed by hand, since there is no convenient built-in tool for changing the interface.





For those who have read to the end - a  link  to the archive with the module's source code.









Author: Vitaly Buturlin





A source








All Articles