Hide/Enable custom fields depending from products

  • Posts: 184
  • Thank you received: 4
12 years 1 month ago #81604

Hello

In the checkout, do you think is possible to Hide/Enable some specific custom fields depending by the product categories present in the cart?

I have a custom field to select the "greeting cards".
The customer may select a greeting card only if they buy some "gift products".

I think I have to modify the file "custom_fields" of the "checkout" view. is correct or is there a smarter way?

Ad the moment I have already modified the checkout/custom_fields view in order to check the username, I think the customization should be something similar.

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.1.0
 * @author	hikashop.com
 * @copyright	(C) 2010-2012 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

  // Stefano Start
  // Legge i dati dell'utente 
  $user =& JFactory::getUser();
  $userGroups = JAccess::getGroupsByUser($user->id, true);
  // parte per conotrollo se almeno un prodotto fa parte della categoria regalo
  $regalo = 0;
  foreach($this->rows as $product){
	  $db = JFactory::getDBO();
	  $db->setQuery('SELECT category_id FROM #__hikashop_product_category WHERE product_id='.$product->product_id);
	  $categories = $db->loadObjectList();
	  foreach($categories as $category){
		if($category->category_id==39){
		   $regalo = 1;
		  }
	  }
  }
  $visualizza = 1;
  // se è già abbonato oppure se non vi sono articolo regalo non stampa la sezione dei deti supplementari
  if (in_array(11,$userGroups))  $visualizza = 0; 
  if ($regalo == 0)   $visualizza = 0; 
  
  if ($visualizza == 1) {
    // Stefano End
	
?><?php if(hikashop_level(2) && !empty($this->extraFields['order'])){ ?>
<div  id="hikashop_checkout_additional_info" class="hikashop_checkout_additional_info">
	<fieldset class="input">
		<legend><?php echo JText::_('ADDITIONAL_INFORMATION');?></legend>
		<table cellpadding="0" cellspacing="0" border="0" class="hikashop_contentpane">
	<?php
		if(!empty($this->extraFields['order'])){
			JRequest::setVar('hikashop_check_order',1);
			$this->setLayout('custom_fields');
			$this->type = 'order';
			echo $this->loadTemplate();
		}
	?>
		</table>
	</fieldset>
</div>
<div style="clear:both"></div>
<?php } 

  // Stefano Start   
  }
  // Stefano End ?>

Last edit: 11 years 10 months ago by Teto.

Please Log in or Create an account to join the conversation.

  • Posts: 26171
  • Thank you received: 4030
  • MODERATOR
12 years 1 month ago #81754

Hi,

It would be possible yes.
You have to make check every product in the cart and look at your products. Depending a product_id or maybe a custom field, you can set a local variable to "true".
After that, in the "extrafields foreach", you can test the "$oneExtraField->field_namekey" and do a "continue" if the local variable is "true".

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 184
  • Thank you received: 4
12 years 1 month ago #81757

Hello jarome and thanks for your help. :)

Do I have to insert the code into the view checkout/custom_fields?

Could you be so kind to give me an example?

Thank you so much!

Please Log in or Create an account to join the conversation.

  • Posts: 83051
  • Thank you received: 13412
  • MODERATOR
12 years 1 month ago #81982

Yes, in checkout/custom_fields.

It should be something like that:

$display_field = false;

foreach($this->rows as $product){
 if($product->product_id == 18) $display_field = true;
}

if($display_field && $fieldName=='my_field'){ 
 //display the field
}

Please Log in or Create an account to join the conversation.

  • Posts: 184
  • Thank you received: 4
11 years 11 months ago #89936

Hello nicolas

I am upgrading to version 2.1.0 so i am going to implement also this modification.

Thanks for you advise, could you suggest me the code to check the categories of each products.

In my case each product is inside to more than One category.

Thank you very much

Please Log in or Create an account to join the conversation.

  • Posts: 83051
  • Thank you received: 13412
  • MODERATOR
11 years 11 months ago #90104

Loading all the categoy_ids of a product can be done like that:

$db = JFactory::getDBO();
$db->setQuery('SELECT category_id FROM #__hikashop_category WHERE product_id='.$product->product_id);
$categories = $db->loadObjectList();
foreach($categories as $category){
 if($category->category_id==18){
   //do something when one of the category of the product match
 }
}

Please Log in or Create an account to join the conversation.

  • Posts: 184
  • Thank you received: 4
11 years 10 months ago #90651

Thank you Nicolas

i modified the checkout/custom_fields view as follow

