The support forum is in archive (read-only) mode and access is limited to members with a purchase history.

For technical support, please visit our support page.

General questions or topics can also be discussed on the ExpressionEngine StackExchange.

   
 
How to build a logical checkout process
Posted: 04 May 2011 03:56 AM   [ Ignore ]
Wallflower
Rank
Total Posts:  2
Joined  2010-05-13

I’m trying to add some logic and validation to the checkout process of a CartThrob site but I’m running into some issues. I think I might be missing something.

What I want to happen is if a shopper doesn’t have any shippable products in their cart I want the user to not have to fill out the shipping form. Conversely, if a user has shippable products in their cart I want them to have to input their shipping data. The way CartThrob currently works for me is that a user can just skip the shipping form.

I had thought there would be a tag within CartThrob to check for shippable products on the shipping page like the below:

{if cart_is_shippable}
  //display shipping form

{if:else}
  //redirect to payment form

{/if}

I’m pretty sure I can just loop over each cart entry, checking if there’s a shippable product within the cart, but that seems like overkill.

Is there some simpler way to do this I’m missing? This being a commercial product I’m reluctant to invest time into writing a custom plugin or module so, being a customer and all, I’m hoping there’s some way I’m missing.

Thanks,
Eric

[ Edited: 04 May 2011 04:20 AM by Victory ]
Profile
 
 
Posted: 05 May 2011 01:41 PM   [ Ignore ]   [ # 1 ]
Absolute Heartthrob!
Avatar
RankRankRankRankRank
Total Posts:  10369
Joined  2008-09-29

Apologies for the slow reply. Catching up tonight…..

There’s not a straightforward cart_is_shippable tag. However, you might be able to use this:

http://cartthrob.com/docs/tags_detail/cart_shipping/

{if "{exp:cartthrob:cart_shipping prefix="" decimals="2"}" == "0.00"}
display shipping
{if
:else}
etc 

Rather than check for shippable items, check for shipping costs.

 Signature 
Profile
 
 
Posted: 06 May 2011 01:34 AM   [ Ignore ]   [ # 2 ]
Wallflower
Rank
Total Posts:  2
Joined  2010-05-13

Thanks for the reply. Unfortunately though, your solution, while effective if there’s a shipping cost, falls down if the site administrator wants to offer free shipping on certain products.

The solution I came up with was to loop over the cart, check each product if it’s shippable, and, if one is, to forward users to the shipping form. Here’s the code if you’re interested:

//checkout/index
{if "{exp:cartthrob:total_items_count}"==0}
    {redirect
="products/index"}
{
/if}

{exp
:cartthrob:cart_items_info}
    {if product_shippable 
== 'Yes'}
        {redirect
="checkout/shipping"}
    {
/if}
    
{
/exp:cartthrob:cart_items_info}

{redirect
="checkout/billing"

Thanks though,
Eric

Profile
 
 
Posted: 06 May 2011 02:29 AM   [ Ignore ]   [ # 3 ]
Absolute Heartthrob!
Avatar
RankRankRankRankRank
Total Posts:  10369
Joined  2008-09-29

I’ll consider adding the tag, or a variable somewhere.

 Signature 
Profile
 
 
Posted: 07 May 2011 01:05 PM   [ Ignore ]   [ # 4 ]
Wallflower
Rank
Total Posts:  2
Joined  2010-05-13

Hi Chris,

I had a dentist appointment today so I couldn’t go out and play tonight so I took a stab at a solution:

Just add the below method to mod.cartthrob.php (within the class obviously) and call it like the below within your templates:

{if "{exp:cartthrob:is_shippable}" == 1}
    {redirect
="checkout/shipping"}
{
/if} 
/**
     * Checks if a given cart has any shippable products based off of product_shippable
     * @return bool
     */
    
public function is_shippable()
    
{
        
foreach ($this->cart->items() as $row_id => $item)
        
{        
            $product 
= ($item->product_id()) ? $this->EE->product_model->get_product($item->product_id()) : FALSE;
            if (
$product)
            
{
                $data 
$this->EE->cartthrob_entries_model->entry_vars($product);
                if(
$data && isset($data['product_shippable']) && $data['product_shippable'== 'Yes')
                
{
                    
return TRUE;
                
}
            }
        }
    } 

If nothing else maybe you can use this as a starting point if you decide to implement smile

Eric

Profile