I wanted to use a form instead of the add to cart link for all products. This way I could specify the quantity. It also makes it consistent with apparel products with variations. I started by dropping this code into my product theme, which is based on the subproduct add to cart form.
$form = array();
$form['#method'] = 'post';
$form['#action'] = url("cart/add/$nid", "destination=node/$nid");
$form['#tree'] = TRUE;
$form['add_to_cart'] = array(
'#type' => 'submit',
'#value' => t('add to cart')
);
$output .= drupal_get_form('subproducts_add_to_cart', $form);
print $output;
This displays the form as expected and posts to the correct URL.
I found that when I post data to "/cart/add/$nid" for an apparel product with no subproducts created, I get an error: "Unable to find the correct product based upon the variation combination." This was being caused by the productapi hook in the apparel module trying to look up variations when there aren't any. I've added a simple test, so that when there are no variations, the original node is returned.
case 'cart add item':
// Test for variations to allow add to cart using form for non-subproduct enabled nodes
if (isset($data->variations)) {
return subproducts_cart_set_subproduct_variation($node, $data);
} else {
return $node;
}
This may not be the orignial intent of the apparel product type, but we have a ton of products already created in this type and not all have variations. This was the only thing preventing it from working.
| Comment | File | Size | Author |
|---|---|---|---|
| add_to_cart.patch | 731 bytes | criznach |
Comments
Comment #1
criznach commentedI'm guessing the 5.0 branch could benefit from this too, but I'm only working on 4.7 right now.
Comment #2
brmassa commentedChris,
its a old problem regarding subproducts.
EC Live Subproducts (an advanced version of ecommerce subproducts) fixs this issue. I suggest to use EC Live Subproducts instead. This module will be merged to subproducts on next Ecommerce version, v4, so you you get full support.
regards,
massa
PS: only for 5.0. D4.7 version is not supported anymore
Comment #3
brmassa commentedComment #4
jinlong commentedthe same prolem. how to deal with?