Friday, October 30, 2015

PrestaShop 1.6 Shopping Cart Sorting

No comments:
By default, PrestaShop orders the items in the shopping cart by the datetime they were added (order by date_add, id_product, id_product_attributes, from \classes\Cart.php). However, it drove me nuts that changing the quantity of the items in the cart changes their ps_cart_products::date_add, thereby causing the sorting order in the cart to change! Since the normal functioning of the code updates the basket with Ajax, it's usually not noticed by the visitor, but my code (for various reasons) actually performs a full update, which causes the items to jump visibly in the basket.

Although the default PrestaShop sorting is perfectly fine for the majority of users, I have seen people asking about how to change the sort order, and see the questions either unanswered or with answers that change the core code (something I try to avoid).

To "correct" this, I first created the following table:

CREATE TABLE `ps_cart_product_sorting` (
  `id_cart` int(10) unsigned NOT NULL,
  `id_product` int(10) unsigned NOT NULL,
  `original_date_add` datetime NOT NULL,
  UNIQUE KEY `ix_cart_product` (`id_cart`,`id_product`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I then overrode \classes\Cart.php (by creating \overrides\classes\Cart.php), function getSummaryDetails. I first call the base class to return the $summary_details array. I then add the original date add to each product, and finally sort the array by the original date add.

<?php

function summary_compare (array $one, array $two)
{
    return strcmp ($one['original_date_add'], $two['original_date_add']);
}

class Cart extends CartCore
{
    //----------------------------------------------------------------------------
    public function getSummaryDetails($id_lang = null, $refresh = false)
    {
        $summary_details = parent::getSummaryDetails($id_lang, $refresh);
        if (isset ($summary_details['products'])) {
            for ($jj = 0; $jj < count($summary_details['products']); $jj++) {
                $summary_details['products'][$jj]['original_date_add'] =
                    $this->getOriginalDateAdd(
                        $summary_details['products'][$jj]['id_product'],
                        $summary_details['products'][$jj]['date_add']);
            }

            //Rather than letting prestashop sort by last modified (which moves things
            //around in the cart when changing qty there), sort by the original add_date.
            //Note that prestashop modifies cart_products::date_add every time the qty
            // changes, setting it to currente time.
            uasort($summary_details['products'], 'summary_compare');
        }

        return $summary_details;
    }

    //----------------------------------------------------------------------------
    protected function getOriginalDateAdd($id_product, $date_add)
    {
        $original_date_add = Db::getInstance()->getValue("
            select
                original_date_add
            from
                ps_cart_product_sorting
            where
                id_cart = {$this->id}
                and id_product = $id_product");

        if (!$original_date_add) {
            $original_date_add = $date_add;
            Db::getInstance()->insert('ps_cart_product_sorting', array (
                'id_cart' => $this->id,
                'id_product' => $id_product,
                'original_date_add' => $original_date_add
            ));
        }
        return $original_date_add;
    }
}

Finally, I added the sorting function

<?php
function summary_compare (array $one, array $two)
{
    return strcmp ($one['original_date_add'], $two['original_date_add']);
}

Wednesday, September 16, 2015

NimbleText

No comments:
As a programmer, you always run into situations where you need to process raw text into usable data. If you're a regex genius, or have a lot of free time, you can always write code to handle this. Or you can quickly get results with NimbleText.

It has about a 5 minute learning curve, and for me, "it just works". Highly recommended.

Yet another jewel I first read about on Scott Hanselman's Ultimate Developer and Power Users Tool List for Windows.

Thursday, September 10, 2015

PrestaShop /img dir Backup to S3

No comments:

I have my PrestaShop code plus all my modifications saved in BitBucket and my database backups in S3 (I keep last 7 days, Monday from the last 5 weeks, monthly and yearly backups on S3). However, I didn't want to back up my PrestaShop /img dir in either of the above ways... I don't need img history, just an rsync of the /img folder to S3.


Searching for solutions led me to S3Tools and the like... open source or paid wrappers to bring S3 to the command line. I was more inclined to write something myself... I'm not real happy putting my S3 keys in programs like this.

Then I found AWS CLI from Amazon. It just works! I did a pip install, filled in my credentials, and 2 minutes later had a differential backup working from Linux to S3. Testing with changes, additions, and deletions all worked as expected.

Once tested, I added the below to my nightly cron backup script...

aws s3 sync /var/www/html/img s3://my_prestashop_bucket/img

Friday, August 28, 2015

New GIT Repository -> BitBucket -> Windows

No comments:
Just added my WordPress code to a Bitbucket Git repository. As always when I want to init a new project, I first head over to gitignore.io to quickly get a decent .gitignore file. If you use the service, and your starting from Linux, the tutorial video is worth watching before you start.

After setting up the .gitignore file, in Linux, the following will create your repository...

sudo git init
sudo git add .
sudo git commit -m 'initial commit'

After that, create a new empty repository in Bitbucket, select the "I have an existing project" link, and follow the instructions to your new repository on Linux added to the empty repository on Bitbucket.

Sidenote: when I switched from Mercurial to Git (Mercurial was fine for me... just wanted to get my feet wet with Git), my idea was to use GitHub. However, being in a startup, with a small team, I decided to keep some Euros in my bank account and use Bitbucket, which is free for up to 5 users. I used Bitbucket with Mercurial, so I was already familiar with it, and I'm very happy to stay there.

Next, since I develop on Windows (10), I needed to get the code locally. To do that, I use SourceTree, which makes cloning the repository on Windows a no-brainer.

Friday, August 21, 2015

Quickly Toggle MySQL Logging

No comments:
MySQL logging is a lifesaver when trying to understand applications at a low level. However, since I use it infrequently, I always have to consult the Google doc I have with the details on how to toggle the setting, and where to find the output.

For Windows, I've switched to a BAT file to speed up the process (which I call from SlickRun).

BEFORE using the script, you will want to have the following in your MySQL config file (in Laragon, use the right button menu -> mysql -> my.ini) in the [mysqld] section.

log-output=FILE
general_log_file = "C:/whatever/mylog.txt"
general_log      = OFF

@echo off
set /p state="Set MySQL Logging state ON or OFF: "
c:\xampp\mysql\bin\mysql -u "root" -e "SET GLOBAL general_log = '%state%';
%SystemRoot%\explorer.exe "C:\whatever"

A better alternative is to output to a table. In your MySQL config file (in Laragon, use the right button menu -> mysql -> my.ini) in the [mysqld] section, add

log-output=TABLE (see docs (MySQL 5.7))
general_log      = OFF

Then your script would be

@echo off
set /p state="Set MySQL Logging state ON or OFF: "
c:\xampp\mysql\bin\mysql -u "root" -e "SET GLOBAL general_log = '%state%';

You will find the data in the mysql.general_log table. What I do to make is easier to grok is to create a table with queries that I want to ignore (e.g. the standard PrestaShop code at the beginning of a page load).

create table mysql.general_log_ignores
(
argument_to_ignore mediumblob null
);

and then select only the interesting queries with


select 
       event_time, 
       convert(argument using utf8)
from 
    general_log gl 
    left join general_log_ignores gli on gl.argument = gli.argument_to_ignore
where
    gli.argument_to_ignore is null
order by 
    gl.event_time;

Wednesday, August 12, 2015

AWS S3, PHP, XAMPP, and cURL error 60

No comments:
Due to problems with VirtualBox and Vagrant on Windows 10, I've temporarily moved back to XAMPP for local development. While restoring the backed up databases to my local MySQL, I hit a cURL error 60.

The correct response to this problem was to download a CA Bundle and tell PHP to use it in the php.ini file, e.g.

[curl]
curl.cainfo="C:\xampp\php\extras\ssl\cacert.pem"

However, even after downloading a pem file from http://curl.haxx.se/docs/caextract.html, setting the above, and restarting apache, I was still getting the same error. Since I'm using a phar file for the AWS SDK, debugging the problem was difficult.

A post by GeoffGordon finally solved this for me... downloading a zip file of the pem, and then using the extracted version, fixed this for me. Thanks, Geoff!

--------------------

When using WampServer, I had the same problem, with a different cause. The php.ini file indicated by a php file served from my local server was different than the php.ini file being used by PHPStorm for debugging. I extracted the php.ini used by PHPStorm by debugging a file with the following code.

<?php
ob_start();
phpinfo();
$phpinfo = ob_get_contents();

Wednesday, July 29, 2015

Code Highlighting for Blogger

No comments:
I don't want to change my template. I don't want to pull in the code via JavaScript like gist embedding does. I just want an HTML snippet of formatted code compatible with blogger. Today I'm using hilite.me to format my code. Anyone using anything better?

PHPStorm + xdebug + ScotchBox 2.0

No comments:
ScotchBox 2.0, by design, does not have xdebug installed. They give the following install instructions:

sudo apt-get install php5-xdebug
sudo service apache2 restart

Which works as far as it goes, but even successfully using PHPStorm's "Web Server Debug Validation", and installing and using the Xdebug bookmarklet, I was not able to get the debugger working.

This post gave me the insight that I needed, so I added the following code to the end of my php.ini (sudo nano /etc/php5/apache2/php.ini).

[xdebug]
zend_extension="/usr/lib/php5/20131226/xdebug.so"
xdebug.remote_enable=on
xdebug.remote_connect_back=on
xdebug.remote_host=192.168.33.10

Restart apache (sudo service apache2 restart), run PHPStorm's "Web Server Debug Validation" again, and Bob's your uncle.

Thanks to Joao Paulo for making it simplier.

Saturday, July 11, 2015

mRemoteNG

No comments:
Vagrant boxes, linux servers, and SSH session terminals, oh my! To clean up the mess, I installed mRemoteNG, to put the PuTTY sessions in tabs. Still get to keep my custom settings... just have it all boxed up for neatness now. Recommended.

Note that there are a lot of alternatives, and I just don't have time to try them. A quick trip through Wikipedia has an article on programs, and I see a lot of people using SuperPuTTY.

I'll also have to check out KiTTY on some rainy day, but really I have no complaints with PuTTY at the moment. Should I care about KiTTY if I already have PuTTY working nice?

Wednesday, July 08, 2015

Enable the PrestaShop 1.6 Smarty Debug Console

No comments:
  1. In /config/defines.inc.php, changed _PS_MODE_DEV_ to true.
  2. In /config/smarty.config.inc.php, after the assignment of the $smarty variable, add the following code:
    if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_)
        $smarty->debugging = true;
  3. In the template (.tpl file) you want to see debug info, add the following line at the very end of the template
    {debug}
  4. MAKE SURE there is a return after {debug}
  5. In the back office / advanced parameters / performance, click the "Clear Cache" button at the top right.
  6. In the URL that you are testing, add the param SMARTY_DEBUG (e.g. http://ls/index.php?id_product=57&controller=product&SMARTY_DEBUG).
  7. Make sure that you are not getting a pop-up blocked by your browser. Once you allow the popup, you will get your console.
  8. If you are still having problems, set the "PS_SMARTY_CONSOLE" value in the configuration table to 1.

Note that San Binario wrote up an alternative method to get the debug console working which changes the core code, which I wanted to avoid. I didn't try their method... but maybe it works better for you.

Thursday, July 02, 2015

PHPStorm Autocomplete for PrestaShop Classes

No comments:
Hmmm, doing a find in files for "public static function isInt" to try to find the validation class (or optionally hitting ctrl+N and looking for the ValidateCore class) is not what I want... I want autocomplete for Validate::(I need the "isDecimal" version).

Julien Bourdeau has the solution, PhpStorm-PrestaShop-Autocomplete. Quick install and just works. Thanks, Julien!

Ah! It's "Validate::isFloat".

Wednesday, July 01, 2015

PrestaShop Admin Area Hook Error Reporting

No comments:
I needed to validate data, report any errors found, and stop the submission of the form while developing a module for the PrestaShop 1.6(.0.14) admin area. However, the documentation was a bit thin on how to do this, and I needed quite a bit of digging before I found what I needed.

I can only tell you 100% for sure that this works for the actionProductUpdate hook, but I imagine it's valid everywhere. Here's a quick snippet to show you the general idea....
 
public function hookActionProductUpdate($params)
{
  if (!Validate::isInt (Tools::getValue('pwr_start')))
  {
    $this->context->controller->errors[] = "Ya screwed up!";
  }
}

There are 3 main things you want to take note of:
  • Use the Validate class to validate your input data
  • Use Tools::getValue instead of directly pulling things from $_GET / $_POST. This does some light purification of the input data.
  • If you want to stop the submit of the form, add the error to the controller's error list. Unfortunately, this is only reported at the top of the form.

Sunday, May 31, 2015

Download a RackSpace Server Image

No comments:
I needed to download one of my Rackspace cloud backup images to my computer for use with VirtualBox. I assumed I would be able to simply download it, but to my surprise, that´s not possible. Rackspace suggested I use their API and their "getting started" tutorial, and upvote the request to add direct image download as a Rackspace feature, which I did.

NOTE: Be aware that, at least with a CentOS 7 Cloud Server, the image you download with the following method is ONLY compatible with XenServer, a Type 1 Hypervisor. It simply did not work with VirtualBox. I saw one report that you can load it with VirtualBox, but Rackspace themselves says it will not work and suggests this method instead.

I got my Rackspace cloud API token, and used curl from my Rackspace server to do the following:

  • Get export endpoint (which is based on your server region) and authentication token using these docs. The export endpoint is the "cloudImages" "publicURL", and the authentication token is the "access" "token" "id".
    curl -X POST https://auth.api.rackspacecloud.com/v2.0/tokens -d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"RACKSPACE_USER_NAME", "apiKey":"API_TOKEN" } } }' -H "Content-type: application/json" |python -m json.tool
  • Export your account and authentication token (NOT your API token).
    export account="RACKSPACE_USER_NAME"
    export token="
    AUTHENTICATION_TOKEN"
  • Combine your endpoint URL with the calls from this article to get your list of images.
    curl -s https://lon.images.api.rackspacecloud.com/v2/images -H "X-Auth-Token: $token" |python -m json.tool
  • Asynchronously export the image your interested in (check the "image" "name" field to find the one you want) by using the image "id" and the name of you Cloud Files container. If you don't have a Cloud Files container (I didn't), you have to sign up for it to get exported images.
    curl -s https://lon.images.api.rackspacecloud.com/v2/tasks -X POST -d '{"type": "export","input":{"image_uuid": "IMAGE_ID","receiving_swift_container": "YOUR_CLOUD_FILE_CONTAINER"}}' -H "Content-Type: application/json" -H "X-Auth-Token: $token" |python -m json.tool
  • You can now simply wait for your image to appear in your cloud file container, or you can actively check the status of you export job by using the id returned by the export command.
    curl -s https://lon.images.api.rackspacecloud.com/v2/tasks/EXPORT_JOB_ID -H "X-Auth-Token: $token" |python -m json.tool
  • If all goes well, you can pick up your image from the cloud file container. Mine took about 5 to 10 minutes to appear.

Friday, May 29, 2015

Adjustable Height Desks - cheaper version

No comments:

Ever since I started reading about it years ago, the idea of an adjustable height desk really intrigued me. Back when I wanted to be a writer, I remember reading about Hemingway using a standing desk, and I've since learned that luminaries such as Ben Franklin and Winston Churchill did the same.

I badly wanted to switch both myself and my staff to adjustable height desks, but could never convince myself to pay the cost. However, in 2012, Susana was able to buy motor driven adjustable height tables for everyone at 443 EUR each from http://www.schaefershop-industrie.es/. These tables have been great for sitting less, and also for breaking up the day.

Standing can be overused (standing for long periods of time can cause varicose veins), and I could not find any long term, detailed studies that back up claims that switching between sitting and standing is healthier and makes you more productive (the standard claims), but I do like having the option to switch between both, and I DO feel that it makes me healthier and more productive.

When I was on my flight back from Düsseldorf yesterday via Air Berlin, I saw an ad for Veridesk that I wish I had seen back before we bought our desks. If you want the benefits of a standing desk without as much cost, then Veridesk might be a useful alternative.

Hmm... looking at their site, maybe I should look into getting a standing mat....

Monday, May 18, 2015

PuTTY - Script Output in Different Color

No comments:
I was writing a shell script with rather verbose output, and was getting confused with which output was from which invocation of the command. I started clearing the screen between commands, but sometimes forgot. There's got to be a better way....

How about if I changed the color of the output? This article turned me onto the basic method, which had to be fine tuned due to my use of the zenburn color theme. Also, I found that, for some reasons, the color macros that work on the command line were not working in the scripts. Doesn't matter... I can now start and end scripts with commands to change color, and get the output in a different color....

So, a shell script (myscript.sh) like this...

#!/bin/bash
echo -e "\e[0;32m----------------------------------------------------"
echo "My Script Output"
echo -e "----------------------------------------------------\e[0m"

Will output the following (assuming you are using the zenburn color theme)....
[root@local ~] ~/myscript.sh
----------------------------------------------------
My Script Output
----------------------------------------------------
[root@local ~]

Friday, May 15, 2015

CentOS 7 Hostname Resetting on Reboot - SOLVED

No comments:

Problem

Setting the hostname was working without problems in CentOS 7 (including latest updates), but the hostname would revent back to the old hostname on every reboot. Nothing I read about worked, and I see lots of other people with the same problems (although RackSpace could not reproduce the problem on a newly spun up CentOS 7).

Solution

Assumes you're running as root. Otherwise, sudo the commands
  • Backup /etc/hosts, /etc/sysconfig/network, /etc/sysconfig/network-scripts/ifcfg-eth0 and /etc/cloud/cloud.cfg
  • execute "hostnamectl set-hostname yourdomain.com"
  • execute "hostnamectl set-hostname --static yourdomain.com"
  • Change old hostname to yourdomain.com in /etc/hosts
  • In /etc/sysconfig/network, add
    HOSTNAME=yourdomain.com
  • In /etc/sysconfig/network-scripts/ifcfg-eth0, add
    HOSTNAME=yourdomain.com
    DHCP_HOSTNAME=yourdomain.com
  • in /etc/cloud/cloud.cfg, set
    preserve_hostname=true
  • Reboot
  • hostname and hostname -f should now correctly give yourdomain.com

Wednesday, May 13, 2015

Windows PuTTY Tips

No comments:

  • Make a .reg file with your color scheme and font to easily apply it to new sessions (since the default dark blue folder names on black background is just a pain in the butt). Just add the name of the session and run the .reg file. I use the zenburn color theme with a larger font.
    I also change my LineCodePage to UTF-8, which is necessary on CentOS7 - ymmv.
    Here is a gist of my customized zenburn.
  • F6 gets you the tilde character in putty (useful for non-US keyboards), which is the path to your home directory.
  • nano ~/.bashrc opens a file where you can define aliases to make your work easier. Of course, this will only be available on the machine you are working on, but if you work on the machine a lot, it's well worth the time to make aliases. Much, much more can be added... search bashrc some day when you're bored
  • I like: alias dir="ls -CFhal"
  • Also, alias bashrc="nano ~/.bashrc && source ~/.bashrc && { echo -e 'Success--'; cat ~/.bashrc; } | mail -s 'bashrc backup' 'you@yourdomain.com'". This open bashrc for editing, puts any changes you make live, and emails you the script so you have a backup. Like, wow!
  • Add scripts to bashrc, which are available from the command line. To backup a file by sending it to an email address (always a good idea before you make any changes to files via the shell), add this to .bashrc
    bufile() {
            file_to_backup=$1
            { cat $file_to_backup; }  | mail -s "'$file_to_backup' Backup" 'you@yourdomain.com'
    }

    ...and then call it from the shell
    bufile index.php

Thursday, May 07, 2015

PrestaShop 1.5/1.6 and the Google Tag Manager

No comments:

Adding Page View Tracking

Most PrestaShop themes use one header.tpl for all pages on the site. This post assumes that is the case with your theme... if not, apply everything about header.tpl to all your headers.
  • Set up a GTM (Google Tag Manager) container, etc., and copy out the GTM snippet
  • open header.tpl
  • Look for the body tag
  • IMMEDIATELY after the end of the body tag, add...
     {literal}
    [your GTM snippet]
    {/literal}

    e.g.
  • NOTE FROM 2018: This article is rather old, and the newest GTM installation code from Google also talks about adding code right after the "head" tag, which could also be done in the header.tpl file. Thanks to Oliver Gerber for pointing that out.
  • Add a tracking tag to GTM
  • Preview and debug your GTM code.
  • Publish the GTM container

Adding Transaction (Conversion) Tracking

Adding page view tracking is super simple. Adding conversion tracking requires a data layer, which I added with a module.
IMPORTANT: For this code to fire, your payment method modules need to be calling PrestaShop after payment. E.g. here are the changes I needed to make to get this to happen with PayPal. As far as I can tell, this "problem" exists for both my module, and for the PrestaShop Google Analytics module.
  • Install the PrestaShop module "Data Layer Module" (instructions for installing a PrestaShop module can be found in the PrestaShop Documentation). This is simply a copy of the ganalytics module with lots of code removed and analytics JavaScript adapted to the GTM. It's a VERY basic module that any PHP coder can understand and expand on.
  • In GTM, add a new Google Analytics transaction tag... named something like "PrestaShop Conversion"
  • Add a firing rule (a.k.a. a Trigger) named e.g. "Order Confirmation" with values {{event}} equals prestashop_order_confirmation (prestashop_order_confirmation is an event that I trigger in the Data Layer Module) for the conversion tag.
  • Preview and debug your conversion tracking.
  • Publish the GTM container

Adding Site Speed Tracking

Speed of your website is fundamental to a good conversion rate. In addition, it effects your ranking in the serps. Monitoring your page speed is an important KPI.
By default, GTM and GA track your site speed, but using only a 5% sampling rate. That's perfect if your Amazon, but many sites need 100% sampling to get good data. Here's how to get this up.

  • In GTM, open your page view tracking tag
  • In the "Fields to Set" section, add a new field called "siteSpeedSampleRate"
  • Set the value to 100 (= 100%) 
  • Preview and debug your GTM code.
  • Publish the GTM container

Notes

  • Always push when using the data layer! See Simo Ahava's article on the subject. Actually, read all of Simo Ahava's articles... he's a great resource.
  • This code was written for a site which only has PayPal as a payment option. Theoretically, it should work for other gateways (it's not at all PayPal specific). I would be interested in hearing of your results if you use this with other payment gateways.

Friday, March 06, 2015

Spidering and Parsing

No comments:
I needed to pull English product reviews from an English language version of a site and put them in Excel files for translation to Dutch, and rather than doing things one by one, I decided to code the work, resulting in this quick note on spidering and parsing in PHP.

For my purposes, PHPCrawl "just worked". I'll post again if I find something better, but so far, no need to look.

Rather than regexing the page contents, I wanted to to query the HTML. There are an AMAZING number of options, and the first two I tried "just broke" (one was Simple HTML DOM, and the other I'm not sure), since the pages I'm trying to parse are rather complicated.
Simple HTML DOM also had the additional disadvantage of being dead slow.

I am now working with the DOMDocument class, based on the comments on this excellent stack overflow post. So far, so good.

Update: This article by Ersin Kandemir was helpful as is, but additionally sent me to the XPath Helper Chrome extension (by Adam Sadovsky), which was also a big help (hint: not only does it show you xpath commands, it also lets you test your own commands on the current page). Thanks guys!

Friday, June 05, 2009

Gmail imap connect and seen/unseen

1 comment:
Some quick notes on imapping gmail (notice the novalidate-cert part... might save you some headaches), marking messages as read, and also about formatting PHP code for blogger. NOTE: code below has error checking removed for clarity.

$mbox = imap_open ("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", "your@gmail.com", "your");

$numMessages = imap_num_msg($mbox);
for ($jj = $numMessages; $jj > 0; $jj--) { //always have to go backwards, since message ID is dynamic
$header = imap_header ($mbox, $jj);
if ('U' == $header->Unseen) { //This message is unread
$body = '';
$struct = imap_fetchstructure($mbox, $jj);
if (!empty ($struct->parts)) {
$numParts = count ($struct->parts);
for ($kk = 0; $kk < $numParts; $kk++) {
$part = $struct->parts[$kk];
if ('PLAIN' == $part->subtype) {
$body = imap_fetchbody($mbox, $jj, $kk + 1);
}
}
} else {
$body = imap_body ($mbox, $jj);
}
imap_setflag_full($mbox, $jj, "\\Seen"); // mark as read
}
}
imap_close($mbox);
Formatting: I still don't have a good method of formatting for blogger. For this post, I used "Convert Special Characters into HTML Entities" by Stanley Shilov (Thanks!), switching to "Edit HTML" in Blogger, putting Stanley's optput between "pre" open and close tags, and then switching back to "compose" mode to clean up the output. There's GOT to be a better way... if anyone knows of any, please yell.

Thursday, June 04, 2009

Permissions for Magento Upgrade

No comments:
Just updated my "live" and local servers to Magento ver. 1.3.2.1 via the admin area, "System" menu, "Magento Connect", "Magento Connect Manager". I needed to set permissions on the live server, and used Magento: Magento Connect Manager - Save Settings - Permissions and How to resolve the file permissions error in Magento Connect Manager?

I got a lot of permissions errors when setting the permissions on the command line, and also a couple (for index.php and .htaccess) when installing (will have to talk to NuBlue about how to update them next time). However, since the only change in the two files was stuff for the super-new compiled stuff, I feel that I can address that problem next time.

For my local server, I simply downloaded the latest version, deleted my "magento" DB, created a new empty one, and installed (checking back with Max Berndt's "Getting Started with Magento Ecommerce!" to make sure I didn't miss anything). To install French, I downloaded the French pack, downloading the "Full Package", and copying the two fr_FR folders to the appropriate folders in Magento.

Wednesday, June 03, 2009

Zend Studio for Eclipse autocompletion hinting

No comments:
I had problems getting auto-complete to work for my "product" member variable (of type Mage_Catalog_Model_Product) working in Zend Studio for Eclipse. I found the solution in this article by HanaDaddy. The below works for me... typing in $this->product-> now gives me auto complete.

/**
*
* @var Mage_Catalog_Model_Product
*/
var $product;

Debugging Magento: Vista, XAMPP, & Zend Studio for Eclipse

3 comments:
Here are some notes about what worked for me for being able to debug Magneto in Zend Studio for Eclipse using XAMPP on Vista.

For debugging with PDT & xDebug, use this excellent site. My php.ini for xDebug:
[XDebug]
zend_extension_ts = D:\xampp\php\ext\php_xdebug-2.1.0-5.2-vc6.dll
zend_debugger.allow_hosts=127.0.0.1/24, 192.168.20.107/108
xdebug.remote_enable=On
xdebug.remote_host=127.0.0.1/24, 192.168.20.107/108
xdebug.remote_port=8080
xdebug.remote_handler=dbgp

XAMPP
Download the Window's Installer version of XAMPP. I have all 4 programs (MySQL, Apache, FileZilla FTP Server, and Mercury SMTP server) all running as services that boot on startup. Make sure you have at least XAMPP version 1.7.1. Afterwards, follow the EXCELLENT Magento installation instructions by Max Berndt (thanks Max!).

ZEND DEBUGGER
I don't have full notes here, but I do remember that I had a lot of problems, and in the end used the zend debugger from the PDT even though I own a licensed version of Zend Studio for Eclipse. I downloaded org.zend.php.debug.debugger.win32.x86_5.2.15.v20081217.jar from http://downloads.zend.com/pdt/plugins/, and used 7zip to open the jar and extract ZendDebugger.dll from the resource/php5 directory, and copied it into C:\xampp\php\ext\. I then commented out all the lines from the [Zend], [XDebug], and [DEBUGGER] sections of the php.ini file. I then added the following 3 lines to the [DEBUGGER] section before rebooting Apache...
zend_extension_ts=C:\xampp\php\ext\ZendDebugger.dll
zend_debugger.allow_hosts=127.0.0.1/24, 192.168.20.25/39
zend_debugger.expose_remotely=always
I don't remember where I got the exact allow_hosts, but I do know that the default values that I had caused Apache to crash, and the above finally worked for me.
Here you might also want to refer to the article PHP Debug with Zend Debugger And Eclipse PDT Tutorial Part 1.

ZEND STUDIO FOR ECLIPSE
The following assumes that Magento can be reached from http://127.0.0.1/magento
- From the "Run" menu, select "Debug Configurations"
- On the left, right-click on "PHP Web Page" and select "New"
- Name: Magento
- Server Debugger: Zend Debugger
- PHP Server: Click on the "New" link
- Name: Magento Server, URL http://127.0.0.1 (do NOT use localhost - I don't have notes on this, but I'm sure that I had trouble accessing the admin area using local host, and switching to http://127.0.0.1 fixed the problem).
- Hit next, going to the Server Path Mapping, and "Add"
- Path on Server: the windows path to your magento dir, e.g. C:\Users\Ed\workspace\magento
- Path in Workspace: /magento
- Click Finish
- Now back in the "Debug Configurations" dialog, set file to magento/
- uncheck "Auto Generate (URL)", and set the path (first part of which is auto-generated) to /magento
- In the "common" tab, click "run" and "debug" for showing in the favorite menus.

Hopefully, the above will work for you...

Tuesday, June 02, 2009

Programmatically Importing Product with Images in Magento

6 comments:
Although written in 2009, this article is still consistently one of the best read articles on the site. Use it at your own risk. If someone knows for sure that it still works (or doesn't!), please let me know.
It took me quite a while to figure out how to import products into Magento including images. The long version is below, the final results are here for those that need a quick fix, with thanks to articles from Darryl Adie (to show me 95% of the solution) and tza79 (who showed me how to set the visibility, a.k.a. mediaAttribute). Start with the code from Darryl, and then add the following...
//This call is needed since the media gallery is null for a newly created product.
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery ($fullImagePath, array ('image'), false, false); 
$product->addImageToMediaGallery ($fullSmallImagePath, array ('small_image'), false, false); 
$product->addImageToMediaGallery ($fullThumbnailPath, array ('thumbnail'), false, false);
----------------------------
Long, boring version:

I understood quickly with debugging (and with the help of Branko Ajzele's article) that the media gallery used in magento/app/code/core/Mage/Catalog/Model/Product.php was the object that I wanted to use, but for a newly created product, product->getMediaGalleryImages() returns NULL.

Product attribute "media_gallery_images" is only set in getMediaGalleryImages, when $product->getMediaGallery is already set, but $product->getMediaGallery is null for a newly created product - this is the cause of getMediaGalleryImages returning null.

product->setMediaGallery doesn't exist - but then again, neither does getMediaGallery (even though it's called in $product->getMediaGalleryImages)! What's going on here?!? This led me to several articles, which led to people talking about PHP5's Overloading. OK, maybe I should have known this already, but it was a cool discovery. However, it didn't help me solve my problem.

Here's where I lost a lot of time... I decided my best bet was to debug the admin area, to see how they create the media gallery. I was able to get Zend Studio for Eclipse to debug the admin area... a task which I will document shortly.

With the debugger, I could see that it was the ProductController that was creating the media gallery while loading the admin part, but EXACTLY where it was creating it, I was not able to find before it was time to go to dinner yesterday. In any case, if you want to debug this yourself, I can give you a hint: start in the "newAction" routine in /magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

Rather than going back to debugging the admin area today, I decided to take a good look at what an empty media gallery looks like, by creating a product in the Magento admin area, and then loading it with...
$product->load(6);
$mediaGallery = $product->getMediaGallery();
print_r ($mediaGallery);
Hmmm, it's nothing more than an array of arrays. Why not make it myself using setMediaGallery? Bingo!

Friday, February 27, 2009

Displaying Google Search Params

No comments:
If I search for 'lensbase víüçñ' in google.es using internet explorer, and capture the 'q' parameter from $_SERVER["HTTP_REFERER"] in PHP, I get 'lensbase+v%C3%AD%C3%BC%C3%A7%C3%B1'. There may be an easy way to convert all the %xx back to something readable, but I did not find it (tried urldecode,  utf8_decode, iconv, and about 100 other things).

I found a urlRawDecode function that worked great on windows, but not on Linux.

I found JavaScript code here, but calling it was just another problem.

At the end, I store my data as the full gobblygook, and then "fix it" on the display. For display, I set my header to utf-8 using 
header('Content-type: text/html; charset=utf-8');
and call urldecode on the string. That fixes everything except the + symbol and, since it's just a quick and dirty check of queries from google, I just str_replace them with spaces.

Tuesday, February 10, 2009

ColorCop

No comments:

If you need a simply color picker utility, I'm very happy with ColorCop.

Wednesday, July 16, 2008

Download from remote CVS

No comments:
* Client machine (your current Linux user, eg: peter)
* Server machine (user where you want access, eg: mister_big)

* Create a keypair private/public key on client machine to avoid retype the password every time (on peter machine)

Go to .ssh folder and type:

ssh-keygen -t dsa

Select Emtpy pass-phrase.

2 files had been created. id_dsa (private key) and id_dsa.pub (public key)

* You need add the id_dsa.pub content into authorized_key2 file on server machine (server machine is the computer where you want to access, in this case mister_big)
* Copy id_dsa.pub into server machine. Eg: via FTP. Note: You need move id_dsa.pub to root folder to access file via FTP since .ssh folder is not listed on FTP clients.
* On mister_big machine, move id_dsa.pub into .ssh directory (.ssh folder is a directory placed on root)
* Merge the new key (WARNING: I saw a lot comands to make this but for me only works fine this option)

mv authorized_keys2 authorized_keys2.bak // It's a backup
cp id_dsa.pub authorized_keys2 // Overwrite current authorized_keys2
cat authorized_keys2.bak >> authorized_keys2 // Use a backup to merge files
chmod 600 authorized_keys2 // Restore permissions

Go again to client machine (peter machine) and type:

ssh-agent /bin/bash
ssh-add

And next, you can download code without type password anymore:

export CVS_RSH=ssh
cvs -d:ext:mister_big@yourdomain.com/cvs checkout

Tuesday, June 10, 2008

Enums

No comments:
For the Tourline email, I needed to parse an FM CVS Export file. First, I split up the fields with a "split" function...




//First, make sure any old files are cleaned up.
System.IO.File.Delete(m_strOutFile);

//Ask FM to generate the new file
Utils.Tools.RunFMScript("Shipments.fp5", "TourlineExport");
Utils.Tools.WaitForFMGenerateFile(m_strOutFile, 120);
System.IO.TextReader reader = null;
try
{
if (System.IO.File.Exists(m_strOutFile))
{
reader = System.IO.File.OpenText(m_strOutFile);
while (reader.Peek() > -1) {
string strLine = reader.ReadLine();
if (strLine.Length > 0 && '"' == strLine[0])
strLine = strLine.Substring(1);
if (strLine.Length > 0 && '"' == strLine[strLine.Length - 1])
strLine = strLine.Substring(0, strLine.Length - 1);
string[] splitter = { "\",\"" };
string[] shipmentLines = strLine.Split(splitter, StringSplitOptions.None);


To index into shipmentLines, I started doing the following...

const int k_shipmentID = 0;
const int k_invoiceID = 1;
const int k_title = 2;
const int k_firstNames = 3;
const int k_surname = 4;


...but quickly realized that it was hard to maintain (if the needed fields change, or the number of fields change - whatever). Therefore, I started using enums





enum FMFields
{
ShipmentID, InvoiceID, Title, FirstNames, Surname, Street, Street2,
Town, City, County, Country, PostalCode, StrUnfoInfo, InvoiceTotal, ScannedStatus,
DateScanned
};


Much nicer, and easier to maintain - and less typing to boot! Note that I use the .NET convention of capitalizing both the enum name and the names of the members.

Thursday, March 13, 2008

Position Preferences in AdWords

No comments:
While going over the settings from a German campaign, I stumbled upon the "position preferences" option. This allows you to specify to Google in which position or range of positions you want to appear in the paid search results. Great! I thought. Let's set up all the keywords to be in position 1-6, since going any lower is just a waste of time.

However, I found 2x problems. The first was that I found no way, besides going in one keyword at a time, to adjust this. Nothing in the web interface, and nothing in the AdWords Editor. So, before sending an email asking Susana to do this across all our campaigns, I started reading about people using it. Apparently, there are a lot of circumstances where it simply doesn't work at all, and Google even specifically says that this option is only a "suggestion", not a guarantee.

Well, since we always rank well anyway, I decided to skip the work.

Thursday, February 21, 2008

Products Not Showing - MySQL 5

No comments:
If you find a new JShop install where products are not showing, it could be a JShop 1.2/1.3 incompatibility with MySQL5 - products not showing up is the major symptom of using MySQL5. To fix this, read the thread http://forums.jshopecommerce.com/showthread.php?t=2711. A copy of the file that needs to be merged to make it work is kept at \\server2003\Permanent\Projects\Details\Amexoptics\jss versions\MySQL5_Patch

Monday, November 19, 2007

Missing data in XML (format FMPXMLRESULT)

No comments:
We usually have XML data with some value missing, like in this example:



<COL>
<DATA>Value1</DATA>
<DATA>Value2</DATA>
<DATA>Value3</DATA>
<DATA>Value4</DATA>
</COL>
<COL>
<DATA>240</DATA>
<DATA />
<DATA>6</DATA>
<DATA>6</DATA>
</COL>



In order that the XML reader overlooks the data missing at the second position in a consistent way, the following condition can be used (the following code is "conceptual", which means that it is not the real one that is currently running):


(...)

if (currentReader.NodeType == XmlNodeType.Text)
{
if (isRowToRetrieve)
{
rowInfo[columnIndex] = reader.Value;

if (columnIndex == ProdIDPos)
{
//Values capture
valuesArray[valuesIndex++] = currentReader.Value;
}
else if (columnIndex == QuantityPos)
{
//Quantities capture
quantitiesArray[quantitiesIndex++] = currentReader.Value;
}
else if{(...)}
else{(...)}
}
}

(...)

if ((lastElementName == "DATA") && (currentReader.Name == "DATA") && (lastNodeType != XmlNodeType.Text))
{
Log("Quantity for "+ valuesArray[quantitiesIndex] +" is missing");
}



Then the key item here is the consideration that, between a XML ElementName and EndElementName, a XML NodeType Text is expected

Wednesday, October 17, 2007

Localhost FTP problems

1 comment:
In the past, I had problems using IIS's FTP to work. Therefore, I used WarFTP, which was an easy setup and worked fine.

However, today, I just could not get FTPPuts to work. I was getting an Access Denied error, and had no clue what the problem was. After uninstalling and reinstalling, checking params in the server, in my Filezilla, and in my FTP code, I was still clueless.

OK, WarFTP out, IIS back in - and the same exact error! A "critical transfer error" 550, permissions. It was not IIS or WarFTP that was the problem, but the lack of write permissions for the FTP user in NTFS. Letting "everybody" write to my FTP directory fixed the problem.

Bye bye WarFTP - you've served me well - but I'll stay with IIS now till I have further problems ;->

Friday, October 12, 2007

Abandoned Carts on Credit Card Page

No comments:
On one of our client's web shops using SecPay's SECPage interface to check out (without 3D turned on - 3D = MasterCard's 'SecureCode', and Visa’s 'Verified by Visa'), we have 4.5% of users that never come back from SecPay (marked "New" in JShop), and 0.5% whose credit cards fail.

Note that "never coming back" might also indicate that they failed SecPay's pre-bank checks - SecPay does not indicate to the merchants if clients did not come back due to the pre-bank checks or because of abandoning the cart. However, it appears that the percentage that fails the pre-bank check is small compared to people who simply do not complete the order.

4.5% is actually good compared to the other web shops that I talked to. One reported 8% (SECPage with 3D turned on), another said 4-8% was "normal" (Secure Trading), and a 3rd report 10% (PayPal).

What to do when the users never come back? We send out an email after 2 hours, and then again after 10 days, with a link to help the user complete the order. In this way, we're able to get almost one third of users to complete their order. One shop (with few orders of much higher totals) follows up with phone calls to all the abandoned carts, and gets a very large percentage (well over 50%) to finish their order.

Thursday, August 02, 2007

FileMaker Auto-Login

No comments:
While working on automating our daily FileMaker -> SQLServer data dump, we needed a way to automatically log into FileMaker whenever it brought up the password dialog. When Felipe mentioned the problem this morning when we got our daily morning coffee at the bakery downstairs, I realized that it could be done with some old Win32 calls.

I borrowed and stole ideas from my Google searches (starting with EnumWindow C#), and used the trusty old Spy++ application (part of Visual Studio Tools, and something that I haven't fired up in years), to get 90% of the way there. When I was at 90% and trying to set the password text in the edit box, I found Using P/Invoke to Automate Database Signon, which would have saved me quite a bit of time if I had found it earlier, and got me over the last 10%.

Here is the basic code. You can recreate this by creating a WindowsApplication project in VisualStudio 2005 and adding a timer component which fires as often as you need. Be sure to set the timer to "enabled", and hook it up to the timer1_Tick function.


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


public delegate bool FMCallBack(int hwnd, int lParam);

namespace FMAutoLogin
{
public partial class Form1 : Form
{
private const int WM_SETTEXT = 0x000C; //TextBox
private const int BM_CLICK = 0x00F5; //Button
[DllImport("user32.Dll")]
public static extern int EnumWindows(FMCallBack x, int y);
[DllImport("User32.Dll")]
public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd,
uint Msg, int wParam, string lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);

//----------------------------------------------------------------------
public Form1()
{
InitializeComponent();
}

//----------------------------------------------------------------------
private void timer1_Tick(object sender, EventArgs e)
{
EnumWindows(new FMCallBack(Form1.EnumWindowCallBack), 0);
}

//----------------------------------------------------------------------
private static bool EnumWindowCallBack(int hwnd, int lParam)
{
StringBuilder sb = new StringBuilder(1024);
GetWindowText((int)hwnd, sb, sb.Capacity);

if (sb.ToString().Contains(".fp5") &&
sb.ToString().Contains("File \""))
{
SetForegroundWindow((IntPtr) hwnd);
IntPtr editBox = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Edit", "");
SendMessage(editBox, WM_SETTEXT, 0, "your_password");
IntPtr okButton = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "OK");
SendMessage(okButton, BM_CLICK, 0, "");
}
return true;
}
}
}

Thursday, April 26, 2007

Taking Shop Offline

No comments:
CT recently had to take the shop offline, and I added the following to /templates/includes/top.html (and top_checkout.html) to make sure that we could use the shop, but not customers...

if ($_SERVER['REMOTE_ADDR'] != "80.28.198.60")
doRedirect("http://www.visiondirect.co.uk/checkoutChanges.txt");
?>

After adding (or removing) this, you need to remove compiled templates.

NOTE: SecPay cannot arrive successfully at the orderSuccess page if you have this code running.
NOTE2: Of course, you additionally have to add the file indicated by "doRedirect".

Wednesday, February 21, 2007

Count sales from Tuesday

No comments:
Here is how I got a count of all Tuesday sales from GreenLight...
select count(*) as mycount, left(datetime, 8) as mydate from {orders} where dayofweek(datetime) = 3 and referURL like "GL%" group by mydate order by mydate desc

Tuesday, January 30, 2007

Daily GreenLight Sales

No comments:
When checking a drop in GreenLight sales, I used...

select left (datetime,8) as mydate, left (datetime,4), mid(datetime, 5, 2), mid(datetime, 7, 2), count(*) as mycount from [OrdersTable] where datetime >= "20070101000000" and referURL like "GL|%" group by mydate order by mydate