1. Go to this page and download the library: Download openbuildings/shipping library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
openbuildings / shipping example snippets
php
class Model_Product extends Jam_Model implements Sellable, Shippable {
public static function initialize(Jam_Meta $meta)
{
$meta
->associations(array(
'shipping' => Jam::association('belongsto', array('inverse_of' => 'products')),
))
->fields(array(
'id' => Jam::field('primary'),
'name' => Jam::field('string'),
'currency' => Jam::field('string'),
'price' => Jam::field('price'),
))
->validator('type', 'price', 'quantity', array(
'present' => TRUE
));
}
// Implement Sellable
public function price(Model_Purchase_Item $item)
{
return $this->price;
}
// Implement Sellable
public function currency()
{
return $this->currency;
}
// Implement Shippable
// Must return a ``Model_Shipping`` object holding all the data for the shipping
public function shipping()
{
return $this->shipping;
}
// Implement Shippable
// Must return a boolean whether or not the product ships can to that location
public function ships_to(Model_Location $location)
{
return $this->shipping ? $this->shipping->ships_to($location) : FALSE;
}
}
class Model_Purchase_Item_Product extends Kohana_Model_Purchase_Item_Product {
public static function initialize(Jam_Meta $meta)
{
parent::initialize($meta);
$meta
->behaviors(array(
'shippable_purchase_item' => Jam::behavior('shippable_purchase_item'),
));
}
}
class Model_Shipping extends Kohana_Model_Shipping {
public static function initialize(Jam_Meta $meta)
{
parent::initialize($meta);
$meta
->associations(array(
'products' => Jam::association('hasmany', [
'inverse_of' => 'shipping',
]),
));
}
}
php
class Model_Brand_Purchase extends Kohana_Model_Brand_Purchase {
public static function initialize(Jam_Meta $meta)
{
parent::initialize($meta);
$meta
->behaviors(array(
'shippable_brand_purchase' => Jam::behavior('shippable_brand_purchase'),
));
}
}
class Model_Purchase extends Kohana_Model_Purchase {
public static function initialize(Jam_Meta $meta)
{
parent::initialize($meta);
$meta
->behaviors(array(
'shippable_purchase' => Jam::behavior('shippable_purchase'),
));
}
}
php
$post = Jam::find('shipping_method', 'Post');
$europe = Jam::find('location', 'Europe');
$france = Jam::find('location', 'France');
$product->shipping = Jam::create('shipping', array(
'currency' => 'GBP',
'ships_from' => $france,
'groups' => array(
// Ships to all of Europe for 20 GBP
array('method' => $post, 'location' => $europe, 'price' => 20),
// Specifically for France - only 10 GBP
array('method' => $post, 'location' => $france, 'price' => 10),
)
));
php
$brand_purchase = Jam::find('brand_purchase', 1);
// If you want to set the informaction explicitly on which purchase_item what shipping_group to use
$brand_purchase->build('shipping', array(
'items' => array(
array(
'purchase_item' => $brand_purchase->items[0],
'shipping_group' => $brand_purchase->items[0]->reference->shipping()->groups[0],
),
)
));
// Or if you want ones selected automatically, based on a preffered shipping method and purchaser location
$post = Jam::find('shipping_method', 'Post');
$france = Jam::find('location', 'France');
$brand_purchase_shipping = $brand_purchase->build('shipping', array(
'location' => $france,
));
$brand_purchase_shipping->build_items_from($brand_purchase->items, $post);
php
$brand_purchase->update_items();
echo $brand_purchase->items_count('shipping'); // should return 1
php
$group_shipping_methods = $brand_purchase->group_shipping_methods()
foreach ($group_shipping_methods as $group)
{
foreach ($group->group_shipping_items() as $items)
{
// Get all the purchase items, shippable to this location by this method.
$items->purchase_items;
// Calculate the price of these items, provide a total price to remove ones that are discounted based on it.
$items->total_price();
// Delivery
$items->total_delivery_time();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.