How to combine product variants with custom field

  • Posts: 58
  • Thank you received: 2
6 years 6 months ago #279415

-- HikaShop version -- : 3.2.0
-- Joomla version -- : 3.8
-- PHP version -- : 7

Hi,

I made a custom field (text area) to be shown beside the product.
The customer is now able to fill in his personal text for a greeting-card message.
This greeting card costs an additional amount of 5 Euro.

Problem:
The text area is always there and customers can always fill in a text - even if the choose "NO, thank you" to the greeting card.

Is it possible to make the the custom field depending to the choice of the user ( greeting card yes or no)?
Maybe just visible when the choice is YES? Or just editable when chosen YES?


Screenshots:
one product with variants plus custom field
one product plus second product (greeting card) options

Attachments:

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
6 years 6 months ago #279441

Hi,

There is no option for that.
That would require adding some custom javascript on the product page in order to hide/display the field based on the characteristic selection.

An alternative is to duplicate the product, one for the card without message, and one for the card with the message.

Or, you could also do it like that, without any coding:
Don't use a characteristic. Have instead a custom item field with its column name being "amount" configured as a simple dropdown with two values, and in each value set the title to what you want to display as choice on the dropdown and as value the amount you want to charge. Then, in your text custom field, use the "display limited to" setting to only display it for one of the two values of your new custom item field.
Finally, configure the donation plugin of HikaShop: demo.hikashop.com/index.php/en/hikashop/...t-page/donation-page
That way, it will take the value selected in the dropdown by the customer as price for the product once in the cart.

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

  • Posts: 58
  • Thank you received: 2
6 years 6 months ago #279529

Hi,

thanks for this very clever solution.

Unfortunately I am already using the custom field with the table name "amount" for another purpose (fill in the amount for gift vouchers).
So I cannot save another custom field with "amount".

Duplicating products is the only solution without coding?

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
6 years 6 months ago #279548

Hi,

You could create another custom field, and make a copy of the donation plugin, changing its name and the field name in it so that you could have both running in parallel. However, without any coding knowledge, it will be difficult. But it will be easy for anyone knowing even a bit of coding. So you could ask our partners: www.hikashop.com/home/our-partners.html
They should be able to do that for you for a small price.

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

  • Posts: 64
  • Thank you received: 7
  • Hikashop Business
6 years 4 months ago #284995

nicolas wrote: Hi,
Or, you could also do it like that, without any coding:
Don't use a characteristic. Have instead a custom item field with its column name being "amount" configured as a simple dropdown with two values, and in each value set the title to what you want to display as choice on the dropdown and as value the amount you want to charge. Then, in your text custom field, use the "display limited to" setting to only display it for one of the two values of your new custom item field.
Finally, configure the donation plugin of HikaShop: demo.hikashop.com/index.php/en/hikashop/...t-page/donation-page
That way, it will take the value selected in the dropdown by the customer as price for the product once in the cart.


Hi Nicolas,

I have done what you suggest here and this is exactly the sollution to our problem. The only last problem is that this plugin changes the total price and not adds the amount in the selected field (from the donation plugin). Is it possible to alter the code so it adds the selected amount to the amount in the cart and not change the total amount? I guess I have to change this code:
$currencyClass->quantityPrices($product->prices,$quantity,$product->cart_product_total_quantity);

But I don't know how.

Thank you in advance,

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
6 years 4 months ago #284999

Hi,

No, this plugin doesn't replace the total.
What this plugin does is that it uses the value entered in the custom field of that product as the unit price of that product.
If you increase the quantity of that product, or if you add other products, the total will take these into account.
If you mean that you want that the value entered in the custom field of that product is added to the unit price of that product the you need to change all the place where you have the code:

hikashop_toFloat($product->$column)
to:
hikashop_toFloat($product->$column+$product->prices[$k]->price_value)
or
hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax)

The following user(s) said Thank You: justme

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

  • Posts: 64
  • Thank you received: 7
  • Hikashop Business
6 years 3 months ago #285523

Hi Nicolas,

A late response because I have been very sick. But I finally feel well enough to look at this again.
Thank you so much for this! You are great! This is exactly what I need.

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

  • Posts: 64
  • Thank you received: 7
  • Hikashop Business
6 years 3 months ago #285524

For everyone else looking for adding a custom item field with a price to go on top of the price of the product. Do as Nicolas explained and change the donation plugin located in plugins/system/custom_price. Below is the code for custom_price.php

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.6.4
 * @author	hikashop.com
 * @copyright	(C) 2010-2016 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
jimport('joomla.plugin.plugin');
class plgSystemCustom_price extends JPlugin{
}

if(!function_exists('hikashop_product_price_for_quantity_in_cart') && !function_exists('hikashop_product_price_for_quantity_in_order')) {
	function hikashop_product_price_for_quantity_in_cart(&$product){
		$currencyClass = hikashop_get('class.currency');
		$quantity = @$product->cart_product_quantity;


		$plugin = JPluginHelper::getPlugin('system', 'custom_price');
		if(version_compare(JVERSION,'2.5','<')){
			jimport('joomla.html.parameter');
			$params = new JParameter($plugin->params);
		} else {
			$params = new JRegistry($plugin->params);
		}

		$taxes = $params->get('taxes',0);

		$column = $params->get('field','amount');

		if(!empty($product->$column)){
			if(empty($product->prices)){
				$price= new stdClass();
				$price->price_currency_id = hikashop_getCurrency();
				$price->price_min_quantity = 1;
				$product->prices = array($price);
			}
			foreach($product->prices as $k => $price){
				if($taxes && $product->product_type=='variant' && empty($product->product_tax_id)){
					$productClass = hikashop_get('class.product');
					$main = $productClass->get($product->product_parent_id);
					$product->product_tax_id = $main->product_tax_id;
				}
				switch($taxes){
					case 2:
						$product->prices[$k]->price_value = $currencyClass->getUntaxedPrice(hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax),hikashop_getZone(),$product->product_tax_id);
						$product->prices[$k]->taxes=$currencyClass->taxRates;
						$product->prices[$k]->price_value_with_tax = hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax);
						break;
					case 1:
						$product->prices[$k]->price_value = hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax);
						$product->prices[$k]->price_value_with_tax = $currencyClass->getTaxedPrice(hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax),hikashop_getZone(),$product->product_tax_id);
						$product->prices[$k]->taxes=$currencyClass->taxRates;
						break;
					case 0:
					default:
						$product->prices[$k]->price_value = hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax);
						$product->prices[$k]->price_value_with_tax = hikashop_toFloat($product->$column+$product->prices[$k]->price_value_with_tax);
						break;
				}
			}
		}

		$currencyClass->quantityPrices($product->prices,$quantity,$product->cart_product_total_quantity);
	}
}

The following user(s) said Thank You: nicolas

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

Time to create page: 0.094 seconds
Powered by Kunena Forum