I have been working on a custom module for cart links on Commerce. Base on a article on CodeKarate.com (http://codekarate.com/blog/creating-drupal-commerce-add-cart-links).

I have try to follow this custom module and for some reason is not working for me. I upload on GitHub my version of the cart link module. https://github.com/darol100/Commerce-Cart-Link If someone can tell me what I did wrong, or what I'm missing I will appreciated.

***NOTE: I do not have a lot experiences on creating modules so sorry if something very simple.

Comments

darol100’s picture

Hey I need help with the Module Development

My goal of the module is having a cart link where if someone click on that link this module will run and add the product to my cart of my commerce site. However, when i go to the link, I will assume that and item will be added to my cart and that is not happening. Theoretically I should go to this link like this www.examplesite.com/product/add/[NODEID]/[Amount Of Items]. And the cart will added and for some resason is not working. I try this out and the product doesnt add to the cart.

My goal is integrating Commerce with another site so I need to have something like Cart Links in Ubercart to be able to do this integration in Commerce.

Can someone help me how to troubleshoot this ?

- Darryl Norris
Be Connected: Website | Twitter | LinkendIn | GitHub

WorldFallz’s picture

It's not very likely someone in the forums will install commerce and your module to try and help. You're best bet is to actually post the code you suspect might be the issue as well as describe in detail what you're expecting to see and what you are seeing.

Another option is posting to the commerce issue queue. But again, simply linking to a module on github isn't likely going to get a useful response.

darol100’s picture

@WorldFallz thanks for the advice. The reason I upload it to Github is because I thought will be easier for someone to troubleshoot. Also, I wanted to make most of my project open source so I thought that but uploading in GitHub will be a good practice for myself.

What I’m trying to accomplished is having a module where I can add a product by click a url. By doing this I will be able to insert my product in an attribute () and add the product to the cart.

The module is not too long so the problem should be some here in there.

<?php
/**
* Implements hook_menu().
*/
function commerce_cart_link_menu() {
  $items['product/add/%/%'] = array (
    'page callback' => 'commerce_cart_link_product_add',
    'page arguments' => array(2,3),
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK,
  );  
  return $items;
}  
?>

<?php
/**
* Custom function to add product(s) to a cart by product id, submitted by a form and looking for an affiliate cookie
*/
function commerce_cart_link_product_form_add($pid,$quantity=1) {
       $other = $_POST["other_info"];
       $affiliate = $_COOKIE['mysite_affiliate_id'];
    if ($product = commerce_product_load($pid) ) {
        global $user;
        $uid = $user->uid;    
        $line_item = commerce_product_line_item_new($product, $quantity);
        $line_item->field_ticket_type['und'][]['value'] = 0;
        $line_item->field_other['und'][]['value'] = $other;
        $line_item->field_affiliate['und'][]['uid'] = $affiliate;
        $line_item = commerce_cart_product_add($uid, $line_item, FALSE);
        
        drupal_goto('checkout');
    }    
}
?>

Any help will be appreciated.

- Darryl Norris
Be Connected: Website | Twitter | LinkendIn | GitHub

MrAdamJohn’s picture

This functionality was present in Ubercart, wondering if anyone got anywhere for Drupal Commerce... seems like a reasonable set of requirements and there are a few modules for Commerce that get close to this functionality... none that I've found nail it, yet.

@darol100 ... did you ever get your use case resolved? i.e. url.com/cart/nid/qty

darol100’s picture

I think, I did solve it but I do not remember. Also, I'm not longer part of that project.

- Darryl Norris
Be Connected: Website | Twitter | LinkendIn | GitHub