adding field to products

Open to all! Ask other shopowners for help.
Post Reply
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

adding field to products

Post 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?


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: adding field to products

Post by Kofod95 »

This link might be useful for you: viewtopic.php?p=10533#p10533

I hope it helps!

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: adding field to products

Post by Portman »

That looks great, thanks heaps, I'll start from there!
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: adding field to products

Post 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;
You do not have the required permissions to view the files attached to this post.
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: adding field to products

Post 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
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply