Well so far I have a working version of the multivendor shipping. There are a still alot of things I need to get working.
Right now it gets aquote for each vendor for each module. It then adds the price of each method with the other methods, from different vendors. It then displays the quote.
This all hinges on each vendor using the exact same modules, and the modules returning the same methods.
here is my new class
<?php
/**
* vendor_shipping.php
*
* @version $Id$
* @copyright 2004
*/
require_once(DIR_WS_CLASSES . 'shipping.php');
define('USE_VENDOR_SHIPPING', 1); //configure in admin later
class vendorShipping extends shipping {
function quote($method = '', $module = '')
{
global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes, $cart, $origininfo;
if (USE_VENDOR_SHIPPING == 1) {
// vendor specific methodes
$sortvendor = $this->sortProductsByVendor($cart->get_products());
if (is_array($this->modules)) {
foreach($sortvendor as $vendor) {
$origininfo = array();
$shipping_quoted = '';
$shipping_weight = '';
$shipping_num_boxes = 1;
$shipping_weight = $vendor['weight'];
$origininfo['zipcode'] = $vendor['vendors_ship_zip'];
$origininfo['country'] = $vendor['vendors_ship_country'];
$origininfo['state'] = $vendor['vendors_ship_state'];
if (SHIPPING_BOX_WEIGHT >= $shipping_weight * SHIPPING_BOX_PADDING / 100) {
$shipping_weight = $shipping_weight + SHIPPING_BOX_WEIGHT;
} else {
$shipping_weight = $shipping_weight + ($shipping_weight * SHIPPING_BOX_PADDING / 100);
}
if ($shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes
$shipping_num_boxes = ceil($shipping_weight / SHIPPING_MAX_WEIGHT);
$shipping_weight = $shipping_weight / $shipping_num_boxes;
}
$include_quotes = array();
$include_quotes = $this->getValidVendorModules($vendor['vendors_id']);
$size = sizeof($include_quotes);
for ($i = 0; $i < $size; $i++) {
$quotes = $GLOBALS[$include_quotes[$i]]->quote('');
if (is_array($quotes)) {
$quotes_array[$vendor['vendors_id']][] = $quotes;
}
}
}
if (tep_not_null($method) && substr($module, 0, 6) == 'vendor') {
$temp_quotes = $this->processQuotes($quotes_array);
// $quotes_array = $quotes_array[$module][$method];
$key = array_search($module, $temp_quotes);
foreach ($temp_quotes as $key => $quote) {
if (array_search($module, $quote)) {
$index = $key;
foreach($quote['methods'] as $key3 => $value) {
if (array_search($method, $value)) {
$index2 = $key3;
}
}
}
}
$quotes_array[0]['methods'][0] = $temp_quotes[$index]['methods'][$index2];
return $quotes_array;
}
}
return $this->processQuotes($quotes_array);
} else {
return parent::quote($method, $module);
}
}
function sortProductsByVendor($products)
{
foreach ($products as $product) {
$sortvendor[$product['vendors_id']]['vendors_id'] = $product['vendors_id'];
$sortvendor[$product['vendors_id']][] = $product;
$sortvendor[$product['vendors_id']]['weight'] += $product['weight'];
}
foreach ($sortvendor as $vendor) {
$vendor_info_query = tep_db_query("Select * from vendors where vendors_id='" . $vendor['vendors_id'] . "'");
$vendor_info = tep_db_fetch_array($vendor_info_query);
// print_r($vendor_info);
$sortvendor[$vendor['vendors_id']] = array_merge($sortvendor[$vendor['vendors_id']], $vendor_info);
}
return $sortvendor;
}
function getValidVendorModules($vendorid)
{
// returns the modules that the vendor ship with
reset($this->modules);
while (list(, $value) = each($this->modules)) {
$class = substr($value, 0, strrpos($value, '.'));
if ($GLOBALS[$class]->enabled) {
$include_quotes[] = $class;
}
}
return $include_quotes;
}
function processQuotes($quotes)
{
$vendorlist = array_keys($quotes);
$numvendors = count($vendorlist);
for($i = 0;$i < $numvendors;$i++) {
$numquotes = count($quotes[$vendorlist[$i]]);
for($j = 0;$j < $numquotes;$j++) {
$finalquote[$j]['id'] = 'vendor' . $j;
$finalquote[$j]['module'] = $quotes[$vendorlist[$i]][$j]['module'];
for($k = 0;$k < count($quotes[$vendorlist[$i]][$j]['methods']);$k++) {
$finalquote[$j]['methods'][$k]['id'] = $quotes[$vendorlist[$i]][$j]['methods'][$k]['id'];
$finalquote[$j]['methods'][$k]['title'] = $quotes[$vendorlist[$i]][$j]['methods'][$k]['title'];
$finalquote[$j]['methods'][$k]['cost'] += $quotes[$vendorlist[$i]][$j]['methods'][$k]['cost'];
}
}
}
return $finalquote;
}
function _getCheapest($quotes)
{
for ($i = 0, $n = sizeof($quotes['methods']); $i < $n; $i++) {
if (isset($quotes['methods'][$i]['cost']) && tep_not_null($quotes['methods'][$i]['cost'])) {
$rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
'cost' => $quotes['methods'][$i]['cost']);
}
}
$cheapest = false;
for ($i = 0, $n = sizeof($rates); $i < $n; $i++) {
if (is_array($cheapest)) {
if ($rates[$i]['cost'] < $cheapest['cost']) {
$cheapest = $rates[$i];
print_r($cheapest);
echo '<br>';
}
} else {
$cheapest = $rates[$i];
}
}
return $cheapest;
}
}
you have to change a checkout_shipping.php
replace
Quote
// load all enabled shipping modules
require(DIR_WS_CLASSES . 'shipping.php');
$shipping_modules = new shipping;
with
Quote
// load all enabled shipping modules
//## vendor shipping
require(DIR_WS_CLASSES . 'vendor_shipping.php');
$shipping_modules = new vendorShipping;
I had to make a few changes, in other places but I can't remember where, I will have to do a diff at some point.
This all depends on a few things, like the vendor id is attached to the product in the shopping cart.
I put this code here mainly to help you guys get somewhere.