Skip to content

Instantly share code, notes, and snippets.

@falehenrique
Created November 17, 2017 19:54
Show Gist options
  • Save falehenrique/b272eee754a4a80605959509ef0ae7e0 to your computer and use it in GitHub Desktop.
Save falehenrique/b272eee754a4a80605959509ef0ae7e0 to your computer and use it in GitHub Desktop.
MercadoEnvios
public function getDimensions(&$products)
{
$width = 0;
$height = 0;
$depth = 0;
$weight = 0;
foreach ($products as &$product) {
if ($product['weight']) {
if (self::$weightUnit == 'KGS') {
$product['weight2'] = $product['weight'] * 1000;
} elseif (self::$weightUnit == 'LBS') {
$product['weight2'] = $product['weight'] * 453.59237;
} else {
$product['weight2'] = 0;
}
} else {
$product['weight2'] = 0;
}
if (self::$dimensionUnit == 'CM') {
$product['width2'] = $product['width'];
$product['height2'] = $product['height'];
$product['depth2'] = $product['depth'];
} elseif (self::$dimensionUnit == 'IN') {
$product['width2'] = $product['width'] * 2.54;
$product['height2'] = $product['height'] * 2.54;
$product['depth2'] = $product['depth'] * 2.54;
} else {
$product['width2'] = 0;
$product['height2'] = 0;
$product['depth2'] = 0;
}
}
if (Configuration::get('shipping_calc_mode') == 'longer_side') {
foreach ($products as $p) {
if ($p['width2'] && $p['width2'] > $width) {
$width = $p['width2'];
}
if ($p['height2'] && $p['height2'] > $height) {
$height = $p['height2'];
}
if ($p['depth2'] && $p['depth2'] > $depth) {
$depth = $p['depth2'];
}
if ($p['weight2']) {
$weight += ($p['weight2'] * $p['quantity']);
} else {
$weight += $this->config['default_weight'];
}
}
} else {
foreach ($products as $p) {
$dimensions = array(0, 0, 0);
$dimensions[0] = $p['width2'] > 0.01 ? $p['width2'] : Configuration::get('default_width');
$dimensions[1] = $p['height2'] > 0.01 ? $p['height2'] : Configuration::get('default_height');
$dimensions[2] = $p['depth2'] > 0.01 ? $p['depth2'] : Configuration::get('default_depth');
sort($dimensions);
for ($i = 0; $i < $p['quantity']; ++$i) {
$width = max($width, $dimensions[1]);
$height = max($height, $dimensions[2]);
$depth += $dimensions[0];
$sort_dim = array( $width, $height, $depth );
sort($sort_dim);
$depth = $sort_dim[0];
$height = $sort_dim[1];
$width = $sort_dim[2];
}
$weight += ($p['weight2'] > 0.1 ? $p['weight2'] : Configuration::get('default_weight')) * $p['quantity'];
}
}
$config_shipment = MercadoPago::$countryOptions[Configuration::get('MERCADOPAGO_COUNTRY')];
$width = max($width, $config_shipment['MP_SHIPPING_MIN_W']);
//$width = min($width, $this->settings['MP_SHIPPING_MAX_W']);
$height = max($height, $config_shipment['MP_SHIPPING_MIN_H']);
//$height = min($height, $this->settings['MP_SHIPPING_MAX_H']);
$depth = max($depth, $config_shipment['MP_SHIPPING_MIN_D']);
//$depth = min($depth, $this->settings['MP_SHIPPING_MAX_D']);
$weight = max($weight, $config_shipment['MP_SHIPPING_MIN_WE']);
//$weight = min($weight, $this->settings['MP_SHIPPING_MAX_WE']);
return array(
'width' => (int)Tools::ps_round($width, 0),// > 0.01 ? $width : $this->config['default_width'], 0),
'height' => (int)Tools::ps_round($height, 0),// > 0.01 ? $height : $this->config['default_height'], 0),
'depth' => (int)Tools::ps_round($depth, 0),// > 0.01 ? $depth : $this->config['default_depth'], 0),
'weight' => (int)Tools::ps_round($weight, 0)// > 0.1 ? $weight : $this->config['default_weight'], 0),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment