0

Does anybody have a sample shipping plugin for Store 2? The only one I have is for 1.5 and the documentation isn't very helpful.

Thanks in advance,

Cole

1 Answer 1

1

(I'm with Vector, the people behind Expresso Store, but this is just something I made quickly and shouldn't be considered "official advice")

Here's a sample to get you started:

<?php

/*
** The below is pseudo-code just meant to show how to write a
** shipping extension for Expresso Store 2, 3, or 4. It shouldn't
** be used as-is in a production environment.
*/

if (defined('PATH_THIRD')) {
    require PATH_THIRD.'store/autoload.php';
}

use Store\Exception\ShippingException;
use Store\Model\Order;
use Store\Model\OrderShippingMethod;

class Store_sample_shipping_ext
{

    public $hooks = array('store_order_shipping_methods');
    public $settings = array();

    public function __construct($settings = array())
    {
        $this->settings = $settings;
    }

    public function store_order_shipping_methods(Order $order, array $methods)
    {

        if (ee()->extensions->last_call !== false) {
            $methods = ee()->extensions->last_call;
        }

        // If you have logic that decides if your method should be used or not,
        // you can always just return the existing $methods

        if (!$this->should_be_active()) {
            return $methods;
        }

        // You'd do logic here to calculate your actual shipping methods and costs.
        // Once you've determined that, add them to the $methods array. You can add
        // more than one. The format for each one is this:

        $option = new OrderShippingMethod;
        $option->id = __CLASS__.':'.'some identifier, if you add more than one';
        $option->name = $this->clean_str('A friendly name');
        $option->class = __CLASS__;
        $option->amount = '12.34';

        $methods[$option->id] = $option;

        // If you want to sort, for example by cheapest first
        uasort($methods, function($a, $b) {
            return $a->amount - $b->amount;
        });

        return $methods;
    }

    protected function clean_str($str)
    {
        return strip_tags(str_replace('&reg;', '', html_entity_decode($str)));
    }

    public function activate_extension()
    {
        foreach ($this->hooks as $hook) {
             $data = array( 'class'     =>  __CLASS__,
                            'method'    =>  $hook,
                            'hook'      =>  $hook,
                            'settings'  =>  serialize($this->settings),
                            'priority'  =>  10,
                            'version'   =>  $this->version,
                            'enabled'   =>  'y'
                );

            // insert in database
            ee()->db->insert('exp_extensions', $data);
        }
    }

    public function disable_extension()
    {
        ee()->db->where('class', __CLASS__);
        ee()->db->delete('exp_extensions');
    }

}
2
  • Many thanks Matt - is there a way to automatically apply a custom shipping method in the checkout and cart calculations or does it have to be applied? Managed to work way round it with a shipping method hidden field in the template/checkout form but be good to have it applied without needing to specify. Thanks again for the help! May 22, 2018 at 9:18
  • @ColeHenley You can potentially make that the only option, but if your hidden field is working for you might just be worth sticking with it!
    – Matt
    May 23, 2018 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.