First of all, thank you for this great module.

I'm in process of building a "related product" modules using node references as the point to fetch all the nodes that will be related to the original product display node.

I've managed to get the products data as follow :

stdClass Object
(
    [product_id] => 4
    [sku] => Product Four
    [type] => product
    [language] => und
    [title] => Product Four
    [uid] => 1
    [status] => 1
    [created] => 1311065162
    [changed] => 1311065162
    [data] => 
    [commerce_price] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [amount] => 10000
                            [currency_code] => USD
                            [data] => Array
                                (
                                    [components] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [field_commerce_image] => Array
        (
        )

    [rdf_mapping] => Array
        (
        )

)

and managed to display the SKU, Product title, Product images.
The problem is how to print the add to cart button based on those arrays?

Thanks for the help.

Cheers

Comments

duckzland’s picture

Answering my own question....

Assuming the object structure of $view['product'] is as the posts above and the following code is created under a template_preprocess function....

    // build the line items for shopping cart
    $line_item = commerce_line_item_new($view['product']->type, $order_id = 0);
    $line_item->data['context']['product_ids'] = array($view['product']->product_id);
    $line_item->quantity = 1;
    
    // need to create config for quantity
    $qty = 1;
    
    $form_id = commerce_cart_add_to_cart_form_id(array($view['product']->product_id), $qty);
    $addtocart_form = drupal_get_form($form_id, $line_item);
    
    // we alter the submit form to use our special theme function
    // need to move this to configuration
    $addtocart_form['submit']['#theme'][] = 'vtcommerce_button_small';
    
    $variables['cart'] = render($addtocart_form);
    

I'm able to print the submit button by invoking print $cart in a custom.tpl.php files, but is this the right way to do this?

johnodonnell@mac.com’s picture

Thanks duckzland, you're awesome!

Just spent all morning trying to make sense of DC documentation, and here you are describing the whole thing in a few lines of code.

duckzland’s picture

Thanks, Although I'm not sure that this is the right way as the commerce developer intended.

I believe there should be a way using entity_load to load this programmatically, just haven't found them yet :)

johnodonnell@mac.com’s picture

Yeah, I'm beginning to rethink this approach as well. I have a product type that uses an option set with a lot of fields (drop downs, dates, etc.) and have been trying to create a wizard that would fill those fields out on behalf of the user. So my plan was just to create a form instance with matching form_state update the option values in the form_state and submit the form programmatically.

But after spending a whole day trying to get this to work, I'm thinking it might be better in my case just to work directly with the entities.

Still, thanks for posting.

duckzland’s picture

well do post in your solution to print out the add to cart form via entities directly :)

johnodonnell@mac.com’s picture

Actually, I didn't need to print the add to cart form. I just needed to add an item to the cart behind the scenes. I have a form with a list of checkboxes for various products. The user could select checkboxes and submit to add all those products to their cart in one go.

At first, I thought it would be easier to get each product's form object and submit it programatically. But it turns out working thru the form API in that way was more trouble then it's worth.

Here's the solution I came up with: #1270116: Pragmatically adding options to a line-item . Just like you, I start by grabbing a product entity and creating a line-item. But instead of creating a form for that line_item, I just add it to my cart.

derekw’s picture

How did you create this form with product checkboxes that can all be added to the cart at once?

arpitr’s picture

to get the add to cart button
I simply created a product display content type(machine name as product_display) with a product reference field (machine name as field_product_reference)
this content type basically act as a bridge to show the products added on front end so to customize the display for the product display I created a node template on my custom theme with name as node---display-product.tpl.php.Inside this I just print all the required fields for the products, like add to cart form
with my naming conventions only this much of code helped me to get the add to cart button
Hope this may be of some help :)
print render($content['field_product_reference']);

rszrama’s picture

Status: Active » Fixed

Closing this as fixed. Rendering the appropriate part of the content array is the solution, and for derekw's follow-up request, there's http://drupal.org/project/commerce_add_to_cart_extras.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

zmove’s picture

I tested the code in #1 and it works well, but how to print the add to cart form with the quantity field ?

Thank you

Alex

mparas’s picture

This is what I got working:

<?php 

$form_id = commerce_cart_add_to_cart_form_id(array($product->product_id));
$line_item = commerce_product_line_item_new($product);
$line_item->data['context']['product_ids'] = array($product->product_id);

$form = drupal_get_form($form_id, $line_item);
// render your $form somewhere

?>

zmove, if you want the quantity field, the form function lets you pass a boolean to show or not show the quantity field.

function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_quantity = FALSE, $context = array())

dahousecat’s picture

Thanks mparas - that code works perfectly for me.

Anonymous’s picture

mparas, you're a life saver. I was missing the part of getting the proper form_id. With this snippet I can now create a simple table of membership levels (e.g. gold, silver, bronze) and have the last row show 3 add to cart buttons, one for each level. To get the product id just grab it from store/product/edit the desired product ... the product_id is in the url.

Here's the code I that built the add to cart button using a specific product_id:

<?php
$form_idp= commerce_cart_add_to_cart_form_id(array(7));  
//in this case product_id of 7 corresponds to a silver level 
$productp = commerce_product_load(7);
$line_itemp = commerce_product_line_item_new($productp, 1);  // 1 is quantity
$line_itemp->data['context']['product_ids'] = array(7);
$formp = drupal_get_form($form_idp, $line_itemp);
print drupal_render($formp);   // renders add to cart for product id of 7
?>
Ruffna’s picture

Thanks emag, your code is working !

The only problem is that my product is added in duplicate :
- If I write "$line_itemp = commerce_product_line_item_new($productp, 1);", I will have two products in my cart.
- If I write "$line_itemp = commerce_product_line_item_new($productp, 2);", I will have four products.
Do you know why ?

This is my final code :
$form_idp= commerce_cart_add_to_cart_form_id(array(295));
$productp = commerce_product_load(295);
$line_itemp = commerce_product_line_item_new($productp, 1);
$line_itemp->data['context']['product_ids'] = array(295);
$formp = drupal_get_form($form_idp, $line_itemp);
print drupal_render($formp);

Thank you