Author Topic: [Solved NEARLY] Customer Choose FREE product if order value over X  (Read 16258 times)

Vdub

  • Jr. Member
  • **
  • Posts: 149
I just need a way to hide free products if customer has already selected one, anyone help with this

Original code from Jan Jezek, modified a little for my needs.

The code below when added to just before the the last <tr> tag at around line 90 in

www\administrator\components\com_virtuemart\html\templates\basketbasket_b2c.html

Will give a customer the opton of selecting a free gift if order value is above X

You will need to set up a new catergory and make sure it is unpublished
Add your products to this category valued at 0.00

Enter details of products and category into code below as indicated


edit: just looking at the code, it probably does need the php echo, just the relevant details


Code: [Select]
<!--  CUSTOMER FREEBIE IF THEY SPEND OVER 
Ceate a New Catergory (make it unpublished)
Create new products with Zero value add to New category and make them unpublished


change Value of $total to prefered value. 
-->

<?php if ( $total 100) { ?>

<tr><td colspan="7">
<hr />
<br /><br />
<form action="<?php $mm_action_url?>index.php" method="post" name="addtocart" id="addtocart">
<center><h2>Your order value is over £100.00</h2></center>
<center><h3>Please select your free gift below</h3></center>

<!-- Replace Product details in the next 3 lines, add more lines as required -->
<input type="radio" name="product1" value="<?php echo 'Product 1 ID';?>" /><?php echo 'Name of product 1 here';?><br />
<input type="radio" name="product2" value="<?php echo 'Product 2 ID';?>" /><?php echo 'Name of product 2 here';?><br />
<input type="radio" name="product3" value="<?php echo 'Product 3 ID';?>" /><?php echo 'Name of product 3 here';?><br />

<!-- Replace category id with the number of your created Free category both places  -->
<input type="hidden" name="category_id" value="<?php @$_REQUEST['category_id']; ?>">
<input type="hidden" name="page" value="shop.cart">
<input type="hidden" name="func" value="cartadd">
<input type="hidden" name="option" value="com_virtuemart">
<!-- change url to button as required images/buynow.gif  -->
<input type="submit" style="text-align:center;background-position:bottom left;font-size:10;width:103px;height:30;cursor:pointer;border:none;background: url('images/buynow.gif') no-repeat center transparent;background-position: 0 -2px;vertical-align: middle;" value="<?php echo "get it free"?>" title="<?php echo "get it free";?>">
</form>   </td></tr>
<?php ?>

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #1 on: October 18, 2007, 04:15:15 am »
 

Hi Vdub,

Thanx for this modification... and J. Jezek thanx for the original code, it kept me thinking. After a little bit of puzzling I think the problem is solved. I modified the code and made a file from it (called: free.product_add.php). See here below the main part of this file which ables you to add a free product when a certain order-amount has been reached

OPTION 1.

Code: [Select]
<?php 
defined
'_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*
* @version $Id: free.product_add.php $
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2007-2008 Marlon. All rights reserved. Inspired by the code from J. Jezek and Modification by Vdub.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.

* http://virtuemart.net
*/

mm_showMyFileName__FILE__ );
global 
$page;

if ( 
$total 150.00) {

echo 
"<table width=\"100%\"><tr><td>

<form action=\""
.$mm_action_url "index.php\" method=\"post\" name=\"addtocart\" id=\"addtocart\">
<center><h2>Your order value is over € 150.00</h2></center>
<center><h3>Please add your free gift below</h3></center>

<!-- Replace Product-ID-number in the value field to the desired product-id -->
<input type=\"hidden\" name=\"product_id\" value=\"18\" /><img src=\"productimage.jpg\"><br />some text like: only one item is allowed<br />
<!-- Replace Category-ID-number in the value field to the desired category-id -->
<input type=\"hidden\" name=\"category_id\" value=\"7\">
<input type=\"hidden\" name=\"page\" value=\"shop.cart\">
<input type=\"hidden\" name=\"func\" value=\"cartadd\">
<input type=\"hidden\" name=\"option\" value=\"com_virtuemart\">
<input type=\"submit\" value=\"add FREE product\">
</form>

</td></tr></table>"
;

}
?>