<?php
/**
 * @package  HikaShop for Joomla!
 * @version  2.1.0
 * @author  hikashop.com
 * @copyright  (C) 2010-2012 HIKARI SOFTWARE. All rights reserved.
 * @license  GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
$showfields = array(
  'my_special_field1' => 0, // my_special_field1 display only on step 0
);
$type = $this->type;
foreach($this->extraFields[$type] as $fieldName => $oneExtraField) {

  // Stefano Start
  $user =& JFactory::getUser();
  $userGroups = JAccess::getGroupsByUser($user->id, true);
  // non entra e quindi non stampa se l'utente fa parte del gruppo "Cliente Abbonato" e se il campo custom da scrivere si chiama 'order_portaunamico'
  // ci vorrebbe una miglioria per evitare di stampare la sezione "informazioni supplementari" qualora ci fosse solo il campo 'order_portaunamico'
  // nel mio caso avendone di sicuro 2 non mi metto a perdere tempo su questa miglioria
  
  // parte per conotrollo se almeno un prodotto fa parte della categoria regalo
  $regalo = 0;
  foreach($this->rows as $product){
	  $db = JFactory::getDBO();
	  $db->setQuery('SELECT category_id FROM #__hikashop_category WHERE product_id='.$product->product_id);
	  $categories = $db->loadObjectList();
	  foreach($categories as $category){
		if($category->category_id==39){
		   $regalo = 1;
		  }
	  }
  }
  
  $visualizza = 1;
  if (  (in_array(11,$userGroups) and ($oneExtraField->field_namekey=='order_portaunamico'))  )     $visualizza = 0;
  if (  ($regalo == 0) and ($oneExtraField->field_namekey=='order_bigliettoauguri')  )     $visualizza = 0;
  if (  ($regalo == 0) and ($oneExtraField->field_namekey=='order_comespedirebiglietto')  )     $visualizza = 0;
  if (  ($regalo == 0) and ($oneExtraField->field_namekey=='order_notespedizionebiglietto')  )     $visualizza = 0;
  if (  ($regalo == 0) and ($oneExtraField->field_namekey=='order_emailamico')  )     $visualizza = 0;
  if (  ($regalo == 0) and ($oneExtraField->field_namekey=='order_frasebiglietto')  )     $visualizza = 0;
  
  if ($visualizza == 1) {
  // Stefano End
  
	  if(isset($showfields[$fieldName]) && $this->step != $showfields[$fieldName]) {
		echo '<tr style="display:none;"><td><input type="hidden" name="data['.$type.']['.$fieldName.']" value="'.$this->escape($this->$type->$fieldName).'"/></td></tr>';
		continue;
	  }
	?>
	  <tr class="hikashop_checkout_<?php echo $fieldName;?>_line" id="hikashop_<?php echo $type.'_'.$oneExtraField->field_namekey; ?>">
		<td class="key">
		  <?php echo $this->fieldsClass->getFieldName($oneExtraField);?>
		</td>
		<td>
	<?php
	  $onWhat='onchange';
	  if($oneExtraField->field_type=='radio')
		$onWhat='onclick';

	  echo $this->fieldsClass->display($oneExtraField,$this->$type->$fieldName,'data['.$type.']['.$fieldName.']',false,' '.$onWhat.'="hikashopToggleFields(this.value,\''.$fieldName.'\',\''.$type.'\',0);"');
	?>
		</td>
	  </tr>
	<?php
  // Stefano Start   
  }
  // Stefano End
}

but unfortunatelty is doesn't work.

is it my foult?

I receive these messages if I enable the Joomla Log:

Warning: Invalid argument supplied for foreach() in /home/madeinv1/public_html/labs/templates/miv_icons_14/html/com_hikashop/checkout/custom_fields.php on line 30

Line 30 is: foreach($categories as $category){

Last edit: 11 years 10 months ago by Teto.

Please Log in or Create an account to join the conversation.

  • Posts: 26171
  • Thank you received: 4030
  • MODERATOR
11 years 10 months ago #90772

Hi,

I guess the SQL query is on the "product_category" table, so:

SELECT category_id FROM #__hikashop_product_category WHERE product_id='.$product->product_id

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.
The following user(s) said Thank You: Teto

Please Log in or Create an account to join the conversation.

  • Posts: 184
  • Thank you received: 4
11 years 10 months ago #90834

Thank you Jerome, it works fine! :laugh:

Last edit: 11 years 10 months ago by Teto.

Please Log in or Create an account to join the conversation.

Time to create page: 0.059 seconds
Powered by Kunena Forum