1

here is the thing:
On my drupal commerce site I added a term reference field 'sizes' to line item, and make it to be an attribute on 'add to cart' form.
Also I added a field collection in to my product, wich has two fields: Margin(price) and size(term reference). So when I add some product I can choose a size and some extra margin to price.
What I want to do is when user add product to cart he chose some size, this size would compare with a size in field collection and the price for this item will update relatively to margin field collection field.
Anyone have any idea how can I do this?
Thanks

1

Thanks, Neograph734, but it's not exectly what I tryed to do. Maybe it isn't a proper way to do it but I solve this problem using custom _add_to_cart_form_submit() function and hook_commerce_cart_line_item_refresh(). So it looks something like this (maybe it will help someone):

function custom_add_to_cart_form_submit($form, &$form_state){
  // line item field_size_pick
  $line_item = $form_state['line_item'];

  $field_size_pick = field_get_items('commerce_line_item', $line_item, 'field_size_pick');
  $size_pick = $field_size_pick[0]['tid'];

  //get product
  $product_id = $form_state['values']['product_id'];
  $product = commerce_product_load($product_id);

  //fc sizes
  $field_fc_product_sizes = field_get_items('commerce_product', $product, 'field_fc_product_sizes');
  foreach ($field_fc_product_sizes as $key => $value) {
    $item_id = $value['value'];
    $fc_item = field_collection_item_load($item_id);

    $field_fc_size = field_get_items('field_collection_item', $fc_item, 'field_fc_size');
    $size = $field_fc_size[0]['tid'];

    if ($size == $size_pick) {
      $field_fc_margin = field_get_items('field_collection_item', $fc_item, 'field_fc_margin');
      $margin = $field_fc_margin[0]['amount'];
      break;
    }
  }

  if (isset($margin)) {
    $commerce_unit_price = field_get_items('commerce_line_item', $line_item, 'commerce_unit_price');
    $price = $commerce_unit_price[0]['amount'];
    $price += $margin;

    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $line_item_wrapper->commerce_unit_price->amount->set($price);
    $line_item_wrapper->save();
    commerce_line_item_rebase_unit_price($line_item_wrapper->value());
    commerce_line_item_save($line_item_wrapper->value());
  }

  $form_state['rebuild'] = TRUE;
}

function custom_commerce_cart_line_item_refresh($line_item, $order_wrapper) {

  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  $field_size_pick = $line_item_wrapper->field_size_pick->value();
  $size_pick = $field_size_pick[0]->tid;

  $field_fc_product_sizes = $line_item_wrapper->commerce_product->field_fc_product_sizes->value();
  foreach ($field_fc_product_sizes as $key => $fc_item) {
    $field_fc_size = field_get_items('field_collection_item', $fc_item, 'field_fc_size');
    $fc_size = $field_fc_size[0]['tid'];

    if ($size_pick == $fc_size) {
      $field_fc_margin = field_get_items('field_collection_item', $fc_item, 'field_fc_margin');
      $fc_margin = $field_fc_margin[0]['amount'];
      break;
    }
  }

  if (isset($fc_margin)) {
    $price = $line_item_wrapper->commerce_unit_price->amount->value();
    $price += $fc_margin;
    $line_item_wrapper->commerce_unit_price->amount->set($price);
    $line_item_wrapper->save();

    commerce_line_item_rebase_unit_price($line_item_wrapper->value());
    commerce_line_item_save($line_item_wrapper->value());
  }
  $order_wrapper->save();
}
0

In drupal commerce this goes by the name of product variants. There is quite some information on it, but this article from Drupal commerce should get you started: https://drupalcommerce.org/user-guide/product-attributes-variations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.