Save this code above as: free.product_add.php
and put it in administrator\components\com_virtuemart\html



Then open basket.php (administrator\components\com_virtuemart\html) and add after:
Code: [Select]
        /* Input Field for the Coupon Code */
        if( PSHOP_COUPONS_ENABLE=='1'
                && !@$_SESSION['coupon_redeemed']
                && ($page == "shop.cart"
                        || @$checkout_this_step == CHECK_OUT_GET_PAYMENT_METHOD
                        || @$checkout_this_step == CHECK_OUT_GET_SHIPPING_ADDR && CHECKOUT_STYLE != 3
                        || @$checkout_this_step == CHECK_OUT_GET_SHIPPING_METHOD && CHECKOUT_STYLE == 3
                        )
) {
include (PAGEPATH."coupon.coupon_field.php");
}

the following code:
Code: [Select]
        /* begin of Field for the Free Product Code */
        if($show_basket
                && ($page == "shop.cart"
                        )
    ) {
include (PAGEPATH."free.product_add.php");
 }
                /* end of Field for the Free Product Code */


Now when the total amount is € 150, the FREE product field will appear in the first Cart page. And this field will disappear going to the checkout. You can change the amount to your desired amount.

Good luck,
Marlon

NOTE please also follow the instructions that Vdub describes in his post above
Quote
You will need to set up a new catergory and make sure it is unpublished
Add your products to this category valued at 0.00



SEE ALSO UPDATED CODE BELOW!!!


-----

Mike C

  • Jr. Member
  • **
  • Posts: 165
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #2 on: October 19, 2007, 00:31:32 am »
What a great idea! Does this work? Can I see it in action on someone's site?

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #3 on: October 19, 2007, 01:02:33 am »
Yes it works  :D

There is only one thing I would like to see happening.
After clicking on the submit (add) button I would like to see the form to disappear right away. And not only to disappear if you go to the checkout as it does now (which is good ofcourse) ;)




[ALMOST SOLVED ] see post below



...


Mike C

  • Jr. Member
  • **
  • Posts: 165
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #4 on: October 19, 2007, 01:04:42 am »
Marlon, can I see this on your website?

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #5 on: October 19, 2007, 01:27:02 am »
Yes I will put the code (free.product_add.php) on my test server:

link NOT active...

It will appear if the order total is over $ 150,-

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #6 on: October 19, 2007, 18:06:53 pm »
UPDATED OPTION 2 !!

I've got the immediately disappear part after submitting working with the following code.
There is only one thing to be solved (see bottom of this post) ;D

Code: [Select]
<?php 
defined
'_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*
* @version $Id: free.product_add.php $
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2007-2008 Marlon. All rights reserved. Inspired by the code from J. Jezek and Modification by Vdub.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.

* http://virtuemart.net
*/

mm_showMyFileName__FILE__ );
global 
$page;


