Jump to content

Custom Product Field not changeable


Qvixx

Recommended Posts

Hi Everybody,

 

I've got a little problem over here which seems a little strange to me.

 

PS-Version: 1.6.0.6

 

I created a new product-field called 'ist_muster'. It is a boolean field which should be changeable in the backoffice by a checkbox.

 

STEP 1: I created a Database-Field in the two tables ps_product and ps_product_shop. They are tiny-int fields with a length of 1.

 

STEP 2: I created an override for the Product-Class: (I did NOT forget to delete the cache!)

<?php
Class Product extends ProductCore
{
    public $ist_muster;
 
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
        self::$definition['fields']['ist_muster'] = array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool');
        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}
?>

STEP 3: I changed the Informations.tpl like this:

<label class="control-label col-lg-3" for="available_for_order">
    {if isset($display_multishop_checkboxes) && $display_multishop_checkboxes}
        {include file="controllers/products/multishop/checkbox.tpl" only_checkbox="true" field="available_for_order" type="default"}
        {include file="controllers/products/multishop/checkbox.tpl" only_checkbox="true" field="show_price" type="show_price"}
        {include file="controllers/products/multishop/checkbox.tpl" only_checkbox="true" field="online_only" type="default"}
        {include file="controllers/products/multishop/checkbox.tpl" only_checkbox="true" field="ist_muster" type="default"}
    {/if}
    {l s='Options'}
</label>
<div class="col-lg-5">
    <p class="checkbox">
        <input type="checkbox" name="available_for_order" id="available_for_order" value="1" {if $product->available_for_order}checked="checked"{/if}  />
        <label for="available_for_order">{l s='Available for order'}</label>
    </p>
    <p class="checkbox">    
        <input type="checkbox" name="show_price" id="show_price" value="1" {if $product->show_price}checked="checked"{/if} {if $product->available_for_order}disabled="disabled"{/if}/>
        <label for="show_price">{l s='Show price'}</label>
    </p>
    <p class="checkbox">
        <input type="checkbox" name="online_only" id="online_only" value="1" {if $product->online_only}checked="checked"{/if} />
        <label for="online_only">{l s='Online only (not sold in your retail store)'}</label>
    </p>
    <p class="checkbox">
        <input type="checkbox" name="ist_muster" id="ist_muster" value="1" {if $product->ist_muster}checked="checked"{/if} />
        <label for="ist_muster">{l s='Muster'}</label>
    </p>
</div>

As you can see i just copied things from online_only. I want my field to behave like 'online_only'.

 

I can check the checkbox an save the article. Everything works fine.

My problem occurs when I uncheck a checkbox an save the product. The Value is not saved it just remains 1 (checked).

 

I have to set the value to 0 manually in the two tables which is very annoying.

 

I just can't figure out where the problem is!

 

Greets

Link to comment
Share on other sites

Hi,

 

You have to do one more thing for it to work since the field You want to add is BOOL. For boolean field value to be properly saved, it has to be parsed to int. You have to extend copyFromPost function from AdminProductsController:

class AdminProductsController extends AdminProductsControllerCore
{
 
	protected function copyFromPost(&$object, $table)
	{
		parent::copyFromPost($object, $table);
		if (get_class($object) != 'Product')
			return;
 
		$object->custom_bool_field = (int)Tools::getValue('custom_bool_field');
	}	
}
  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...
×
×
  • Create New...