Search

Facebook and Twitter Take Over Customer Service?

Landing Pages Mystery Revealed

Amazing Non-Stock Images

How to Survive a Cataclysm Known as "Abandoned Shopping Cart?"

How to Create Winning Marketing Strategy in Six Steps

Integrating Magento and WordPress using the WordPress REST API

Reset Magento orders to start at #100000001

In categories: Magento, PHP published on 16.06.2015 by mladen

During our development today, we offer our clients a server on which they can try and test their future store so they can make a lot of changes, and even prepare for the launch. We call it a staging server.

Once we’re ready for the launch and store is fully developed, it is quite possible that our client has already entered all the products information and uploaded pictures of products. It would be a shame to make them do all this over again, and it’s way to complicated for us to move just bits and pieces. Instead, wouldn’t it be amazing if we could transfer complete database from staging server to live server and just delete all the test orders, test invoices, credit memos, shipments and quotes, as well as test and demo customers?

Now we can with the following code. Enjoy using it on your own project, but be careful, and don’t use it on live server more than once (just before the launch).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
 * Magento store reset script
 *
 * Removes all Orders, Invoices, Credit memos, Shipments, Quotes and Customers
 * Also resets Increment id of sales models back to 100000000
 *
 */
 
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
 
require_once "lib/Zend/Debug.php";
require_once ('app/Mage.php');
 
Mage::setIsDeveloperMode(true);
Mage::app();
Mage::register('isSecureArea', 1);
 
$config = array(
    'delete_order'      => true,
    'delete_invoice'    => true,
    'delete_creditmemo' => true,
    'delete_shippment'  => true,
    'delete_customers'  => true,
    'delete_quotes'     => true,
    'reset_ids'         => true
);
 
$orderCollection = Mage::getModel('sales/order')->getCollection();
 
