/** * {@inheritdoc} */ public function getQuotes(OrderInterface $order) { $rate = $this->configuration['base_rate']; $field = $this->configuration['field']; foreach ($order->products as $product) { if (isset($product->nid->entity->{$field}->value)) { $product_rate = $product->nid->entity->{$field}->value * $product->qty->value; } else { $product_rate = $this->configuration['product_rate'] * $product->qty->value; } $rate += $product_rate * $product->weight->value * uc_weight_conversion($product->weight->units); } return [$rate]; }
/** * {@inheritdoc} */ public function render(ResultRow $values) { $oid = $values->{$this->aliases['order_id']}; $order = Order::load($oid); $total = 0; foreach ($order->products as $product) { $unit_conversion = uc_weight_conversion($product->weight_units, $this->options['weight_units']); $total += $product->qty * $product->weight * $unit_conversion; } $this->field_alias = 'order_weight'; $values->{$this->field_alias} = $total; if ($this->options['format'] == 'numeric') { return parent::render($values); } if ($this->options['format'] == 'uc_weight') { return uc_weight_format($values->{$this->field_alias}, $this->options['weight_units']); } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, Request $request = NULL) { $checked_pkgs = $request->query->has('pkgs') ? (array) $request->query->get('pkgs') : array(); $form['#tree'] = TRUE; $form['#attached']['library'][] = 'uc_fulfillment/uc_fulfillment.scripts'; $units = \Drupal::config('uc_store.settings')->get('weight.units'); $result = db_query('SELECT * FROM {uc_packages} WHERE order_id = :id AND sid IS NULL', [':id' => $uc_order->id()]); $header = array(array('data' => '', 'class' => array('select-all')), 'package' => $this->t('Package'), 'product' => $this->t('Products'), 'weight' => $this->t('Weight')); $packages_by_type = array(); foreach ($result as $package) { $products = array(); $weight = 0; $result2 = db_query('SELECT pp.order_product_id, pp.qty, pp.qty * op.weight__value AS weight, op.weight__units, op.title, op.model FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id', [':id' => $package->package_id]); foreach ($result2 as $product) { $units_conversion = uc_weight_conversion($product->weight__units, $units); $weight += $product->weight * $units_conversion; $products[$product->order_product_id] = $product; } $package->weight = $weight; $package->products = $products; $packages_by_type[$package->shipping_type][$package->package_id] = $package; } // Find FulfillmentMethod plugins. $methods = FulfillmentMethod::loadMultiple(); uasort($methods, 'Drupal\\uc_fulfillment\\Entity\\FulfillmentMethod::sort'); foreach ($methods as $method) { // Available fulfillment methods indexed by package type. $shipping_methods_by_type[$method->getPackageType()][] = $method; } $pkgs_exist = FALSE; $option_methods = array(); $shipping_types = uc_quote_get_shipping_types(); foreach ($packages_by_type as $shipping_type => $packages) { $form['shipping_types'][$shipping_type] = array('#type' => 'fieldset', '#title' => $shipping_types[$shipping_type]['title']); $rows = array(); $form['shipping_types'][$shipping_type]['table'] = array('#type' => 'table', '#header' => $header, '#empty' => $this->t('There are no products available for this type of package.')); foreach ($packages as $package) { $pkgs_exist = TRUE; $row = array(); $row['checked'] = array('#type' => 'checkbox', '#default_value' => in_array($package->package_id, $checked_pkgs) ? 1 : 0); $row['package_id'] = array('#markup' => $package->package_id); $product_list = array(); foreach ($package->products as $product) { $product_list[] = $product->qty . ' x ' . $product->model; } $row['products'] = array('#theme' => 'item_list', '#items' => $product_list); $row['weight'] = array('#markup' => uc_weight_format($package->weight, $units)); $form['shipping_types'][$shipping_type]['table'][$package->package_id] = $row; } if (isset($shipping_methods_by_type[$shipping_type])) { foreach ($shipping_methods_by_type[$shipping_type] as $method) { $option_methods += array($method->id() => $method->label()); } } } $form['order_id'] = array('#type' => 'hidden', '#value' => $uc_order->id()); if ($pkgs_exist) { // uc_fulfillment has a default plugin to provide the "Manual" method. $form['method'] = array('#type' => 'select', '#title' => $this->t('Shipping method'), '#options' => $option_methods, '#default_value' => 'manual'); $form['actions'] = array('#type' => 'actions'); $form['actions']['ship'] = array('#type' => 'submit', '#value' => $this->t('Ship packages')); } return $form; }
/** * Loads a package and its products. * * @param int $package_id * The package ID. * * @return \Drupal\uc_fulfillment\Package|null * The Package object, or NULL if there isn't one with the given ID. */ public static function load($package_id) { if (!isset(self::$packages[$package_id])) { $result = db_query('SELECT * FROM {uc_packages} WHERE package_id = :id', [':id' => $package_id]); if ($assoc = $result->fetchAssoc()) { $package = Package::create($assoc); $products = array(); $description = ''; $weight = 0; $units = \Drupal::config('uc_store.settings')->get('weight.units'); $addresses = array(); $result = db_query('SELECT op.order_product_id, pp.qty, pp.qty * op.weight__value AS weight, op.weight__units, op.nid, op.title, op.model, op.price, op.data FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id ORDER BY op.order_product_id', [':id' => $package->package_id]); foreach ($result as $product) { $address = uc_quote_get_default_shipping_address($product->nid); // TODO: Lodge complaint that array_unique() compares as strings. if (!in_array($address, $addresses)) { $addresses[] = $address; } $description .= ', ' . $product->qty . ' x ' . $product->model; // Normalize all weights to default units. $weight += $product->weight * uc_weight_conversion($product->weight__units, $units); $product->data = unserialize($product->data); $products[$product->order_product_id] = $product; } $package->addresses = $addresses; $package->description = substr($description, 2); $package->weight = $weight; $package->weight_units = $units; $package->products = $products; if ($package->label_image && ($image = file_load($package->label_image))) { $package->label_image = $image; } self::$packages[$package_id] = $package; return $package; } else { return NULL; } } // Return package from cache. return self::$packages[$package_id]; }