Page 1 of 1
adding field to products
Posted: Sat Oct 07, 2023 5:47 am
by Portman
Hi again,
I am trying to do another modification or two and was hoping to get a bit of guidance on how to do it...
I am hoping to add a field to products that will essentially allow me to count stock for some products and not for others....
I have set up an extra collum in the products field 'products_count_stock' which will be a 1/0 switch
I am stuck however in how to add the "switch' into the products form in admin... from what I can gather you do not use the override folder for admin... do I just edit the core code? or is there another way to do this?
Re: adding field to products
Posted: Sat Oct 07, 2023 7:23 am
by Kofod95
This link might be useful for you:
viewtopic.php?p=10533#p10533
I hope it helps!
//Daniel
Re: adding field to products
Posted: Sun Oct 08, 2023 9:37 pm
by Portman
That looks great, thanks heaps, I'll start from there!
Re: adding field to products
Posted: Sun Oct 22, 2023 6:53 am
by Portman
@Kofod95 thanks for your help with this, it has allowed me to make a hook that actually works!!!
based on your example,this is what I have done;
file includes/hooks/admin/catalog/stockCount.php
Code: Select all
<?php
class hook_admin_catalog_stockCount {
function listen_updateProductAction() {
$this->listen_productActionSave();
}
function listen_insertProductAction() {
$this->listen_productActionSave();
}
function listen_injectDataForm() {
$products_id = Text::input($_GET['pID']);
$grab_val = $GLOBALS['db']->query("SELECT products_count_stock FROM products WHERE products_id = " . $products_id)->fetch_assoc();
$product_count_stock = $grab_val['products_count_stock'];
$out_status = '0' == $product_count_stock;
$in_status = !$out_status;
?>
<div class="form-group row" id="zCountStock">
<label for="pCountStock" class="col-form-label col-sm-3 text-left text-sm-right"><?= TEXT_COUNT_STOCK_TITLE ?></label>
<div class="col-sm-9">
<div class="col-sm-9">
<div class="custom-control custom-radio custom-control-inline">
<?= (new Tickable('product_count_stock', ['value' => '1', 'id' => 'inStatus', 'class' => 'custom-control-input'], 'radio'))->tick($in_status) ?>
<label class="custom-control-label" for="inStatus"><?= TEXT_COUNT_STOCK_YES ?></label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<?= (new Tickable('product_count_stock', ['value' => '0', 'id' => 'outStatus', 'class' => 'custom-control-input'], 'radio'))->tick($out_status) ?>
<label class="custom-control-label" for="outStatus"><?= TEXT_COUNT_STOCK_NO ?></label>
</div>
</div>
</div>
</div>
<?php
}
function listen_productActionSave() {
global $products_id, $db;
$sql_data_array = ['products_count_stock' => (int)$_POST['product_count_stock'] ?? 0];
$db->perform('products', $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");
}
}
I have it all working!!!
One quick question I have though is; is there a way to change where it apprears on the form? obviously for asthetics and flow it would make more sense if I could move it like this;
Re: adding field to products
Posted: Sun Oct 22, 2023 7:55 pm
by Kofod95
Good to see you got it working!
I think the only way to move it is with javascript. I haven't done it, so I have no examples to give, but I think you would change the injectDataForm listerner to injectSiteEnd or similar, and put all the output in a variable, which you then display on the site with js.
//Daniel