USPSShipment.php in Commerce USPS 8
File
src/USPSShipment.php
View source
<?php
namespace Drupal\commerce_usps;
use USPS\Address;
use USPS\RatePackage;
class USPSShipment extends USPSShipmentBase implements USPSShipmentInterface {
public function buildPackage() {
parent::buildPackage();
$this
->setService();
$this
->setShipFrom();
$this
->setShipTo();
$this
->setWeight();
$this
->setContainer();
$this
->setDimensions();
$this
->setPackageSize();
$this
->setExtraOptions();
return $this->uspsPackage;
}
protected function setShipTo() {
$address = $this->commerceShipment
->getShippingProfile()
->get('address')
->first();
$to_address = new Address();
$to_address
->setAddress($address
->getAddressLine1());
$to_address
->setApt($address
->getAddressLine2());
$to_address
->setCity($address
->getLocality());
$to_address
->setState($address
->getAdministrativeArea());
$this->uspsPackage
->setZipDestination(substr($address
->getPostalCode(), 0, 5));
}
protected function setShipFrom() {
$address = $this->commerceShipment
->getOrder()
->getStore()
->getAddress();
$from_address = new Address();
$from_address
->setAddress($address
->getAddressLine1());
$from_address
->setCity($address
->getLocality());
$from_address
->setState($address
->getAdministrativeArea());
$this->uspsPackage
->setZipOrigination(substr($address
->getPostalCode(), 0, 5));
}
protected function setPackageSize() {
$this->uspsPackage
->setSize(RatePackage::SIZE_REGULAR);
}
protected function setService() {
$rate_class = RatePackage::SERVICE_ALL;
if (!empty($this->configuration['rate_options']['rate_class'])) {
switch ($this->configuration['rate_options']['rate_class']) {
case 'online':
case 'commercial':
$rate_class = RatePackage::SERVICE_ONLINE;
break;
case 'commercial_plus':
$rate_class = 'PLUS';
break;
}
}
$this->uspsPackage
->setService($rate_class);
}
protected function setContainer() {
$this->uspsPackage
->setContainer(RatePackage::CONTAINER_VARIABLE);
}
protected function setExtraOptions() {
$this->uspsPackage
->setField('Machinable', TRUE);
$this->uspsPackage
->setField('ShipDate', $this
->getProductionDate());
}
protected function getProductionDate() {
$date = date('Y-m-d', strtotime("now"));
return $date;
}
}
Classes
Name |
Description |
USPSShipment |
Class that sets the shipment details needed for the USPS request. |