for (
$i=0;$i<$_SESSION["cart"]["idx"];$i++) {

//Replace total order amount (150.00) and product_id number (30) to whatever you like.
if ($_SESSION['cart'][$i]["product_id"] == 30 &&  $total 150.00 )
{
echo 
"<table width=\"100%\"><tr><td>
<!-- If you want you can put some text here or leave it blank -->
<h3>Your total order amount is below € 150,-<br>
The FREE product is not valid anymore !!</h3>
</td></tr></table>"
;
}
//Replace product_id number (30) to whatever you like
elseif ($_SESSION['cart'][$i]["product_id"] == 30) {
echo 
"<table width=\"100%\"><tr><td>
<!-- If you want you can put some text here or leave it blank -->
<b>The FREE product is yours!</b><br> 1x FREEBEE per customer
</td></tr></table>"
;
}
//Replace total order amount (150.00) to whatever you like
//And in the form below Replace product_id (30) to whatever you like
elseif ($_SESSION['cart'][$i]["product_id"] == $product_id &&  $total 150.00 ) {
echo 
"<table width=\"100%\"><tr><td>

<form action=\""
.$mm_action_url "index.php\" method=\"post\" name=\"addtocart\" id=\"addtocart\">
<center><h2>Your order value is over € 150.00</h2></center>
<center><h3>Please add your free gift below</h3></center>
<table border=\"0\" align=\"center\"><td><img src=\"image_image_image.jpg\"></td><td width=\"20\"> </td><td>some text like bladiebla<br />
<!-- Replace Product-ID-number in the value field to the desired product-id -->
<input type=\"hidden\" name=\"product_id\" value=\"30\" />
<input type=\"hidden\" name=\"page\" value=\"shop.cart\">
<input type=\"hidden\" name=\"func\" value=\"cartadd\">
<input type=\"hidden\" name=\"option\" value=\"com_virtuemart\">
<input type=\"submit\" value=\"toevoegen\"></td></table>
</form>

</td></tr></table>"
;
}
}




?>

save code above  as free.product_add.php :


and in basket.php after this code :
Code: [Select]
        /* Input Field for the Coupon Code */
        if( PSHOP_COUPONS_ENABLE=='1'
                && !@$_SESSION['coupon_redeemed']
                && ($page == "shop.cart"
                        || @$checkout_this_step == CHECK_OUT_GET_PAYMENT_METHOD
                        || @$checkout_this_step == CHECK_OUT_GET_SHIPPING_ADDR && CHECKOUT_STYLE != 3
                        || @$checkout_this_step == CHECK_OUT_GET_SHIPPING_METHOD && CHECKOUT_STYLE == 3
                        )
) {
include (PAGEPATH."coupon.coupon_field.php");
}


add:
Code: [Select]
        /* begin of Field for the Free Product Code */
        if($show_basket
                && ($page == "shop.cart"
                        || @$checkout_this_step == CHECK_OUT_GET_SHIPPING_ADDR && CHECKOUT_STYLE != 3
                      )
    ) {
include (PAGEPATH."free.product_add.php");
 }
                /* end of Field for the Free Product Code */
Now when the amount changes to less then € 150,- when the FREE product is already added, the customer also gets a warning that the freebee becomes invalid.

This code is not optimal to use, because after adding a new product the form displays itself again:
elseif ($_SESSION['cart'][$i]["product_id"] == $product_id &&  $total > 150.00 )
How to solve this???

Marlon

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #7 on: October 24, 2007, 00:11:56 am »
Does anybody have a solution what to do with this one?  ???

Kind regards
Marlon

Anthony C.

  • Full Member
  • ***
  • Posts: 286
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #8 on: October 26, 2007, 00:18:13 am »
Hi Marlon,

here is my one cent contribution:

replace your codes with the following:

Code: [Select]
<?php 
defined
'_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*
* @version $Id: free.product_add.php $
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2007-2008 Marlon. All rights reserved. Inspired by the code from J. Jezek and Modification by Vdub.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.

* http://virtuemart.net
*
*
*/

mm_showMyFileName__FILE__ );
global 
$page$total;

$freeItemID "367" // Free Item ID
$freeAmount "150.00"; // Amount that will get a free item

