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
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It only takes a minute to sign up.
Sign up to join this community(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('®', '', 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');
}
}