Jump to content

Cart.php question - How to add special alerts for certain products


craigedmonds

Recommended Posts

I have certain special products which need additional verification.

So what I would like to do is:

1. When someone adds a product and goes to cart.php?a=view
2. If the product is a certain productid or marked as a special product somehow
3. Show some custom html

See attachment for screenshot of what I am trying to achieve.

Is this possible? And if so, how would I go about it?


 

whmcs-request-1.png

Link to comment
Share on other sites

6 minutes ago, craigedmonds said:

Is this possible? And if so, how would I go about it?

to use the relevant hook point for this would likely be ShoppingCartViewCartOutput - an example of which is in the thread below...

now in your case, you're probably going to need to loop through the contents of the cart, to determine the content of the output.... e.g., there could be scenarios where multiple alerts could be triggered my the cart containing multiple applicable products.

Link to comment
Share on other sites

1 hour ago, brian! said:

to use the relevant hook point for this would likely be ShoppingCartViewCartOutput - an example of which is in the thread below...

now in your case, you're probably going to need to loop through the contents of the cart, to determine the content of the output.... e.g., there could be scenarios where multiple alerts could be triggered my the cart containing multiple applicable products.

Thanks. That has pointed  me in the right direction!!

This is what I have now when someone selects certain products.

Ideally would be nice to have it above the cart but I will keep looking.

This is the code I ended up with.

 I just manually define the special products and then loop through the cart.

Seems to work.
 

<?php
add_hook('ShoppingCartViewCartOutput', 1, function($vars) {

    //define list of products that need to show a warning
    $array_of_special_items[] = array(
	'special_pid' => 33, 
	'special_product_name' => "Mailtrust Professional Email Accounts", 
	);
    $array_of_special_items[] = array(
	'special_pid' => 79, 
	'special_product_name' => "Hosted Microsoft Exchange", 
	);
    
    //grab cart contents
    $products_list = $vars["cart"]["products"];

    //loops through the cart items
    $count = 0;
    foreach($products_list as $x) {

        //get product id
        $pid = $products_list[$count]["pid"];
        
        //loop through array_of_special_items to get any matching products
        $count_special = 0;
        foreach($array_of_special_items as $x) {
            
            $special_pid = $array_of_special_items[$count_special]["special_pid"];
            $special_product_name = $array_of_special_items[$count_special]["special_product_name"];
            
            if($special_pid == $pid) {
                $show_warning = true;
                $array_of_special_items_names[] = $special_product_name;
            } 
            
           $count_special = $count_special + 1;
            
        } //end loop $array_of_special_items
        
        $count = $count + 1;
        
    } //end loop $products_list
    
    //prepare cart warning for special products
    if($show_warning == true) {
        
        //prepare the list of products that match special items
        foreach($array_of_special_items_names as $names) {
            $special_items_list_html .=  "<div class='special-names'>$names</div>";            
        }
        
        $return = '
        <style>
        .special-names {
            background-color: #a94442;
            color: white;
            margin-bottom: 5px;
            padding: 10px;
            font-size: 16px;
        }
        </style>
        <p></p>
        <div class="alert alert-danger" role="alert">
        <h2 style="font-weight:bold;">BEFORE YOU CONTINUE TO CHECKOUT - PLEASE READ</h2>
        <p>One or more of the products in your cart are regarded as SPECIAL ITEMS.</p>
        <p>'.$special_items_list_html.'</p>
        <p>This means they require additional verification before the order can be activated.</p>
        <p><strong>Verification may include and or all of the below.</strong></p>
        <p>
        <ol>
            <li>Verifcation by Phone</li>
            <li>Verifcation by SMS</li>
            <li>Verifcation by Email</li>
            <li>Verifcation of Photio ID</li>
            <li>Verifcation of Utility Bill</li>
            <li>Verifcation of Credit Card</li>
        </ol>
        </p>
         <p><strong>Verification times may vary. During normal business hours, verification can take 1-3 hours. Outside of business hours it may take longer.</strong></p>
        </div>
        ';
    }
	
	return $return;
    
});

 

whmcs-request-2.png

Edited by craigedmonds
Link to comment
Share on other sites

21 minutes ago, craigedmonds said:

Ideally would be nice to have it above the cart but I will keep looking.

technically, the output will be wherever the code below is in the template...

                        {foreach $hookOutput as $output}
                            <div>
                                {$output}
                            </div>
                        {/foreach}

so if you move that code, you can output wherever you like...

1X0LHBa.png

I suppose if you weren't using promotion codes in the site, then you could use a ClientAreaPageCart hook to return an error message warning (yellow) alert - and that would by default be shown where you want your output without the need to change the template...

<?php

function cart_show_promo_error_hook($vars) {
	if ($vars['templatefile'] == 'viewcart') {
		$errormessage = "some message";
		return array("promoerrormessage" => $errormessage);
	}	
}
add_hook("ClientAreaPageCart", 1, "cart_show_promo_error_hook");

F0XgEwA.png

.. if you were using promo codes, then I would just get the content of the promomessage variable, e.g $vars['promoerrormessage'] and append your output to it.

the other way would be to use JS and insert your output into the appropriate div.

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