Custom pricing for product

Sometimes you need to modify pricing based on different factors - with pw hooks you can do pretty much anything.

There are million reasons to customizer pricing. Some of them are pretty strange and others seems to be very valid. Offering customizing options through admin UI and settings is neverending story. That is the reason why in Padloper it's possible only through simple autoload module - that also means that pretty much everything is possible: Selling products with half the price during night time? If you have that other product in cart, then the next one will be 50% cheaper? Use some base pricing, unless overwritten per product basis?

Here is example module, that doubles all the prices (save the code as CustomPrices.module into your /site/modules/ folder):

<?php
class CustomPrices extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'Custom Prices',
            'version' => 1,
            'summary' => 'Example module for modifying prices in Padloper.',
            'singular' => true,
            'autoload' => true,
            'icon' => 'money',
            );
    }

    public function init() {

        /*
         * Let's hook into PadCart::getProductPrice() method. If you open PadCart.module, you'll see
         * that it is very simple method and pretty much it's only there to allow hooking.
         *
         * In this example we'll just double the prices, but you can do whatever you want,
         * like calculate price based on other values in the page etc.
         *
         */
        $this->addHookAfter('PadCart::getProductPrice', $this, 'customizePrice');

    }

    /**
     * This is the method that hook run. Since in Padloper we can have any field as pricefield,
     * we need to find out which is the pricefield.
     */
    public function customizePrice($event) {

        // This way we can access the arguments that are send to hooked method
        $product = $event->arguments('product');

        // $event->object returns the object we hooked in, in this case PadCart object.
        $pricefield = $event->object->pricefield;

        $price = (float) $product->$pricefield;
        $doublePrice = $price * 2;

        // The modified return value is set here
        $event->return = $doublePrice;
    }
}

The fun doesn't have to stop there. You have all the ProcessWire API at your use when deciding how to modify the price. You get the current cart with $this->modules->get("PadCart")->getCart() and can do magic based on the products in the cart. Or need to look the price from parent page of the current product? Just access it from $product->parent->$pricefield at the above example. Possibilities are pretty much endless.

Padloper newsletter

To get occasional updates about new features and discounts