class USPSShipment in Commerce USPS 8
Class that sets the shipment details needed for the USPS request.
@package Drupal\commerce_usps
Hierarchy
- class \Drupal\commerce_usps\USPSShipmentBase implements USPSShipmentInterface
- class \Drupal\commerce_usps\USPSShipment implements USPSShipmentInterface
Expanded class hierarchy of USPSShipment
1 file declares its use of USPSShipment
- USPSRateRequestTest.php in tests/
src/ Unit/ USPSRateRequestTest.php
1 string reference to 'USPSShipment'
1 service uses USPSShipment
File
- src/
USPSShipment.php, line 13
Namespace
Drupal\commerce_uspsView source
class USPSShipment extends USPSShipmentBase implements USPSShipmentInterface {
/**
* Returns an initialized rate package object.
*
* @return \USPS\RatePackage
* The rate package entity.
*/
public function buildPackage() {
parent::buildPackage();
$this
->setService();
$this
->setShipFrom();
$this
->setShipTo();
$this
->setWeight();
$this
->setContainer();
$this
->setDimensions();
$this
->setPackageSize();
$this
->setExtraOptions();
return $this->uspsPackage;
}
/**
* Sets the ship to for a given shipment.
*/
protected function setShipTo() {
/** @var \CommerceGuys\Addressing\Address $address */
$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());
// Due to API limitations, only accept the first 5 digits of the Zip Code.
$this->uspsPackage
->setZipDestination(substr($address
->getPostalCode(), 0, 5));
}
/**
* Sets the ship from for a given shipment.
*/
protected function setShipFrom() {
/** @var \CommerceGuys\Addressing\Address $address */
$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());
// Due to API limitations, only accept the first 5 digits of the Zip Code.
$this->uspsPackage
->setZipOrigination(substr($address
->getPostalCode(), 0, 5));
}
/**
* Sets the package size.
*/
protected function setPackageSize() {
$this->uspsPackage
->setSize(RatePackage::SIZE_REGULAR);
}
/**
* Sets the services for the shipment.
*/
protected function setService() {
$rate_class = RatePackage::SERVICE_ALL;
// Determine if the shipping method uses a special
// rate class.
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);
}
/**
* Sets the package container for the shipment.
*/
protected function setContainer() {
$this->uspsPackage
->setContainer(RatePackage::CONTAINER_VARIABLE);
}
/**
* Sets any extra options specific to the shipment like ship date etc.
*/
protected function setExtraOptions() {
$this->uspsPackage
->setField('Machinable', TRUE);
$this->uspsPackage
->setField('ShipDate', $this
->getProductionDate());
}
/**
* Returns the current date.
*
* @return string
* The current date, formatted.
*/
protected function getProductionDate() {
$date = date('Y-m-d', strtotime("now"));
return $date;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
USPSShipment:: |
public | function |
Returns an initialized rate package object. Overrides USPSShipmentBase:: |
|
USPSShipment:: |
protected | function | Returns the current date. | |
USPSShipment:: |
protected | function | Sets the package container for the shipment. | |
USPSShipment:: |
protected | function | Sets any extra options specific to the shipment like ship date etc. | |
USPSShipment:: |
protected | function | Sets the package size. | |
USPSShipment:: |
protected | function | Sets the services for the shipment. | |
USPSShipment:: |
protected | function | Sets the ship from for a given shipment. | |
USPSShipment:: |
protected | function | Sets the ship to for a given shipment. | |
USPSShipmentBase:: |
protected | property | The commerce shipment entity. | |
USPSShipmentBase:: |
protected | property | The shipping method configuration. | |
USPSShipmentBase:: |
protected | property | Event disptcher. | |
USPSShipmentBase:: |
protected | property | The USPS rate package entity. | |
USPSShipmentBase:: |
public | function |
Allow rate to be altered. Overrides USPSShipmentInterface:: |
|
USPSShipmentBase:: |
public | function |
Get the RatePackage object. Overrides USPSShipmentInterface:: |
|
USPSShipmentBase:: |
protected | function | Determine the package type for the shipment. | |
USPSShipmentBase:: |
public | function |
Store the shipping method configuration. Overrides USPSShipmentInterface:: |
|
USPSShipmentBase:: |
public | function | Sets the package dimensions. | |
USPSShipmentBase:: |
protected | function | Sets the package weight. | |
USPSShipmentBase:: |
public | function | USPSShipmentBase constructor. |