You are here

uc_usps.module in Ubercart 8.4

United States Postal Service (USPS) shipping quote module.

File

shipping/uc_usps/uc_usps.module
View source
<?php

/**
 * @file
 * United States Postal Service (USPS) shipping quote module.
 */
use Drupal\Core\Url;
use Drupal\uc_usps\USPSUtilities;

/**
 * Implements hook_theme().
 */
function uc_usps_theme() {
  return [
    'uc_usps_option_label' => [
      'variables' => [
        'service' => NULL,
        'packages' => NULL,
      ],
      'file' => 'uc_usps.theme.inc',
      'function' => 'theme_uc_usps_option_label',
    ],
  ];
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 *
 * Adds package type to products.
 *
 * @see uc_product_form()
 */
function uc_usps_form_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  if (uc_product_is_product($node
    ->bundle())) {
    $form['shipping']['usps'] = [
      '#type' => 'details',
      '#title' => t('USPS product description'),
      '#tree' => TRUE,
    ];
    $form['shipping']['usps']['container'] = [
      '#type' => 'select',
      '#title' => t('Package type'),
      '#options' => USPSUtilities::packageTypes(),
      '#default_value' => isset($node->usps['container']) ? $node->usps['container'] : 'VARIABLE',
    ];
  }
}

/**
 * Implements hook_node_insert().
 */
function uc_usps_node_insert($node) {
  uc_usps_node_update($node);
}

/**
 * Implements hook_node_update().
 */
function uc_usps_node_update($node) {
  if (uc_product_is_product($node)) {
    if (isset($node->usps)) {
      $usps_values = $node->usps;
      $connection = \Drupal::database();
      if (!$node
        ->isNewRevision()) {
        $connection
          ->delete('uc_usps_products')
          ->condition('vid', $node
          ->getRevisionId())
          ->execute();
      }
      $connection
        ->insert('uc_usps_products')
        ->fields([
        'vid' => $node
          ->getRevisionId(),
        'nid' => $node
          ->id(),
        'container' => $usps_values['container'],
      ])
        ->execute();
    }
  }
}

/**
 * Implements hook_node_load().
 */
function uc_usps_node_load($nodes) {
  $nids = [];
  foreach ($nodes as $node) {
    if (uc_product_is_product($node)) {
      $nids[] = $node
        ->id();
    }
  }
  if (empty($nids)) {
    return;
  }
  $vids = [];
  $connection = \Drupal::database();
  $usps_config = \Drupal::config('uc_usps.settings');
  $shipping_type = $usps_config
    ->get('uc_store_shipping_type');
  $shipping_types = $connection
    ->query("SELECT id, shipping_type FROM {uc_quote_shipping_types} WHERE id_type = :type AND id IN (:ids[])", [
    ':type' => 'product',
    ':ids[]' => $nids,
  ])
    ->fetchAllKeyed();
  foreach ($nids as $nid) {
    if (isset($shipping_types[$nid])) {
      $nodes[$nid]->shipping_type = $shipping_types[$nid];
    }
    else {
      $nodes[$nid]->shipping_type = $shipping_type;
    }
    if ($nodes[$nid]->shipping_type == 'small_package') {
      $vids[$nid] = $nodes[$nid]
        ->getRevisionId();
    }
  }
  if ($vids) {
    $result = $connection
      ->query("SELECT * FROM {uc_usps_products} WHERE vid IN (:vids[])", [
      ':vids[]' => $vids,
    ], [
      'fetch' => PDO::FETCH_ASSOC,
    ]);
    foreach ($result as $usps) {
      $nodes[$usps['nid']]->usps = $usps;
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function uc_usps_node_delete($node) {
  $connection = \Drupal::database();
  $connection
    ->delete('uc_usps_products')
    ->condition('nid', $node
    ->id())
    ->execute();
}

/**
 * Implements hook_node_revision_delete().
 */
function uc_usps_node_revision_delete($node) {
  $connection = \Drupal::database();
  $connection
    ->delete('uc_usps_products')
    ->condition('vid', $node
    ->getRevisionId())
    ->execute();
}

/**
 * Implements hook_uc_shipping_type().
 */
function uc_usps_uc_shipping_type() {
  $quote_config = \Drupal::config('uc_quote.settings');
  $weight = $quote_config
    ->get('type_weight', [
    'envelope' => -1,
    'small_package' => 0,
  ]);
  $types = [
    'envelope' => [
      'id' => 'envelope',
      'title' => t('Envelope'),
      'weight' => isset($weight['envelope']) ? $weight['envelope'] : -1,
    ],
    'small_package' => [
      'id' => 'small_package',
      'title' => t('Small package'),
      'weight' => isset($weight['small_package']) ? $weight['small_package'] : 0,
    ],
  ];
  return $types;
}

/**
 * Implements hook_uc_shipping_method().
 */
function uc_usps_uc_shipping_method() {
  $operations = [
    'configure' => [
      'title' => t('configure'),
      'url' => Url::fromRoute('uc_usps.settings')
        ->toString(),
    ],
  ];
  $methods = [
    'usps_env' => [
      'id' => 'usps_env',
      'module' => 'uc_usps',
      'title' => t('U.S. Postal Service (Envelope)'),
      'operations' => $operations,
      'quote' => [
        'type' => 'envelope',
        'callback' => 'uc_usps_quote',
        'accessorials' => USPSUtilities::envelopeServices(),
      ],
    ],
    'usps' => [
      'id' => 'usps',
      'module' => 'uc_usps',
      'title' => t('U.S. Postal Service (Parcel)'),
      'operations' => $operations,
      'quote' => [
        'type' => 'small_package',
        'callback' => 'uc_usps_quote',
        'accessorials' => USPSUtilities::services(),
      ],
    ],
    'usps_intl_env' => [
      'id' => 'usps_intl_env',
      'module' => 'uc_usps',
      'title' => t('U.S. Postal Service (Intl., Envelope)'),
      'operations' => $operations,
      'quote' => [
        'type' => 'envelope',
        'callback' => 'uc_usps_quote',
        'accessorials' => USPSUtilities::internationalEnvelopeServices(),
      ],
      'weight' => 1,
    ],
    'usps_intl' => [
      'id' => 'usps_intl',
      'module' => 'uc_usps',
      'title' => t('U.S. Postal Service (Intl., Parcel)'),
      'operations' => $operations,
      'quote' => [
        'type' => 'small_package',
        'callback' => 'uc_usps_quote',
        'accessorials' => USPSUtilities::internationalServices(),
      ],
      'weight' => 1,
    ],
  ];
  return $methods;
}

Functions

Namesort descending Description
uc_usps_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter() for node_form().
uc_usps_node_delete Implements hook_node_delete().
uc_usps_node_insert Implements hook_node_insert().
uc_usps_node_load Implements hook_node_load().
uc_usps_node_revision_delete Implements hook_node_revision_delete().
uc_usps_node_update Implements hook_node_update().
uc_usps_theme Implements hook_theme().
uc_usps_uc_shipping_method Implements hook_uc_shipping_method().
uc_usps_uc_shipping_type Implements hook_uc_shipping_type().