/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; foreach ($items as $delta => $item) { if ((double) $item->value) { $elements[$delta] = array('#markup' => uc_weight_format($item->value, $item->units)); } } return $elements; }
/** * Overrides uc_product_handler_field_weight::render(). */ public function render($values) { $package = Package::load($values->{$this->aliases['package_id']}); if ($this->options['format'] == 'numeric') { return $package->weight; } if ($this->options['format'] == 'uc_weight') { return uc_weight_format($package->weight, $package->weight_units); } }
/** * {@inheritdoc} */ public function render(ResultRow $values) { if ($this->options['format'] == 'uc_weight') { $value = $this->getValue($values); if (is_null($value) || $value == 0 && $this->options['empty_zero']) { return ''; } return uc_weight_format($value, $values->{$this->aliases['weight_units']}); } else { return parent::render($values); } }
/** * {@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']); } }
public function testProductNodeForm() { $this->drupalGet('node/add/product'); $fields = array('model[0][value]', 'price[0][value]', 'shippable[value]', 'weight[0][value]', 'weight[0][units]', 'dimensions[0][length]', 'dimensions[0][width]', 'dimensions[0][height]', 'dimensions[0][units]', 'files[uc_product_image_0][]'); foreach ($fields as $field) { $this->assertFieldByName($field, NULL); } $title_key = 'title[0][value]'; $body_key = 'body[0][value]'; // Make a node with those fields. $edit = array($title_key => $this->randomMachineName(32), $body_key => $this->randomMachineName(64), 'model[0][value]' => $this->randomMachineName(8), 'price[0][value]' => mt_rand(1, 150), 'shippable[value]' => mt_rand(0, 1), 'weight[0][value]' => mt_rand(1, 50), 'weight[0][units]' => array_rand(array('lb' => t('Pounds'), 'kg' => t('Kilograms'), 'oz' => t('Ounces'), 'g' => t('Grams'))), 'dimensions[0][length]' => mt_rand(1, 50), 'dimensions[0][width]' => mt_rand(1, 50), 'dimensions[0][height]' => mt_rand(1, 50), 'dimensions[0][units]' => array_rand(array('in' => t('Inches'), 'ft' => t('Feet'), 'cm' => t('Centimeters'), 'mm' => t('Millimeters')))); $this->drupalPostForm('node/add/product', $edit, 'Save'); $this->assertText(t('Product @title has been created.', ['@title' => $edit[$title_key]]), 'Product created.'); $this->assertText($edit[$body_key], 'Product body found.'); $this->assertText($edit['model[0][value]'], 'Product model found.'); $this->assertNoUniqueText(uc_currency_format($edit['price[0][value]']), 'Product price found.'); $this->assertText(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']), 'Product weight found.'); $this->assertText(uc_length_format($edit['dimensions[0][length]'], $edit['dimensions[0][units]']), 'Product length found.'); $this->assertText(uc_length_format($edit['dimensions[0][width]'], $edit['dimensions[0][units]']), 'Product width found.'); $this->assertText(uc_length_format($edit['dimensions[0][height]'], $edit['dimensions[0][units]']), 'Product height found.'); $elements = $this->xpath('//body[contains(@class, "uc-product-node")]'); $this->assertEqual(count($elements), 1, 'Product page contains body CSS class.'); // Update the node fields. $edit = array($title_key => $this->randomMachineName(32), $body_key => $this->randomMachineName(64), 'model[0][value]' => $this->randomMachineName(8), 'price[0][value]' => mt_rand(1, 150), 'shippable[value]' => mt_rand(0, 1), 'weight[0][value]' => mt_rand(1, 50), 'weight[0][units]' => array_rand(array('lb' => t('Pounds'), 'kg' => t('Kilograms'), 'oz' => t('Ounces'), 'g' => t('Grams'))), 'dimensions[0][length]' => mt_rand(1, 50), 'dimensions[0][width]' => mt_rand(1, 50), 'dimensions[0][height]' => mt_rand(1, 50), 'dimensions[0][units]' => array_rand(array('in' => t('Inches'), 'ft' => t('Feet'), 'cm' => t('Centimeters'), 'mm' => t('Millimeters')))); $this->clickLink('Edit'); $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText(t('Product @title has been updated.', ['@title' => $edit[$title_key]]), 'Product updated.'); $this->assertText($edit[$body_key], 'Updated product body found.'); $this->assertText($edit['model[0][value]'], 'Updated product model found.'); $this->assertNoUniqueText(uc_currency_format($edit['price[0][value]']), 'Updated product price found.'); $this->assertText(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']), 'Product weight found.'); $this->assertText(uc_length_format($edit['dimensions[0][length]'], $edit['dimensions[0][units]']), 'Product length found.'); $this->assertText(uc_length_format($edit['dimensions[0][width]'], $edit['dimensions[0][units]']), 'Product width found.'); $this->assertText(uc_length_format($edit['dimensions[0][height]'], $edit['dimensions[0][units]']), 'Product height found.'); $this->clickLink('Delete'); $this->drupalPostForm(NULL, [], 'Delete'); $this->assertText(t('Product @title has been deleted.', ['@title' => $edit[$title_key]]), 'Product deleted.'); }
/** * {@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; }