for ($i=0;$i<$_SESSION["cart"]["idx"];$i++) {

//Replace total order amount ($freeAmount) and product_id number ($freeItemID) to whatever you like.
if ($_SESSION['cart'][$i]["product_id"] == $freeItemID &&  $total $freeAmount )
{
echo 
"<table width=\"100%\"><tr><td>
<!-- If you want you can put some text here or leave it blank -->
<h3>Your total order amount is below € 150,-<br>
The FREE product is not valid anymore !!</h3>
</td></tr></table>"
;
$freeItem["product_id"] = $freeItemID;
require_once(
CLASSPATH.'ps_cart.php');
$ps_cart = new ps_cart;
$ps_cart->delete($freeItem);
}
//Replace product_id number ($freeItemID) to whatever you like
elseif ($_SESSION['cart'][$i]["product_id"] == $freeItemID) {
echo 
"<table width=\"100%\"><tr><td>
<!-- If you want you can put some text here or leave it blank -->
<b>The FREE product is yours!</b><br> 1x FREEBEE per customer
</td></tr></table>"
;
}
//Replace total order amount ($freeAmount) to whatever you like
//And in the form below Replace product_id ($freeItemID) to whatever you like
elseif ($_SESSION['cart'][$i]["product_id"] == $product_id &&  $total $freeAmount ) {
echo 
"<table width=\"100%\"><tr><td>

<form action=\""
.$mm_action_url "index.php\" method=\"post\" name=\"addtocart\" id=\"addtocart\">
<center><h2>Your order value is over $150</h2></center>
<center><h3>Please add your free gift below</h3></center>
<table border=\"0\" align=\"center\"><td><img src=\"image_image_image.jpg\"></td><td width=\"20\"> </td><td>This is the Free Item<br />
<!-- Replace Product-ID-number in the value field to the desired product-id -->
<input type=\"hidden\" name=\"product_id\" value=\"
$freeItemID\" />
<input type=\"hidden\" name=\"page\" value=\"shop.cart\">
<input type=\"hidden\" name=\"func\" value=\"cartadd\">
<input type=\"hidden\" name=\"option\" value=\"com_virtuemart\">
<input type=\"submit\" value=\"Add Free Item\"></td></table>
</form>

</td></tr></table>"
;
}
}

?>

actually this is not what I want but hope it helps someone.

Anthony

Marlon

  • Beginner
  • *
  • Posts: 38
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #9 on: October 27, 2007, 16:58:38 pm »
Hi Anthony,

THX! This is actually better....  ;D
 
But after submitting a new item when the freebee is already in the basket, the form displays itself again. How to solve this?


Beste regards,
Marlon

Anthony C.

  • Full Member
  • ***
  • Posts: 286
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #10 on: October 27, 2007, 22:36:21 pm »
Hi Marlon,

you need to rewrite the whole structure.
use some for loop to complete your request. If else sometime can't solve such question.

Regards,

Anthony

John - Australia

  • Beginner
  • *
  • Posts: 47
Not getting product information
« Reply #11 on: November 19, 2007, 05:56:40 am »
Hi
I have tested this and it loks great, but I cannot get me products to show up on the basket, I ust get the default data
Name of product 1 here
Name of product 2 here
Name of product 3 here

This is the code I am using

<input type="radio" name="product_id]" value="1099"<?php echo 'Product_id';?>"><?php echo 'product_name';?><br />

Also
<!-- Replace category id with the number of your created Free category both places  -->

I can only find one entry for category_id

<input type="hidden" name="category_id" value="47"<?php @$_REQUEST['category_id']; ?>">

obviously it doesn't add anything to the cart when the radio button is checked

Help
John

Preston Moore

  • Jr. Member
  • **
  • Posts: 80
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #12 on: December 10, 2007, 15:25:46 pm »
Has this been given up on?  This is a great mod to Virtuemart and I can't believe somebody hasn't picked this up and made it work right.  Marlon and others are on to something here...where's their support?  Oh yeah, I forgot, this is Virtuemart's forum. 

Don't mean to be so cynical, but I've never been to a forum with so many unanswered pleas for help.  Props to the people who do lend a hand and volunteer their time (including the Virtuemart team) but it's almost like you have to pay somebody (I wonder who...) to get any help around here.
You can pick your friends, and you can pick your code, but you can't pick your friend's code.

sfinx

  • Beginner
  • *
  • Posts: 31
Re: [Solved NEARLY] Customer Choose FREE product if order value over X
« Reply #13 on: January 25, 2014, 15:36:44 pm »
Was anyone able to update and use this method in VM 2.0.22 ? I am trying like crazy, but I dont have appropriate php knowledge...