Jump to content

Add a free product to cart


pRieStaKos

Recommended Posts

Hello,

I would like to add a free product every time a client adds a specific product from a product group.

I'm trying to find a hook and an API call to use for this, but I cannot find anything related.

Note: Due to complex product configurations and other provisioning actions I cannot use bundles for this.

Any suggestion ?

Thank you in advance

Link to comment
Share on other sites

1 minute ago, pRieStaKos said:

Any suggestion ?

use a bundle.... oh i'm joking! 😃

if you had said it was a product addon, i'd have suggested enabling it in the link... but I guess you're going to tell me that it's a product.

the cart uses the session array, so have you tried adding your free product to the $session ?

Link to comment
Share on other sites

@brian! Thank you again (typical) for your help. 😅

I managed to add a free product by using `PreCalculateCartTotals` and push a product array in `$_SESSION['cart']['products']`

$addProduct = array(
    "pid" => $customProduct,
    "qty" => "1",
    "domain" => "",
    "billingcycle" => NULL,
    "configoptions" => NULL,
    "customfields" => NULL,
    "addons" => NULL,
    "server" => "");
array_push($_SESSION['cart']['products'], $addProduct);

 

Link to comment
Share on other sites

array pushing the free product into the session should add the product - if you're going to want it to occur after c/o, then you might need to set a conditional statement to check whether c/o X has a value, and only if that's the case to push the array.

you will also need to ensure that you only add it the free product once, e.g if the user refreshes viewcart, ensure that the hook doesn't keep on adding multiple free products.

Link to comment
Share on other sites

21 hours ago, brian! said:

array pushing the free product into the session should add the product - if you're going to want it to occur after c/o, then you might need to set a conditional statement to check whether c/o X has a value, and only if that's the case to push the array.

you will also need to ensure that you only add it the free product once, e.g if the user refreshes viewcart, ensure that the hook doesn't keep on adding multiple free products.

I'm already doing it .

The problem is that in action hook "ClientArePageCart" or "PreCalculateCartTotals" the free product is added when you are on configoption page too. I want to add the free product only on Review & Checkout page.

 

Link to comment
Share on other sites

23 hours ago, pRieStaKos said:

The problem is that in action hook "ClientArePageCart" or "PreCalculateCartTotals" the free product is added when you are on configoption page too. I want to add the free product only on Review & Checkout page.

even if you wrap the hook function in an if ($vars['templatefile'] == 'viewcart') { condition to ensure that it only runs on viewcart/checkout ?

3 hours ago, pRieStaKos said:

UPDATE: I finally used "ShoppingCartViewCartOutput" and checking for "noconfig" variable. It worked...

there are plenty of ways to skin a cat! 😼

Link to comment
Share on other sites

  • 3 years later...

There is a better, less non-invasive way to achieve this.

1.  GoTo [WHMCS] -> [Products & Services] -> [Product Addons] -> [{Product to Add The Products To}] -> [Applicable Products] -> [{Add Preferred Addon To Selected Products}]

2. The on the Hosting Server, Edit the file [{Your Template Folder}] -> [Store} -> [Order.tpl}

3. Find the line [

<form method="post" action="{routePath('cart-order-addtocart')}" id="frmAddToCart">

]

4. Replace it with [

<form method="post" action=" {$WEB_ROOT}/cart.php?a=confproduct&i=0" id="frmAddToCart">

]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated