There may be use cases where modules would want to alter the product variants that can be selected via a product display's add to cart form.

One such use case is as follows:

Product (variants) should only be visible when a field on that variant (which contains a synchronisation id number that connects it to fulfillment / back office software) is not empty.

Patch to follow.

Comments

PQ created an issue. See original summary.

pq’s picture

Status: Active » Needs review
StatusFileSize
new1.76 KB

Patch attached.

rszrama’s picture

I'm not sure if we need / want to introduce a hook here. The problem is it reduces the usefulness of the line item argument having in it all the context required to recreate an Add to Cart form. Would it be possible for you to alter the line item before it gets passed into this form? One potential could be a wrapper function inserted via hook_forms(), but I'm not sure it sufficiently supports altering wrapper callbacks onto other modules' forms.

pq’s picture

I tried that, the problem is that in commerce_cart_add_to_cart_form_product_ids() the condition that causes the product IDs to be populated is as follows:

  // If the product IDs setting tells us to use entity values...
  if ($line_item->data['context']['product_ids'] == 'entity' &&
    is_array($line_item->data['context']['entity'])) {
    $entity_data = $line_item->data['context']['entity'];

    // Load the specified entity.
    $entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']);

    // Extract the product IDs from the specified product reference field.
    if (!empty($entity->{$entity_data['product_reference_field_name']})) {
      $product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}->raw();
    }
  }

So basically it loads the host entity and gets the raw values from there, so the only way to hook into that without modifying commerce_cart.module would be to hook into the entity load and unset them there but that would have all sorts of horrendous side effects.