0

I have created a customized product with lots of attributes to set for it. I also altered hook_add_to_cart_form_submit() where I collect all selected attributes and calculate a new price based on the selected and configured attributes. So in my

function MYMODULE_add_to_cart_form_submit($form, &$form_state) {}

I create a big array and:

if (empty($form_state['line_item']->line_item_id)) {
    if ($attributes)
        $form_state['line_item']->data['context']['attributes'] = $attributes;
    $line_item = commerce_product_line_item_new($product, $form_state['values']['quantity'], 0, $form_state['line_item']->data, $form_state['line_item']->type);
    //   ...
}

I'm not sure if I am doing it right, I just want to store that array in the line-item table. While adding product to cart randomly I get:

"PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'commerce_line_item-177-0-0-und' for key 'PRIMARY': INSERT INTO {field_data_commerce_unit_price} ..."

error. Should I use some special format for this $attribute array? Everything is stored as BLOB in commerce_line_item.data field.

1

When you call commerce_product_line_item_new, drupal is trying to insert an already existant record to the table field_data_commerce_unit_price which has it's primary key defined as :

PRIMARY KEY (entity_type,entity_id,deleted,delta,language),

Since the product already exists, it fails with a constraint violation error :

Integrity constraint violation: 1062 Duplicate entry

What you are trying to do could be accomplished by adding a new cost item to the current order based on the attributes you mention, not by modifying the current item price which I guess is what you are trying to do.

So IMHO, you may try to have a base price for your item (or even a 0 price) and once the customer adds it to cart, add another cost item with the charges calculated based on the attributes he sets.

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.