VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: OliviaH on July 04, 2016, 15:58:53 PM

Title: Custom Field checkbox to add free sample
Post by: OliviaH on July 04, 2016, 15:58:53 PM
I have been trying to no avail to create a button for a free sample of many of the products on my site.
I first went down the route of making an edited 'add to cart' button that added a set 'Free Sample' product, however I can not figure out how to make it show what product it has come from; I tried to make the 'Free Sample' product's customfield the name or SKU of the main product, however I could not figure out how to. The inner workings of how the data is sent and stored for the cart is beyond my level of knowledge/detective skills.

I am now trying, the perhaps simpler method, of adding a cutomfield to the main product that simply sets the price to 0.00 (0.0001). However so far I have only been able to make the option a radio button that deducts the price from it.
What I want is a checkbox (rather than a radio button) that simply sets the price to 0.0001 (rather than deducting let's say 9.999). This is because I have hundreds of products and setting all those prices will take a very long time and is prone to errors.


So my questions are:


Joomla 3.4.8
Virtuemart 3.0.12
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 04, 2016, 17:45:07 PM
its a bit hard to understand what you want to do?

2 buttons? 1 product, 1 free sample?
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 10:23:48 AM
Ideally, one button to add a free sample of the product to the cart; so basically another add to cart that would add eg: 'Free Sample: Product 1' to the cart.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 11:06:22 AM
I do something similar on adding say gift wrapping ..
In your case  link the free sample in some way to the product

e.g. by sku

product sku xxxx
free sample sku free_xxxx

then add a 2nd add to cart button and a query to find the correct product id to use in it

e.g.

$database = JFactory::getDbo();

$queryfindorders = $database->getQuery(true);
$queryfindorders
->select('virtuemart_product_id')
->from($database->quoteName('#__virtuemart_products'))
->where($database->quoteName('product_sku') . '="free_'.$this->virtuemart_product_sku.'"');
$database->setQuery($queryfindorders);
$database->execute();
$freeid = $database->loadResult() ;


code not checked but the idea will work
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 11:11:53 AM
Yeah that's the only way I can figure to do it currently. However adding another 800+ products is using up space that I wanted to avoid using.
There must be a way of changing the data sent to the cart via code. I have a generic 'Free Sample' product with a textinput custom field, wondered if anyone knew how to change what the value of that was with a button and while not on that product. Big ask I know.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 11:19:37 AM
If u don't want to config the free samples as products you just need to flag in the cart that it is a freebie, not the actual

so as u tried, some sort of custom field.. maybe a hidden chk or radio that is set by JS when the free add to cart is clicked?

or by JS change the pname same to something that tells you he wants the sample -- you need to experiment
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 11:35:48 AM
I hadn't thought of doing the code in the cart, I shall have a play with that.
Cheers
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 12:26:33 PM
no - I meant when it gets to the cart I assume you just need something to tell who ever does the orders that they are requesting a free sample
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 12:30:21 PM
Well I could do that, the problem I am having is making the product price 0 with a custom field, without making each of them deduct the sales price.
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 12:31:38 PM
I have no idea how to change what data is sent to the cart as I don't know how the data is sent, too advanced for me.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 13:07:16 PM
as I suggested

if he hits the free sample add to cart change the pname by JS (add "free sample" to it) and submit
in the cart and emails etc display this

or some similar method
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 13:31:50 PM
What I'm saying is I don't know how to change the pname.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 14:40:07 PM
 I would try by javascript - its just a text in a hidden field.. then submit
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 15:10:05 PM
Oh that yeah, I have tried changing that in the php file, didn't change what was in the cart.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 16:50:22 PM
FFS, u said u didn't want the cart changed?

you just want to FLAG that they hit the free add to cart button!
so u change something .. detect it at the cart .. render it or display something else so who ever deals with your orders knows its a freebie...
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 05, 2016, 16:55:25 PM
Never said I didn't want the cart changed.
Flagging it is helpful but if the price isn't changed to 0 then the customer would be charged the product cost, no?
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 05, 2016, 17:53:31 PM
It wont of course just by this.. u ten need (I assume) to manipulate the cart to reduce to 0.0001 ..

but this is all suck and see.. I don't need this so I am only making suggestions for you to follow thru on

look for example how the vmcustom textinput plugin manipulates the price in cart

public function plgVmPrepareCartProduct(&$product, &$customfield,$selected,&$modificatorSum){
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 06, 2016, 10:52:39 AM
Thank you for your help. But I don't know enough about how virtuemart works or php to figure how it all works on my own.
I'll have to stick to the extra products for now.
I appreciate your help but I am just not experienced/knowledgeable enough to work it all out.
Title: Re: Custom Field checkbox to add free sample
Post by: balai on July 06, 2016, 14:44:45 PM
Alternatively you can try product bundles.
https://breakdesigns.net/extensions/product-bundles
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 06, 2016, 16:31:01 PM
@balai I don't see how that would help
Title: Re: Custom Field checkbox to add free sample
Post by: balai on July 07, 2016, 10:10:55 AM
By letting you add to the cart a main product together with your free one.
Just thought that could be helpful. You can check it and decide
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 07, 2016, 17:54:42 PM
I don't want it to add both products.
Title: Re: Custom Field checkbox to add free sample
Post by: GJC Web Design on July 07, 2016, 18:20:16 PM
1. you don't want to use products
2. u don't have the skills to code an alternate solution

solution?  Hire someone with the skills....  unlikely anyone here is going to use their time to code your website - that's the way the world works...
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 08, 2016, 09:01:19 AM
Well my question initially was how to make a custom field a checkbox or set the price to 0, don't see how that's asking anyone to code my website.
I was asking for help, all you have to say is, well, nothing if you don't wish to help.
Title: Re: Custom Field checkbox to add free sample
Post by: jenkinhill on July 08, 2016, 15:19:29 PM
Abuse of other forum members is not acceptable behaviour. You have been offered suggestions by an experienced developer developer giving up some of his free time to try to help. You are ssking for something that no other forum member has asked  before, and as you believe that none of the suggestions are helpful there comes a point where you should commission a developer/programmer to solve the problem for you.
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 08, 2016, 15:26:53 PM
I did actually thank him for his help:
Quote from: DanBH on July 06, 2016, 10:52:39 AM
Thank you for your help. But I don't know enough about how virtuemart works or php to figure how it all works on my own.
I'll have to stick to the extra products for now.
I appreciate your help but I am just not experienced/knowledgeable enough to work it all out.

So I felt rather offended when told:
Quote from: GJC Web Design on July 07, 2016, 18:20:16 PM
that's the way the world works...
Since I was only asking if he could help, not demanding it, if he couldn't help he didn't need to talk down to me just because I know less than him.

But you can rest assured it will not happen again.
(PS I would have written this in a PM to you jenkinhill, but I don't know the correct procedure here. Is there a place that has all the forum rules, can't seem to find it)
Title: Re: Custom Field checkbox to add free sample
Post by: Studio 42 on July 09, 2016, 01:25:41 AM
Hi DanBH,
If you need a specific changes then try to be specific.
Free sample is possible using a customfield list and minus the price in the variant.
Another solution is to use child variant with price 0.
In all case if you give free sample, for same product, the stock is changed. So for stock you need to use a child variant, or a product "free sample"(you can use related product for eg.)
If you need free sample for many product and dont want use the child variant, related... then you can check for a developer, to do it.
Another solution, is to use a button+javascript displayed in product, that add a free sample with the right information.
In all case, or you need to add in all products the option. Or develop a code to do this automatically.

Do not think that GJC, never help, but time is money, and we cannot always loose time, when you do not can do it or not test the proposed solutions.

Regards,
Patrick
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 11, 2016, 10:04:32 AM
Where my questions not specific enough??

Quote from: DanBH on July 04, 2016, 15:58:53 PM
So my questions are:

  • How can I make the Custom Field a checkbox rather than radio button.
  • How would I make the custom field a set price rather than it adding/subtracting a value from the price.

Serious question, as I thought I was really specific and made my questions generic, I just added why I wanted these changes incase I was missing a really obvious solution.
Like I said before I appreciated the help, just not being belittled, even when I had said I couldn't figure most of it out so would go with the option I was trying to avoid but sadly could not.
Also a developer just isn't an option as my company will not pay for it, it has been put onto me to make these changes.


PS. We don't use stock on our website, so luckily I don't have to worry about that.
Title: Re: Custom Field checkbox to add free sample
Post by: Studio 42 on July 11, 2016, 10:49:49 AM
If you really need to change price, and use ratio button.
Best solution at all, is to do a plugin.
Check the custom field text input plugin, and change the code for your need, this need only some line changes.
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 11, 2016, 10:59:08 AM
I have had to go with the solution of making a sample product for each product.
Custom fields as far as I can decipher, can only deduct from the price, rather than set it, which is what I would need to do.
If there is a way to edit the custom text input plugin I have not been able to figure it out.

PS. I have tried to use the plugin, as well as all other custom fields, the code is just a bit beyond me, at least how virtuemart works is a bit beyond me. I could do the radio buttons, but I would need to make this a checkbox or button to achieve the ease of use we want.
But thank you for your help.
Title: Re: Custom Field checkbox to add free sample
Post by: jenkinhill on July 11, 2016, 12:21:19 PM
You can set  custom price with this plugin:  http://forum.virtuemart.net/index.php?topic=127362.0
Title: Re: Custom Field checkbox to add free sample
Post by: OliviaH on July 11, 2016, 15:20:13 PM
Awesome thank you jenkinhill, I shall have a look into that and post results.