Tag: php

Using papertrail logging in Slim Framework v3 with MonoLog

I was looking for a good way to group logs from different webapplications into one managable place, without too much configuration work on the different webservers, since some run on shared hosting, and some have different setups.

I came across Paper Trail App A hosted log management tool, which gives you the ability to aggegrate logs from different places into one location, and here you can filter and group different webservers together, and search the log history.

Opencart calculate shipping distance

UPDATE 30-09-2018

Some changes have been made by google, unfortunately I can’t support 200 stores to update the extension, so in order to keep using this extension you need to update to the latest version, or follow these steps to make it work again (this might be the quickest, also if you have a customized version):

Opencart 3.0 and up

step 1. open the file catalog\model\extension\shipping\zip_distance.php
step 2. find the getgoogleurl function, and replace it with:

 private function _googleUrl($params) {
        return array(
            'url' => 'https://maps.googleapis.com/maps/api/distancematrix/json',
            'query' => 'origins=' . $params['origins'] . '&destinations=' . $params['destinations'] . '&mode=driving&units=' . $params['units'] . '&sensor=false&key=' . $this->config->get('shipping_zip_distance_api_key')
        );
    }

The changes in this function are: replaced http url with https url, added api key to the query params

Save changes, and you are done!

 

Opencart 2.3.0.0, 2.3.0.1, 2.3.0.2:

step 1. open the file catalog\model\extension\shipping\zip_distance.php
step 2. find the getgoogleurl function, and replace it with:

/**
     * Build the google url with parameters
     * @param array $params
     * @return array
     */
    private function _googleUrl($params) {
        return array(
            'url' => 'https://maps.googleapis.com/maps/api/distancematrix/json',
            'query' => 'origins=' . $params['origins'] . '&destinations=' . $params['destinations'] . '&mode=driving&units=' . $params['units'] . '&sensor=false&key=' . $this->config->get('zip_distance_api_key')

        );
    }

The changes in this function are: replaced http url with https url, added api key to the query params

Save changes, and you are done!

Any Lower versions
The older versions don’t have the apikey yet in the backend, so you need to do it manually:

Find the getgoogleurl function and replace http with https and at the end of the query string add &key=YOURAPIKEY

I created an extension for opencart where you can calculate the distance based on the shippingaddress or the zipcode of the customer & your shop.

Caching repetitive requests

For a client I created a php script that gets certain user profile statistics from another website and returns it as JSON to the ajax request that our own website sends.
This was the easiest way, since saving it ourselves and keeping the data up to date with all the changes would be too much work.

In order to lower the loadtime & keep the other website from being swamped with requests (every profile load) I used memcached to cache the data for 12 hours.
Caching data with memcached is fast and simple.

If you don’t have memcached yet, you can install it on ubuntu with the following command:

sudo apt-get update
sudo apt-get install php5-memcached memcached

It should instantly start a memcached process, or you can start it manually:

/etc/init.d/memcached start

the config file will be in /etc/memcached.conf or /etc/sysconfig/memcached

Here you can change the port (default it runs on 11211)

Now whenever my php script gets a request for a profile, I check if we have the data cached in memory:

mc = new Memcached();
$mc->addServer("127.0.0.1", 11211);

$result = $mc->get($cachekey);

if ($result) {
   return $result;
}

the cachekey can be anything you wan’t wich will identify the data you are looking for (in my case I used the url encoded as the key)

If no result was found, I then get the result from the remote website, and save it in my memcached server for 12 hours

$mc->set($cachekey, $stats, 43200);

This way only the first request for every profile in 12 hours will be sending a request to a remote server, decreasing the average loadtime of the pages for the visitors.

Don´t forget we can also use memcached for caching results we get from database queries on our own database, to decrease loadtime and database cpu load.

Personally I prefer varnish for full page caching solutions (I will talk about that in another post), but for smaller stuff like a hand full of objects / results memcached is a very nice option.

 

Frans Boone Store Realtime UPS Rates & Pickup points

Frans Boone wanted to offer his clients to choose between normal ups shipping rates & free shipping to ups pickup points near the client.
In order to do this I created a custom shopify app for Frans Boone, which uses the different ups api’s to get the pickup points near the customer & normal shipping rates, and return it to the shopify checkout process, where the client can choose the desired option.

 

 

Magento isSaleable() false

If you are having problems with products not being saleable in magento, make sure you add the price attribute to the select, otherwise it will always return false!

$collection = Mage::getModel('catalog/product')->getCollection();
        $collection->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('url_path')
                ->addAttributeToSelect('status')
                ->addUrlRewrite();

 

 

Pushover push notifications for opencart

Get opencart push notifications on your phone!

This App uses the pushover app to send push messages to your Android or IOS device if a new order is received, or a customer is created.

To receive notifications you will need to purchase the Pushover App for Android (http://pushover.net/clients/android) or iOS (http://pushover.net/clients/ios)

Then download and install the opencart extension:

https://www.opencart.com/index.php?route=extension/extension/info&extension_id=14803&token=2a91840bddb0b8cefeb4a4317cac88f7