Update product price using custom field info

  • Posts: 4
  • Thank you received: 0
11 years 3 weeks ago #168182

-- HikaShop version -- : 2.3.3
-- Joomla version -- : 3.3.0
-- PHP version -- : 5.4.4
-- Browser(s) name and version -- : chrome 36.0.1985.143

Hi,

I'm trying to get the displayed price on a product page to update using the custom field data. I'm configuring Hikashop for a print business, so have used variants for some options (when selected, these change the displayed product price), but have to also use custom fields (and the plugin) for some other options to stop the number of variants getting too great.

So, for example:
Business card, base price £1.60. Select a variant, and the price will be changed to £1.80. No prob.

Then, I use the custom field to select quantity (eg 10). On the product page the price stays at £1.80, although when I "add to cart", the cart price is fine (£18.00 in this case).

I want the customer to be able to see the impact of their choices when choosing, rather than having to go to the cart page to see the price.

This seems quite simple, but it's got me stuck, sorry if it's something obvious, have trawled the forum etc without joy! Could anyone give me a pointer please?

Other than that, impressed with Hikashop - you seem to have thought of almost everything, great.

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

  • Posts: 13201
  • Thank you received: 2322
11 years 3 weeks ago #168184

Hi,

Why not just use a cart module ?
This allow to show directly the total amount of the order on any website pages.

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

  • Posts: 4
  • Thank you received: 0
11 years 3 weeks ago #168344

Hi Xavier,

