function uc_product_kit_uc_cart_display in Ubercart 8.4
Same name and namespace in other branches
- 7.3 uc_product_kit/uc_product_kit.module \uc_product_kit_uc_cart_display()
Implements hook_uc_cart_display().
Displays either the kit as a whole, or each individual product based on the store configuration. Each product in the cart that was added by uc_product_kit was also given a unique kit id in order to help prevent collisions. The side effect is that identical product kits are listed separately if added separately. The customer may still change the quantity of kits like other products.
Parameters
$item: An item in the shopping cart.
Return value
array A form element array to be processed by the cart form().
File
- uc_product_kit/
uc_product_kit.module, line 921 - The product kit module for Ubercart.
Code
function uc_product_kit_uc_cart_display($item) {
static $elements = [];
static $products;
$unique_id = $item->data->unique_id;
$kit = Node::load($item->data->kit_id);
if ($kit->mutable == UC_PRODUCT_KIT_MUTABLE) {
return uc_product_uc_cart_display($item);
}
else {
$nid = $item->nid->target_id;
if (!isset($products[$unique_id])) {
// Initialize table row.
$kit_qty = $item->qty->value / $kit->products[$nid]->qty;
$element = [];
$element['nid'] = [
'#type' => 'value',
'#value' => $kit
->id(),
];
$element['module'] = [
'#type' => 'value',
'#value' => 'uc_product_kit',
];
$element['remove'] = [
'#type' => 'submit',
'#value' => t('Remove'),
];
$element['title'] = [
'#type' => 'link',
'#title' => $kit
->label(),
'#url' => $kit
->toUrl(),
];
$element['qty'] = [
'#type' => 'uc_quantity',
'#title' => t('Quantity'),
'#title_display' => 'invisible',
'#default_value' => $kit_qty,
];
$element['description'] = [
'#markup' => '',
];
$element['#total'] = 0;
$element['#suffixes'] = [];
$element['#extra'] = [];
// Override the entity associated with this render array
// to be the kit itself.
$element['#entity'] = $kit;
$elements[$unique_id] = $element;
}
if ($kit->mutable == UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST) {
$elements[$unique_id]['#extra'][] = [
'#theme' => 'uc_product_kit_list_item',
'#product' => $item,
'#suffix' => uc_product_get_description($item),
'#wrapper_attributes' => [
'class' => [
'kit-component-cart-desc',
],
],
];
}
// Build the kit item product variant.
$variant = uc_product_load_variant($nid, $item->data
->first()
->toArray());
$elements[$unique_id]['#total'] += $variant->display_price * $item->qty->value;
$elements[$unique_id]['data'][$nid] = $item->data
->first()
->toArray();
$products[$unique_id][] = $nid;
// Check if all products in this kit have been accounted for.
$done = TRUE;
foreach ($kit->products as $product) {
if (!in_array($product
->id(), $products[$unique_id])) {
$done = FALSE;
break;
}
}
if ($done) {
$elements[$unique_id]['data'] = [
'#type' => 'value',
'#value' => serialize($elements[$unique_id]['data']),
];
if ($kit->mutable == UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST) {
$elements[$unique_id]['description'] = [
'#theme' => 'item_list',
'#items' => $elements[$unique_id]['#extra'],
'#attributes' => [
'class' => [
'product-description',
],
],
];
}
$element = $elements[$unique_id];
unset($products[$unique_id]);
unset($elements[$unique_id]);
return $element;
}
}
return [];
}