You are here

public function Package::delete in Ubercart 8.4

Deletes this package.

File

shipping/uc_fulfillment/src/Package.php, line 601

Class

Package
Defines the Package class.

Namespace

Drupal\uc_fulfillment

Code

public function delete() {
  \Drupal::database()
    ->delete('uc_packages')
    ->condition('package_id', $this->package_id)
    ->execute();
  \Drupal::database()
    ->delete('uc_packaged_products')
    ->condition('package_id', $this->package_id)
    ->execute();
  if ($this->label_image) {
    file_usage_delete($this->label_image, 'uc_fulfillment', 'package', $this->package_id);
    file_delete($this->label_image);
  }

  // Remove the package_id from the OrderProduct.
  foreach ($this->products as $id => $product) {
    $order_product = OrderProduct::load($id);
    if (NULL != $order_product) {

      // There may be multiple packages per OrderProduct.
      $package_array = $order_product->data->package_id;
      unset($package_array[$this->package_id]);
      $order_product->data->package_id = $package_array;
      $order_product
        ->save();
    }
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('Package @id has been deleted.', [
    '@id' => $this->package_id,
  ]));
}