Thanks, appreciate the reply. I thought about using the cart module too (eventually!), but it doesn't do what I want. I'm trying to get the price to update on the product page, when the visitor selects an option (which it does) or a custom field option (which it doesn't). So, if they have (eg) a business card on standard paper, they select a heavier paper and can see the price change before they "add to basket".

I'm thinking the page must generate an "all in" price, as it's this that the cart picks up once they add to cart, so, it I can use that to display the updated price (I can't see what that is though). Maybe I can do it using the product/option_price view - I'll have a look.

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

  • Posts: 13201
  • Thank you received: 2322
11 years 3 weeks ago #168351

Hi,

So you can use the options, create characteristics, add them to a product and use this product as an option to the desired product (the main one).

Or you can use directly the characteristics, and when changing the characteristic value on the product page the price will be changed automatically.

Here is documentation:
hikashop.com/support/support/documentati...ct-form.html#options
hikashop.com/support/support/documentati...html#characteristics

Hope this is what you are looking for.

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

  • Posts: 4
  • Thank you received: 0
11 years 3 weeks ago #168834

Hi,

The Characteristic / Variants way is closest, as this handles all the product aspects, and updates the price when a variant is selected (and before it's added to the basket). The problem with just using variants is that I end up with too many variants, and use up all the php memory allocation (it's easy to end up with over 200 variants).

My approach is to use the variants for the product characteristics that aren't linear in pricing, and then use custom fields for elements that are (linear). Eg, for a product, pages and quantity will not be linear (as the unit price decreases in a non-regular manner as quantity goes up), so these I deal with in variants.

Then paper quality, special finishes are linear, for example adding (eg) 10% to any particular product / pages combination, so these can be dealt with using the custom field option and a custom price plugin. This way, I can keep the total number of product variants manageable (Nicholas commented elsewhere on the forum that this was the way to deal with too many variants, as it's not really a situation that the variants set up is meant to handle).

The option / characteristic route doesn't really help either, as the option-variant is either a straight additional price (eg +£10), whatever the quantity, or we could use the % of main product price route, but this is a % of the "option product" not the product that I've allocated the option too (using the Hikademo as an example, it's a % of RAM rather than a % of the computer price).

I'm so close, as Hikashop deals rally well with every other aspect that I need, it's just finding a way to get the price to update taking into account the custom price adjustment, in the same way it does for variants.

Thank you though, I appreciate it.

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

  • Posts: 26274
  • Thank you received: 4045
  • MODERATOR
10 years 11 months ago #172289

Hi,

Just to inform that if you want to have dynamic custom prices, you can use the HikaShop triggers.
www.hikashop.com/support/support/documen...mentation.html#other
Like the trigger : onBeforeCalculateProductPriceForQuantity(&$product)

HikaShop have some example system plugins for that : plg_system_custom_price or plg_system_custom_quantity_tax

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: 4
  • Thank you received: 0
10 years 11 months ago #172471

Thanks Jerome, this looks like the best way to deal with it - I'll have a careful look. Meanwhile I'm working around it using variants, which is fine but not ideal. Appreciated.

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

  • Posts: 105
  • Thank you received: 2
9 years 10 months ago #218797

Hello, I have a similar issue.
Have you been able to solve it using triggers?
when, in the product page, a user select an option of a custom field, is there a trigger generated dynamically ( by the hikashopToggleFields just function?) and which one?
I would appreciate some guidelines.

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

  • Posts: 84306
  • Thank you received: 13701
  • MODERATOR
9 years 10 months ago #218835

Hi,

There is no trigger for that yet. The simplest is to edit the view file displaying the custom fields and add a call to your custom js function.
It depends on the table of the custom field but for custom item fields on the product page, its the file show_block_custom_item where you can modify that line:
' '.$onWhat.'="hikashopToggleFields(this.value,\''.$fieldName.'\',\'item\',0);"'

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

  • Posts: 105
  • Thank you received: 2
9 years 10 months ago #219353

Hi Nicolas,
before even trying to update the price dynamicall; i have first to display the correct ones when loading the page.
I have created a table in database (i have made some changes in the admin/product to be able to input prices depending on the value of the custom fields) where I have price depending on a set of custom fields selected by the buyer.
I have tried to look inside several areas about the price/currency (including the class currency) and I need your "light".
Basically the question is where can I used my custom price table to calculate the actual price and "input it" into the right variable (which one) so that my new calculated price is displayed?

To give you some explanation on my custom prices: I have 3 custom fields :
a "Nb of evaluation" dropdown field : the customer can select among several values; each value triggers a different "base price"
a "detailed analysis" dropdown field: depending on the seleted value, the price increases the price given by the " Nb of evaluation" by X%
a "benchmark" dropdown field : depending on the choice here, there is an additional fix price (as an option).

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

  • Posts: 84306
  • Thank you received: 13701
  • MODERATOR
9 years 10 months ago #219372

Hi,

I'm not following you.
If you want to display a dynamic price based on what the customer enters in custom fields on the page, the page is already loaded when you want to do your first display of the dynamic price.
Before that, you can just let HikaShop display the default price of the product on the page.
And, then it's up to your javascript to erase that price and replace it with yours using the DOM of the browser.
So there is no variable to change anywhere to do that. It's just up to your javascript code to do that.

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

  • Posts: 105
  • Thank you received: 2
9 years 10 months ago #219667

Hi,

maybe I wanted to be to clever...or complicated.
For the dynamic part, I am fine.
my question was the "BEFORE" (where you suggest to use the default price) because the default price may no longer be the "default" when the custom fields items the price depend on are already "preselected" thanks to a variable passed through the url.
Of course, I could do a complete override of all the price functions but i would have liked to charge the right price before coming to the view.
So I would appreciate to know where I can modify the input price before the view is charged (because the loadTemplate() function doesn't tell me much). By knowing that, I assume it may also help me using the js for the dynamic change??

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

  • Posts: 84306
  • Thank you received: 13701
  • MODERATOR
9 years 10 months ago #219676

Hi,

I didn't know that you were passing data through the URL to initialize the custom fields. Hence, I didn't see the need for that.
In that case, you'll indeed want to change the default price displayed.
There are three ways to do it:
- You modify the view displaying the price (the file "listing_price" of the view "product" via the menu Display>Views) so that you take into account the data in the URL to recalculate the price
- You can also do it before that view in the file "show_default" of the view product by resetting the price in the $this->element->prices array based on the data coming from the URL.
- You could simply call your javascript function to refresh the price when the page is loading. That way, you avoid having your algorithm for the calculations at two different places. It's easier to write and easier to maintain. I would personally have gone with that method.

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

  • Posts: 105
  • Thank you received: 2
9 years 10 months ago #220391

Hi Nicolas,

thanks for your advices. I will try to follow your recommendation about using a js.
But I am sorry, i realise I have more questions
1/ when the page is loading..where is that? I don't understand fully the architecture of this page; i have just understood that severals part of the page are loaded (listing_price, show_block_custom_item,..) but where is it that I know when the full page is loaded?;
or said in another way where can I put js? can i just put in anywhere in one of these views?

2/ what "price" should I modify. I see that the "listing_price" loads the "$this->row->prices as $k => $price" and then $price->unit_price.
is it this one that I should modify? and where then (what file/function/element)?

3/ Let's assume i can do that. When then the user selects a different "custom field value", i assume that the "hikashopToggleFields" function of the "show_block_custom_item" view will review the prices. But then i believe it will "destroy" my own values; So should i replace the "hikashopToggleFields" call by a call to my js function?

Sorry if my questions look stupid, but I am not really a developer, though i believe i am becoming, with the amount of work I put here :unsure: ).
I just continue this post in englsh since we started in english, but I am not sure this is best (I believe it could be if someone is interested in it?)

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

  • Posts: 84306
  • Thank you received: 13701
  • MODERATOR
9 years 10 months ago #220400

Hi,

1. When you're writing javascript, "when the page is loading" means that you add your javascript code into some specific javascript which allows it to run after the page is loaded:
stackoverflow.com/questions/807878/javas...utes-after-page-load
You can write your javascript anywhere.

2. You can use the getElementById and innerHTML of javascript in order to modify the HTML of a specific element of the page. Just get it by its id and modify its HTML. So it has nothing to do with PHP. Again, that's something you want to do in javascript.

3. You can replace that call yes. Or you can add it before/after that.

4. Yes, it's better to post in English. That way, more people can use our conversations as references.

Note that these answers are more about how to code in javascript than HikaShop development. Our support is not there to teach how to write javascript. So it would be best to learn a bit more about how to code in javascript via tutorials you can find on google.

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

  • Posts: 105
  • Thank you received: 2
9 years 10 months ago #220648

Nicolas,

I can write in Js so I don't need so much info on it(though I am far from good,; this is why i asked about where I could put the test on the loaded page). Anyway thanks for your answer it guided me to realise that it was much simpler than i thought (I thought i needed to change price variables before they where displayed).

So I could write my js code successfully; I can change the price and display it correctly.

However my new price isn't taken into the cart.

I have noticed that the price isn't displayed with an element with an ID but "span" elements with ClassName 'hikashop_product_price".

I suspect then that the cart isn't using the price that is "displayed" but takes if from a variable? or is the price also in a "hidden" element in the page?

Can you please tell me what do i need to change, variable (but then isn't it weird to change a php/servier variable through js?), hidden element,... by my js code so that my new price is transfered to the cart?

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

  • Posts: 84306
  • Thank you received: 13701
  • MODERATOR
9 years 10 months ago #220706

Hi,

Yes, the javascript code is only to dynamically update the price on the product page.
If you want to have the price of the product recalculated in the cart/order based on the custom fields data, you need to write a small HikaShop plugin which will do the calculation, as explained in this message:
www.hikashop.com/forum/product-category-...eld-info.html#172289

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

  • Posts: 105
  • Thank you received: 2
9 years 8 months ago #224780

Just to say thank you for you support on this topic (and another one). I could do what I wanted thanks to you.

Merry Christmas and Happy new year.
Richard

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

Time to create page: 0.105 seconds
Powered by Kunena Forum