Archive for December, 2012

Laravel and WordPress as a subfolder

I wanted to recreate a small blog for a site I was building in Laravel for a bit of fun but didn’t want to waste/spend loads of time recreating a blog. When in my honest opinion WordPress does an un-touchably good job of that already.

If you want to just simply install WordPress in your Laravel install under a subfolder, it’s quite simple really. Just install into public/blog (or whatever you want to name your blog).

Mac terminal fan? Little SVN one liner for you.

cd public | mkdir blog && cd blog | svn co http://core.svn.wordpress.org/tags/3.5 .

If you don’t use the public folder as your vhosts root like me you could use a .htaccess rule instead.

# Apache configuration file
# http://httpd.apache.org/docs/2.2/mod/quickreference.html
# Note: ".htaccess" files are an overhead for each request. This logic should
# be placed in your Apache config whenever possible.
# http://httpd.apache.org/docs/2.2/howto/htaccess.html
# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.
<IfModule mod_rewrite.c>
	Options +FollowSymLinks
	RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Maybe something like that might help? But don’t hold me to it. You could also use a exclusion rule in your .htaccess file.

RewriteCond %{REQUEST_URI} !^/(~blog|/.*)$

MySQL rand unix timestamp without additional PHP loop + subquery

Figured we could improve a PHP script to not require an additional loop around records and sub query today.

This query will insert a random unix timestamp between now and 86399 seconds time (or almost one day) for each row.

I’ve found it to be an improvement on sub queries.

UPDATE dummy_table SET time_field = UNIX_TIMESTAMP(NOW())+FLOOR(1 + (RAND() * 86399));

A rule of life

Make it a rule of life never to regret and never to look back. Regret is an appalling waste of energy; you can’t build on it; it’s only good for wallowing in.

Lemonstand get unpaid orders by customer

During the checkout process Lemonstand automatically clears your shopping cart in favour of creating a Shop_Order object which is pretty static and can be paid for at any point in the future. This object represents the state of your cart when you clicked pay now.

Sometimes (as many of you will know) a user will click pay and not actually either a) want to pay or b) able to pay successfully. Unfortunately in most theme designs the cart is usually fairly prominent in the UI design and serves as a reference point before you checkout.

This is fine, that is, until a user clicks pay. After they click pay if they choose to navigate away little do they know but they’ll find it tricky to get back to that point again without visiting a ‘My account’ page or something similar. Which at this early stage and since in alot of users eyes your not a customer with an account until you check out and complete an order is not great.

We wanted to be able to show the customer there unfinished order in two places so to make it a little easier to pick up where you left off. Below I discuss a couple of ways that we have implemented this.

*Note: Lemonstand do not provide a function for selecting unpaid orders.

/* The order status id way */
$unPaid = Shop_Order::create()->where('customer_id = ?',$this->customer->id)->where('status_id != ?',$your_unpaid_status_id)->limit(2)->find_all(); ?>
/**
  * The more flexible way perhaps for sellable themes.
  * - works best when no status id is known */
$unPaid = Shop_Order::create()->where('customer_id = ?',$this->customer->id)->find_all();
$un_paid = 0;
if(count($unPaid) > 0)
{
    foreach($unPaid as $u_order)
    {
        if(!$u_order->is_paid())
        {
            $un_paid++;
            # Do what you like here.
            echo 'Order ID '.$u_order->id.' is unpaid';
        }
    }
}
/**
  * The tidy way for a non re-sellable theme.
  * - loaded in through a custom module (perhaps containing helper functions)
  * - works best when no status id is known */
# To be placed in your custom module.
function has_unpaid_orders($customer_id){
    $orderCount = 0;
    if($customer_id)
    {
        $unPaid = Shop_Order::create()->where('customer_id = ?',$customer_id)->find_all();
        if(count($unPaid) > 0)
        {
            foreach($unPaid as $u_order)
            {
                if(!$u_order->is_paid())
                {
                    $orderCount++;
                }
            }
        }
    }
    return $orderCount;
}
/* Called from your template like this */
Your_Helper::has_unpaid_orders($this->customer->id);
/* or */
if(Your_Helper::has_unpaid_orders($this->customer->id))
{
    # do something
}

If you are unsure what we mean by a custom helper module. Shortly I will describe in another post how you can achieve this.

Lemonstand back to search or category results

Today I had to add a basic back to search results implementation and thought i’d blog it for future reference.

I could not find anything implemented in the documentation so decided to add this snippet of code to any of the category or search pages that I had. Be sure to add it inside of the partial which gets re-loaded during pagination changes to continuously update the session as the user browses.

<?php Phpr::$session->set('back_r', $_SERVER['REQUEST_URI']); ?>

You can imagine whats next, open your product page / product partial (depending on your setup) and add the following lines where you would like to display the back to search results link.

$back_r = Phpr::$session->get('back_r');
if($back_r)
{
    echo '<a href="'.root_url($back_r).'" title="Back to search results">&laquo; back</a>';
}

Hopefully this might be of use to someone else.