Load price from mysql on product page, based on custom fields

  • Posts: 208
  • Thank you received: 3
  • Hikashop Business
3 weeks 10 hours ago #309410

-- HikaShop version -- : 4.2.1

Hello!

Each my product has about 5.000 - 20.000 variants of price. They are calculated based on a few customs fields of item.
I need to create a custom plugin, that will load the price from own MySQL table based on a custom fields and if-else conditions.

Actually, I already hired three freelancers on that, one stole our money and two of them give up a project. Despite I am not a programmer, I can do many things with PHP, javascript,...

I would love to ask if you could kindly give us advise on that. I believe I can do this, only what I need to know:

1) How to start a plugin. What function should I use to handle changes of custom fields on product page? Is there any, or can I use jQuery? I haven't found anything in API :(

2) How to overwrite the price of the product on product page and affect the price in cart, checkout, order in backend, confirmation email, everything..

Here is my product page:
elodyprsteny.cz/snubni/zlute-zlato/model-6441

Thank you very much for any help! I highly appreciate everything!

F.

Last edit: 3 weeks 10 hours ago by FilipHradil.

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

  • Posts: 66619
  • Thank you received: 9806
  • MODERATOR
2 weeks 6 days ago #309426

Hi,

First, to override the price of the product in cart, checkout, order, etc, you can do it like it's done in the plugins we provide on our marketplace to change the product price based on custom item fields:
www.hikashop.com/marketplace/product/151...h-length-plugin.html
www.hikashop.com/marketplace/product/221...quantity-plugin.html
www.hikashop.com/marketplace/product/138...ce-letter-count.html
www.hikashop.com/marketplace/product/146...rice-word-count.html
With these examples, you should have no problem doing it if you know PHP.

Now, regarding the change of the price dynamically on the product page, you would have to add your own javascript to the page. For example, by editing the file "show" of the view "product" and added script tags at the top or bottom of the code via the menu Display>Views.
You would add listeners on the inputs, selects ( www.w3schools.com/jsref/met_element_addeventlistener.asp ), do your calculations and then modify the HTML of the area where the price is displayed. If you knwo javascript, it's quite basic programing.

The following user(s) said Thank You: FilipHradil

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

  • Posts: 208
  • Thank you received: 3
  • Hikashop Business
2 weeks 5 days ago #309446

Hello Nicolas!

Thank you so so much for tips. I spend day and night and I managed it to work. I only have 2 issues, which are not connected with this modification (I tested).

1. If I try to apply a coupon on discounted product (with setting to override discount) it calculates the wrong amount.
2. When I empty cart, I have not redirected anywhere

I recorded both issues there:
www.loom.com/share/213b27f04df04346b7f43e00cc6c273e

Just for sure, I will send you admin access via form (in case there will need to check something).


Thank you in advance for any tip or piece of advice!


Filip

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

  • Posts: 66619
  • Thank you received: 9806
  • MODERATOR
2 weeks 4 days ago #309484

Hi,

1. I think this totally is linked to the plugin you developed to change the price of the product in the cart / checkout / order.
In your plugin, you're called after the prices have been loaded and discounts have been applied to the prices of the products in the cart.
So if you have a discount on the product, the base price of the product will change and thus the calculation of your plugin will produce a different result than if you don't have a discount on the product.
And then the coupon is applied on the result of your calculations (and this doesn't change).

2. You've added some javascript error which prevents the redirection system (done in javascript) from working:
monosnap.com/file/splRgyCw6hRqzXOz60lGSeUXsYMMm4

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

  • Posts: 208
  • Thank you received: 3
  • Hikashop Business
2 weeks 4 days ago #309486

Hello Nicols,

You are right about the second issue, I removed javascript that and cart is redirecting :-). Thank you.


About second issue, I beg to differ. I believe that it came from Hikashop mistake.

Price after coupon with discount override was calculated like:

$coupon->discount_percent_amount * $product->prices[0]->$price
In $price[0] is already discounted value

instead of
$coupon->discount_percent_amount * $product->prices[0]->$price_without_discount

I fixed it in file back/classes/discount.php in this section:

ORIGINAL - WRONG
case 2:
if(isset($product->prices[0]->$price_without_discount)) {
	$coupon->discount_flat_amount += ($coupon->discount_percent_amount * $product->prices[0]->$price) / 100;
	$coupon->discount_flat_amount -= $product->prices[0]->$price_without_discount - $product->prices[0]->$price;
	if($coupon->discount_flat_amount < 0)
		$coupon->discount_flat_amount = 0;
		break;
	}


FIXED
case 2:
if(isset($product->prices[0]->$price_without_discount)) {
// MISTAKE FIXED BELLOW
	$coupon->discount_flat_amount += ($coupon->discount_percent_amount * $product->prices[0]->$price_without_discount) / 100;
	$coupon->discount_flat_amount -= $product->prices[0]->$price_without_discount - $product->prices[0]->$price;
	if($coupon->discount_flat_amount < 0)
		$coupon->discount_flat_amount = 0;
	break;
}


Is that right?

Maybe there will be more needed fixes to this issue, if you create a patch for that, please, let me know.

Thank you

F.

Last edit: 2 weeks 4 days ago by FilipHradil.

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

  • Posts: 66619
  • Thank you received: 9806
  • MODERATOR
2 weeks 3 days ago #309487

Hi,

There is no patch to make. It is normal that the coupon value is calculated based on the discounted value of the price.
If you don't want that, then you need to change the setting "coupon handling of discounted products" of your coupon.

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

  • Posts: 208
  • Thank you received: 3
  • Hikashop Business
2 weeks 3 days ago #309501

Hello,

Maybe I am wrong, but...

If I set coupon handling of discount to "override discounted products" I expect to ignore discount and use coupon only.

For example:
Original price 1000
Discounted price 900
Coupon (20%)
Price after coupon with discount override: 800

Is this how it should work? If yes, then override discounted products not working, at least on my side.


F.

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

  • Posts: 23612
  • Thank you received: 3666
  • MODERATOR
2 weeks 2 days ago #309535

Hello,

I'm sorry but I'm not able to understand your message nor the issue you're pointing.
As Nicolas said, the code you want us to modify is working as it should ; changing it will introduce a regression and a bug for users.

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: 208
  • Thank you received: 3
  • Hikashop Business
2 weeks 2 days ago #309543

Hello, I am sorry for confusion. The main is that it is working for us :-)

Thank you,
F.

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

Time to create page: 0.071 seconds
Powered by Kunena Forum