Ein Artikel unseres Entwicklers aus seinem persönlichen Blog.
Bei der Entwicklung eines Moduls für OpenCart musste auf der Produktkarte ein beliebiges Feld erstellt werden, das nur im Admin- Bereich sichtbar sein sollte . Dies sollte ein Feld mit dem booleschen Wert "markiert oder nicht" sein. Also am Ende:
Nach einem kurzen Graben in den Admin - Panel, fand ich heraus , dass aus der Box OpenCart nicht unterstützen benutzerdefinierte Felder, aber es gibt Module bezahlt, zum Beispiel hier . Sie müssen also selbst ein benutzerdefiniertes Feld in OpenCart erstellen.
Optionen zur Lösung des Problems :
Verwenden Sie nicht verwendete Felder (sku, upc, ean, jan, isbn, mpn) - fast sofort , aber vielleicht verwendet einer unserer Kunden das von uns belegte Feld.
Eingriffe in den Motorcode sind schnell und wenig Code , aber dann ist die Lösung nicht portierbar, und wenn die Engine aktualisiert wird, muss die Lösung selbst aktualisiert werden, da die Änderungen verloren gehen, wenn die Engine aktualisiert wird.
- , , 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');
Im Allgemeinen ist es nicht so schwierig, aber es scheint ein wenig seltsam, dass das Layout von Hand geändert werden muss, da es kein praktisches integriertes Tool zum Ändern der Benutzeroberfläche gibt.
Für diejenigen, die bis zum Ende gelesen haben - ein Link zum Archiv mit dem Quellcode des Moduls.
Verfasser: Vitaly Buturlin