foreach ($orderCollection as $order) {
    $order = Mage::getModel('sales/order')->load($order->getId());
 
    if (true === $config['delete_invoice']) {
        foreach ($order->getInvoiceCollection() as $invoice) {
            Mage::getModel('sales/order_invoice')->setId($invoice->getId())->delete();
            echo "Deleted invoice ID:{$invoice->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_creditmemo']) {
        foreach ($order->getCreditmemosCollection() as $creditmemo) {
            Mage::getModel('sales/order_creditmemo')->setId($creditmemo->getId())->delete();
            echo "Deleted creditmemo ID:{$creditmemo->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_shippment']) {
        foreach ($order->getShipmentsCollection() as $shippment) {
            Mage::getModel('sales/order_shipment')->setId($shippment->getId())->delete();
            echo "Deleted shippment ID:{$shippment->getId()}<br/>";
        }
    }
 
    if (true === $config['delete_order']) {
        $order->delete();
        echo "Deleted order ID:{$order->getId()}<br/>";
    }
}
 
if (true === $config['delete_customers']) {
    $customerCollection = Mage::getModel("customer/customer")->getCollection();
    foreach ($customerCollection as $customer) {
            Mage::getModel('customer/customer')->setId($customer->getId())->delete();
            echo "Deleted customer ID:{$customer->getId()}<br/>";
    }
}
 
if (true === $config['delete_quotes']) {
    $quoteCollection = Mage::getModel("sales/quote")->getCollection();
    foreach ($quoteCollection as $quote) {
            Mage::getModel('sales/quote')->setId($quote->getId())->delete();
            echo "Deleted quote ID:{$quote->getId()}<br/>";
    }
}
 
if (true === $config['reset_ids']) {
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    $write->query("UPDATE eav_entity_store SET increment_last_id = '100000000';");
}

Git sync/fetch remote branches

In categories: Git published on 17.10.2014 by Dejan Jacimovic

To synchronise all remote branches with local version in git, all you need to do is run following commands:

for remote in `git branch -r`; do git branch --track $remote; done
git fetch --all
git pull --all

Thanks Wookie88 for answer on stackoverflow and TL;DR version.

Automatically log in Magento administrator to WordPress administration panel

In categories: Magento, PHP, WordPress published on 06.10.2014 by mladen

When integrating Magento and WordPress to work together, it is useful to integrate login as well. This way Magento administrators can log in just in one place and be automatically logged in WordPress as well.

Probably the best way to check if administrator is logged in is in WordPress. To check if Magento administrator is logged in, we just have to use following snippet:

1
2
3
4
5
6
7
8
9
10
11
function is_magento_admin() {
    // Init Mage app
    $magePath = realpath(ABSPATH . '../app/Mage.php');
    require_once($magePath);
    umask(0);
    Mage::app();
 
    // Init session with adminhtml session name
    Mage::getSingleton('core/session', array('name'=>'adminhtml'));
    return Mage::getSingleton('admin/session')->isLoggedIn();
}

Then, we need to make WordPress log in the administrator. Here is the snippet for this functionality:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function login_if_magento_admin() {
    if (!is_magento_admin() || !is_admin()) {
        return;
    }
 
    wp_clear_auth_cookie();
    wp_set_auth_cookie(1);
 
    if (!is_user_logged_in()) {
        // Redirect and finish executing to prevent
        // WP core from messing with auth cookie
        header('location: ' . get_admin_url());
        die;
    }
}

Of course, we must call function login_if_magento_admin once we make sure user is trying to access admin. This can be done on admin_init hook or/and on login page.

Lastly, we must not forget to set Magento cookie path to “/”. Without this, Magento session cookie won’t be accessible to Magento session models from WordPress. This can be done in System -> Configuration -> Web -> Session Cookie Management.

Save the settings and you’re good to go. You can now enjoy using login only in Magento.

Prevent access for users which are not logged in WordPress

In categories: PHP, WordPress published on 22.09.2014 by Dejan Jacimovic

We all have our small and big WordPress projects. Sometimes we want to allow our clients to see what has been done so far, but we don’t want anybody else to see it. The easiest way to define this is: prevent access for users which are not logged in WordPress administration.

Sure – you can use .htaccess for content protection, but there is a much more simple way.

Since our clients already have access to the website administration, we can just allow them to log in, and then view the website as it currently is. This way we will prevent all the users which we don’t want to see our website, and those are the ones with no access data to administration.

Just copy and paste following lines to functions.php file, and you will have your redirect set in no time.

1
2
3
4
5
6
7
8
function prevent_unauthorized_access() {
    if(!is_user_logged_in()) {
        header('location: ' . wp_login_url());
        die;
    }
}
 
add_action('wp_head', 'prevent_unauthorized_access');

Remove Magento pricing for options in dropdown on both simple and configurable products

In categories: JavaScript, Magento, PHP published on 22.09.2014 by mladen

Sometimes we and/or our customers want to remove price changes from product options and have them display only on change on our price label in Magento.

For configurable products, prices are added to label from js module Product.Config. To remove them, we have to rewrite module’s function getOptionLabel. We need a new javascript file, inside js folder, with following code:

1
2
3
Product.Config.prototype.getOptionLabel = function(option, price) {
    return option.label;
};

Of course, to make it work, we need to add newly created script to single product view page. This can be done by adding following line to the catalog.xml, after varien/configurable.js has been added.

<action method="addJs"><script>path/to/our/file.js</script></action>

To remove the price from single product’s custom options, we need to (due to lack of better solution), rewrite core block responsible for rendering product options input elements.

After rewriting Mage_Catalog_Block_Product_View_Options_Type_Select block, we have to change the way options are rendered. Around line 65, there is a loop similar to the following code:

1
2
3
4
5
6
7
8
9
10
11
foreach ($_option->getValues() as $_value) {
    $priceStr = $this->_formatPrice(array(
        'is_percent'    => ($_value->getPriceType() == 'percent'),
        'pricing_value' => $_value->getPrice(($_value->getPriceType() == 'percent'))
    ), false);
    $select->addOption(
        $_value->getOptionTypeId(),
        $_value->getTitle() . ' ' . $priceStr . '',
        array('price' => $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false))
    );
}

In our own file which we used for rewriting Magento’s default code, we should use the following version:

1
2
3
4
5
6
7
foreach ($_option->getValues() as $_value) {
    $select->addOption(
        $_value->getOptionTypeId(),
        $_value->getTitle(),
        array('price' => $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false))
    );
}

With these simple changes we will be able to remove ugly formatted pricing in dropdown in our Magento’s single product view pages.

Git ignore local changes and pull from remote

In categories: Git published on 15.08.2014 by Dejan Jacimovic

Sometimes you just want to ignore changes you made locally and have the same situation you have on your git remote server.

It is a simple thing to do, but don’t forget you will delete all the changes you have made locally before running this command.

Continue reading…

Show post number instead of a date in WordPress

In categories: WordPress published on 11.08.2014 by Dejan Jacimovic

It is not usual to show the number of the published post, however, it is not necessarily a bad thing. Sometimes, we just don’t publish on daily or weekly basis and this “published” date might make our posts look outdated. Therefore, we can build a simple procedure which will tell us how many of this post type has been published before the current article and use it to present our visitors with post number on our blog.

To use procedure shown in snippet below, just call the shortcode post_order.

Continue reading…

Separate first and last name in Excel

In categories: Excel published on 02.08.2014 by Dejan Jacimovic

Assuming that we have column A containing first and last name with a potential middle name, we can assume that taking first name can be conducted by getting the value from beginning until first space.

Continue reading…

Page 1 of 2112345...1020...Last