custom field value for variants

  • Posts: 232
  • Thank you received: 13
22 hours 26 minutes ago #372580

-- HikaShop version -- : 6.5.1
-- Joomla version -- : 6.1.2
-- PHP version -- : 8.3

hi
i have several products and variants.
I also have a custom field called seller percent
celler percent field is filled for all main products, but not for variants. i want to set the value of this field for variants, equal to its parent main product

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

  • Posts: 85860
  • Thank you received: 14112
  • MODERATOR
9 hours 10 minutes ago #372582

Hi,

HikaShop already automatically use dynamically the value of the main product for the variant display on the product page on the frontend if the value of the custom field is left empty in the variant.
Is that what you're talking about ?
Or are you talking about something else ?
Could you explain your use case ? What are you trying to do with that custom product field ?

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

  • Posts: 232
  • Thank you received: 13
8 hours 55 minutes ago #372584

Thanks for the response.
In my case, i need the custom field value be filled in database for both main products and variants. Because we have developed a custom plugin which grabs the products directly from the database.
We are selling gold products. Our custom plugin has a unique formula to calculate the final product price base on global gold price and product weight.
So we grab the products, do some calculations and then update the prices.
In grabbing the products we get main products and variants separately

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

  • Posts: 85860
  • Thank you received: 14112
  • MODERATOR
5 hours 5 minutes ago #372587

Hi,

The inheritance I mentioned is only a display-time fallback: when the variant's custom field is empty, HikaShop reads the main product's value on the fly to show it on the frontend. It never writes that value to the variant's own row, so if you read the database directly you'll still find the field empty on the variants.

Product custom fields are stored as columns of the hikashop_product table, and variants are rows of that same table (with product_parent_id pointing to the main product). So the column already exists on your variant rows, it's just not filled.

Two solutions:

1) A one-time (or scheduled) SQL query copying the value from the parent to the empty variants. Replace seller_percent with the real column name of your field (its "Column name" in the field configuration, not the label):

UPDATE `#__hikashop_product` AS v
JOIN `#__hikashop_product` AS p ON p.product_id = v.product_parent_id
SET v.seller_percent = p.seller_percent
WHERE v.product_type = 'variant'
  AND (v.seller_percent IS NULL OR v.seller_percent = '');

2) Since your plugin already grabs the products from the database, the cleaner option is to do the fallback there: when a variant's field is empty, read the value from its product_parent_id row. This mirrors what HikaShop does and stays correct even when the main product's value changes later, whereas the SQL copy above is a snapshot that goes stale if you edit the main product afterwards.

I would recommend you go for option 2. That's the clean, long-term solution.

The following user(s) said Thank You: khashiz

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

Time to create page: 0.043 seconds
Powered by Kunena Forum