[Skip to main content](#vessel-content)

#   Restrict custom field to product variant

##

- **[arnask](/forum/user/6752-arnask "View arnask's Profile")**
- [![arnask's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/6752-arnask "View arnask's Profile") Topic Author
- [ Offline](/forum/user/6752-arnask)

Less More

- Posts: 198
- Thank you received: 4
-
-

1 year 7 months ago [\#366496](#366496) by [arnask](/forum/user/6752-arnask "View arnask's Profile")  *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366496 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")* was created by *[arnask](/forum/user/6752-arnask "View arnask's Profile")*

Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86584
- Thank you received: 14325
-
-

1 year 7 months ago [\#366498](#366498) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366498 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

Hi,

If you have a custom field of the table "product" and you configure different values for it in different variants of the product in the backend, the system will automatically refresh the displayed value of that custom field on the frontend when you change the variant.

If you're talking about a custom field of the table "item", which you want to be available only for some variants of the product, there is no option to do that. Now, it's not impossible, but it requires to add some custom code. Here is a subject where someone else did it :
[ www.hikashop.com/forum/product-category-...characteristics.html](https://www.hikashop.com/forum/product-category-display/901807-dependent-characteristics.html)

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[arnask](/forum/user/6752-arnask "View arnask's Profile")**
- [![arnask's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/6752-arnask "View arnask's Profile") Topic Author
- [ Offline](/forum/user/6752-arnask)

Less More

- Posts: 198
- Thank you received: 4
-
-

1 year 7 months ago [\#366512](#366512) by [arnask](/forum/user/6752-arnask "View arnask's Profile")  Replied by *[arnask](/forum/user/6752-arnask "View arnask's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366512 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

My custom field is in table "order"

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86584
- Thank you received: 14325
-
-

1 year 7 months ago [\#366513](#366513) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366513 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

Hi,

For a custom field of the table "order", you'll want to customize the file checkout / show_block_fields via the menu Display&gt;Views.
There, you'll want to take into account the product objects in $cart-&gt;products to skip or not the display of the fields.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[arnask](/forum/user/6752-arnask "View arnask's Profile")**
- [![arnask's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/6752-arnask "View arnask's Profile") Topic Author
- [ Offline](/forum/user/6752-arnask)

Less More

- Posts: 198
- Thank you received: 4
-
-

1 year 7 months ago [\#366590](#366590) by [arnask](/forum/user/6752-arnask "View arnask's Profile")  Replied by *[arnask](/forum/user/6752-arnask "View arnask's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366590 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

Can you give some example, course for me its too complicated

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86584
- Thank you received: 14325
-
-

1 year 7 months ago [\#366592](#366592) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366592 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

Well, something like that at the beginning (not tested): Code:

&lt;?php // check if the products with the id XX or YY or ZZ are in the cart $found = false; foreach($cart-&gt;products as $product) { if(in_array($product-&gt;product_id, array(XX, YY, ZZ))) { $found = true; } } // if none of them are in the cart, remove the field AA from the array of fields to be displayed if(!$found) { foreach($cart-&gt;order_fields as $k =&gt; $field) { if($field-&gt;field_namekey == 'AA') { unset($cart-&gt;order_fields[$k]); break; } } } ?&gt;

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[arnask](/forum/user/6752-arnask "View arnask's Profile")**
- [![arnask's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/6752-arnask "View arnask's Profile") Topic Author
- [ Offline](/forum/user/6752-arnask)

Less More

- Posts: 198
- Thank you received: 4
-
-

1 year 7 months ago -  1 year 7 months ago [\#366595](#366595) by [arnask](/forum/user/6752-arnask "View arnask's Profile")  Replied by *[arnask](/forum/user/6752-arnask "View arnask's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366595 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

I have managed to get this code (show_block_fields.php) with help of chatgpt: Code:

&lt;?php /** * @package HikaShop for Joomla! * @version 5.1.5 * @author hikashop.com * @copyright (C) 2010-2025 HIKARI SOFTWARE. All rights reserved. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html */ defined('_JEXEC') or die('Restricted access'); $cart = $this-&gt;checkoutHelper-&gt;getCart(); if(!hikashop_level(2)) { $this-&gt;emptyBlocksCount++; return; } // Check wheather there is products 78 or 79 in cart $target_ids = array(78, 79); $found = false; if (!empty($cart-&gt;products) &amp;&amp; is_array($cart-&gt;products)) { foreach ($cart-&gt;products as $product) { if (in_array((int)$product-&gt;product_id, $target_ids)) { $found = true; break; } } } $fields = null; if (!empty($cart-&gt;order_fields)) { $fields = hikashop_copy($cart-&gt;order_fields); } // If there are none of products 78 or 79 in cart we hide field "sleepwith" if (!$found &amp;&amp; !empty($fields)) { foreach ($fields as $k =&gt; $field) { if (isset($field-&gt;field_namekey) &amp;&amp; $field-&gt;field_namekey === 'sleepwith') { unset($fields[$k]); // Pašalina tik "sleepwith" lauką break; } } } // If there is 78 or 79 in cart, make "sleepwith" field reguired if ($found &amp;&amp; !empty($fields)) { foreach ($fields as $k =&gt; $field) { if (isset($field-&gt;field_namekey) &amp;&amp; $field-&gt;field_namekey === 'sleepwith') { $field-&gt;field_required = 1; // Padarome lauką privalomu break; } } } $labelcolumnclass = 'hkc-sm-4'; $inputcolumnclass = 'hkc-sm-8'; if(empty($this-&gt;ajax)) { ?&gt; &lt;div id="hikashop_checkout_fields_&lt;?php echo $this-&gt;step; ?&gt;_&lt;?php echo $this-&gt;module_position; ?&gt;" data-checkout-step="&lt;?php echo $this-&gt;step; ?&gt;" data-checkout-pos="&lt;?php echo $this-&gt;module_position; ?&gt;" class="hikashop_checkout_fields"&gt; &lt;?php } ?&gt; &lt;div class="hikashop_checkout_loading_elem"&gt;&lt;/div&gt; &lt;div class="hikashop_checkout_loading_spinner"&gt;&lt;/div&gt; &lt;?php $this-&gt;checkoutHelper-&gt;displayMessages('fields'); ?&gt; &lt;fieldset class="hkform-horizontal"&gt; &lt;?php if(!empty($fields)) { if(!empty($this-&gt;options['show_title'])) { ?&gt; &lt;legend&gt;&lt;?php echo JText::_('ADDITIONAL_INFORMATION'); ?&gt;&lt;/legend&gt; &lt;?php } $after = array(); foreach($fields as $fieldName =&gt; $oneExtraField) { $oneExtraField-&gt;registration_page = @$this-&gt;registration_page; if(isset($_SESSION['hikashop_order_data']) &amp;&amp; is_object($_SESSION['hikashop_order_data']) &amp;&amp; isset($_SESSION['hikashop_order_data']-&gt;$fieldName) &amp;&amp; !is_null($_SESSION['hikashop_order_data']-&gt;$fieldName)) { $element = $_SESSION['hikashop_order_data']; } else { $element = $cart-&gt;cart_fields; } $value = @$element-&gt;$fieldName; if(empty($value) &amp;&amp; !empty($this-&gt;options['read_only'])) continue; if(empty($this-&gt;options['read_only'])) { $onWhat = ($oneExtraField-&gt;field_type == 'radio') ? 'onclick' : 'onchange'; $html = $this-&gt;fieldClass-&gt;display( $oneExtraField, $value, 'data[order_' . $this-&gt;step . '_' . $this-&gt;module_position.']['.$fieldName.']', false, ' class="'.HK_FORM_CONTROL_CLASS.'" '.$onWhat.'="window.hikashop.toggleField(this,\''.$fieldName.'\',\'order_' . $this-&gt;step . '_' . $this-&gt;module_position.'\',0,\'hikashop_\');"', false, $fields, $cart-&gt;cart_fields, false ); if($oneExtraField-&gt;field_type == 'hidden') { $after[] = $html; continue; } }else{ $oneExtraField-&gt;currentElement = $element; $html = $this-&gt;fieldClass-&gt;show($oneExtraField, $value); } ?&gt; &lt;div class="hkform-group control-group hikashop_checkout_&lt;?php echo $fieldName;?&gt;_line" id="hikashop_order_&lt;?php echo $this-&gt;step . '_' . $this-&gt;module_position . '_' . $oneExtraField-&gt;field_namekey; ?&gt;"&gt; &lt;?php $requiredDisplay = true; if(!empty($this-&gt;options['read_only'])) { $requiredDisplay = false; } $label = $this-&gt;fieldClass-&gt;getFieldName($oneExtraField, $requiredDisplay, $labelcolumnclass.' hkcontrol-label'); if(!empty($this-&gt;options['read_only'])) { $label = str_replace('&lt;/label&gt;',' :&lt;/label&gt;',$label); } echo $label; ?&gt; &lt;div class="&lt;?php echo $inputcolumnclass;?&gt;"&gt; &lt;?php echo $html; ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php } if(count($after)) { echo implode("\r\n", $after); } if(!empty($this-&gt;options['show_submit'])) { ?&gt; &lt;div class="hkform-group control-group hikashop_fields_button_line"&gt; &lt;div class="&lt;?php echo $labelcolumnclass;?&gt; hkcontrol-label"&gt;&lt;/div&gt; &lt;div class=" &lt;?php echo $inputcolumnclass;?&gt;"&gt; &lt;button type="submit" onclick="return window.checkout.submitFields(&lt;?php echo $this-&gt;step.','.$this-&gt;module_position; ?&gt;);" class="&lt;?php echo $this-&gt;config-&gt;get('css_button','hikabtn'); ?&gt; hikabtn_checkout_fields_submit"&gt; &lt;?php echo JText::_('HIKA_SUBMIT_FIELDS'); ?&gt; &lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php } } else { $this-&gt;emptyBlocksCount++; } ?&gt; &lt;/fieldset&gt; &lt;?php if(!empty($this-&gt;options['js'])) { ?&gt; &lt;script type="text/javascript"&gt; &lt;?php echo $this-&gt;options['js']; ?&gt; &lt;/script&gt; &lt;?php } if(empty($this-&gt;ajax)) { ?&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; if(!window.checkout) window.checkout = {}; window.Oby.registerAjax(['checkout.fields.updated','cart.updated','checkout.shipping.changed','checkout.payment.changed'], function(params){ window.checkout.refreshFields(&lt;?php echo (int)$this-&gt;step; ?&gt;, &lt;?php echo (int)$this-&gt;module_position; ?&gt;); }); window.checkout.refreshFields = function(step, id) { return window.checkout.refreshBlock('fields', step, id); }; // make sure field is filled window.checkout.submitFields = function(step, id) { // Check weather "sleepwith" is fiiled if products 78 or 79 is in cart var sleepwithField = document.querySelector('[name="data[order_&lt;?php echo $this-&gt;step; ?&gt;_&lt;?php echo $this-&gt;module_position; ?&gt;][sleepwith]"]'); if (sleepwithField &amp;&amp; sleepwithField.value == "" &amp;&amp; &lt;?php echo $found ? 'true' : 'false'; ?&gt;) { alert("Please fill this field"); return false; // Stops if its empty } return window.checkout.submitBlock('fields', step, id); }; &lt;/script&gt; &lt;?php } ?&gt;

Basically it checks if there are products with special IDs in cart and if there it is, then make custom field visible and required. All working good, except of field requirement. It shows in red that its required, but i'm still able to proceed without filling anything in. Even chatgpt could not help me with that. Maybe you can?

Last edit: 1 year 7 months ago by [arnask](/forum/user/6752-arnask "View arnask's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86584
- Thank you received: 14325
-
-

1 year 7 months ago [\#366597](#366597) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Restrict custom field to product variant](/forum/26-product-category-display/909312-restrict-custom-field-to-product-variant#366597 "Hello, i need to show custom field only on special product variant, not just for whole product, but for one or couple of variants. How to do that?")*

Hi,

The check on the required attribute is done on the server side, when your browser sends the form data to the server.
Here, by adding the required attribute, you tell the client side (the browser) that the field is required (that's why it is red) but the server side still bases itself on the required attribute in the database.

Adding a check on the server side is not impossible, but it's a bit harder than just a view override.
You'll need to develop a small HikaShop plugin and implement the onBeforeCartSave(&amp;$element,&amp;$do) event ( [ www.hikashop.com/support/documentation/6...tml#onBeforeCartSave](https://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#onBeforeCartSave) ).
There, check the data in $cart-&gt;cart_fields basing yourself on $cart-&gt;products for the products, and if something is not ok, you can set $do to false and use $app-&gt;enqueueMessage to add an error message.
So, it's still possible, but it's a bit more complex.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI2LXByb2R1Y3QtY2F0ZWdvcnktZGlzcGxheS85MDkzMTItcmVzdHJpY3QtY3VzdG9tLWZpZWxkLXRvLXByb2R1Y3QtdmFyaWFudD90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.065 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
