Skip to content

Instantly share code, notes, and snippets.

@mrconnerton
Last active December 24, 2015 12:08
Show Gist options
  • Save mrconnerton/6795192 to your computer and use it in GitHub Desktop.
Save mrconnerton/6795192 to your computer and use it in GitHub Desktop.
bundle parent
<?php
function CUSTOMMODULE_commerce_cart_product_add($order, $product, $quantity, $line_item) {
if (!empty($product->field_bundled_products)) {
$ids = array();
foreach($product->field_bundled_products[LANGUAGE_NONE] as $bundle) {
$ids[] = $bundle['value'];
}
$line_item->data['bundled'] = array();
$bundles = entity_load('field_collection_item', $ids);
foreach ($bundles AS $entity) {
if(!empty($entity->field_single_product)) {
$product_id = $entity->field_single_product[LANGUAGE_NONE][0]['product_id'];
$quantity = $entity->field_qty[LANGUAGE_NONE][0]['value'];
$product = commerce_product_load($product_id);
$product->commerce_price[LANGUAGE_NONE][0]['amount'] = 0;
$product->field_cost[LANGUAGE_NONE][0]['amount'] = 0;
$line = commerce_product_line_item_new($product, $quantity);
$line->field_bundle_parent[LANGUAGE_NONE][0]['line_item_id '] = $line_item->line_item_id;
$line->data['bundled'] = array();
commerce_cart_product_add($order->uid, $line, FALSE);
$line = entity_metadata_wrapper('commerce_line_item', $line);
$line->field_bundle_parent->set($line_item->line_item_id);
$line->save();
$line_item->data['bundled'][$line->line_item_id->value()] = array(
'product' => $entity->field_single_product['und'][0]['product_id'],
'qty' => $entity->field_qty['und'][0]['value'],
);
}
}
commerce_line_item_save($line_item);
}
}
function CUSTOMMODULE_commerce_cart_product_remove($order, $product, $quantity, $line_item) {
if (isset($product->field_bundled_products)) {
foreach($order->commerce_line_items[LANGUAGE_NONE] AS $item) {
$line = commerce_line_item_load($item['line_item_id']);
if ($line->field_bundle_parent[LANGUAGE_NONE][0]['line_item_id'] == $line_item->line_item_id) {
commerce_cart_order_product_line_item_delete($order, $line->line_item_id, TRUE);
}
}
}
}
function CUSTOMMODULE_form_alter(&$form, &$form_state, $form_id) {
global $user;
switch($form_id) {
case 'views_form_commerce_cart_form_CUSTOMMODULE_default': // I used custom view for cart to tweak
$order = commerce_cart_order_load($user->uid);
$line_item_ids = array();
foreach($order->commerce_line_items['und'] as $delta => $item) {
$line_item_ids[] = $item['line_item_id'];
}
$items = commerce_line_item_load_multiple($line_item_ids);
for($i = 0; $i < count($form['edit_quantity']); $i++) {
$id = $form['edit_quantity'][$i]['#line_item_id'];
if(!empty($items[$id]->field_bundle_parent)) {
$form['edit_quantity'][$i]['#disabled'] = TRUE;
$form['edit_delete'][$i]['#access'] = FALSE;
}
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment