Tags/topics: 
2
Answers
Vote up!
0
Vote down!

Get add to cart product info and bring to url-arguments

Hi,
Now I finally am able to get visitors to click on my contact category page when a product is not available, see: http://www.drupalcommerce.org/questions/8363/how-alter-add-cart-button-d...

I would very much like to be able to have the productinfo attached to the contactpage category.
I am able to show this using this module: https://drupal.org/project/contact_forms
With this module I can show contactforms with /contact/category/subject
But I am unable to build the needed url with in the subject the SKU and product Title.

How can I get the product SKU and product title to be set in a url using something like form_state may be?
I see http://drupal.stackexchange.com/questions/24279/populating-fields-from-t... for users, but do not see a product example.

Greetings, Martijn

Asked by: Summit
on June 10, 2013

2 Answers

Vote up!
0
Vote down!

Hi, Morne helped me again using rules! Where are other people here?
The rule is a changed stock-2.dev rule:

{ "rules_redirect_to_contact_form" : {
    "LABEL" : "Stock: redirect to contact form",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "commerce_ss", "rules", "commerce_stock" ],
    "ON" : [ "commerce_stock_check_add_to_cart_form_state" ],
    "IF" : [
      { "commerce_ss_stock_enabled_on_product" : { "commerce_product" : [ "commerce_product" ] } },
      { "commerce_ss_stock_not_disabled" : { "commerce_product" : [ "commerce_product" ] } },
      { "NOT data_is" : {
          "data" : [ "commerce-product:commerce-stock" ],
          "op" : "\u003E",
          "value" : "0"
        }
      }
    ],
    "DO" : [
      { "commerce_stock_custom_cart_form_state" : {
          "hide_qty" : 0,
          "text" : "Contact us for stock",
          "class_name" : "out-of-stock",
          "stock_action" : "1",
          "disabled_cart" : 0,
          "custom_submit" : "commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit",
          "custom_submit_clear" : 1,
          "custom_validate_clear" : 1
        }
      }
    ]
  }
}

The only code needed is the custom_submit:

<?php
/**
* @file
* Adds a link to the contact page when clicking on the add to cart form
of a disabled product
*/

/**
* Add to cart form submit handler.
*/
function
commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit($form,
&
$form_state) {
 
// Note: only use data in $form_state['values']
 
$product_id = $form_state['values']['product_id'];

 
$form_state['redirect'] = array(
   
'contact',
    array(
     
'query' => array(
       
'product_id' => $product_id,
      ),
    ),
  );
}
?>

greetings, Martijn
Answer by: Summit
Posted: Jun 29, 2013
Vote up!
0
Vote down!

Hi, I solved this myself with some help from Morne.

<?php
/**
 * @file
 * Adds a link to the contact page when clicking on the add to cart form of a disabled product
 */

/**
 * Implements hook_form_FORM_ID_alter(): commerce_cart_add_to_cart_form
 */
function commerce_product_disabled_link_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id, $context = array()) {
  if (!empty(
$form['submit']['#attributes']['disabled'])) {
    
$form_state['context'] = $context;
    
$form['contact_link'] = array(
            
'#markup' => l(t('Please contact us'), 'contact'),
       ); 
   
$form['submit']['#value'] = t('Contact for larger amounts');
    unset(
$form['submit']['#attributes']['disabled']);
    unset(
$form['submit']['#validate']);
   
$form['submit']['#validate'] = array('commerce_product_disabled_link_commerce_cart_add_to_cart_form_validate');
   
//$form['#validate'] = array();
   
$form['#submit'] = array('commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit');
  }
}
/**
 * Add to cart form validate handler.
 */
function commerce_product_disabled_link_commerce_cart_add_to_cart_form_validate(&$form, &$form_state) {
 
//drupal_set_message("<pre>".print_r($form_state, true)."</pre>");
}

/**
 * Add to cart form submit handler.
 */
function commerce_product_disabled_link_commerce_cart_add_to_cart_form_submit($form, &$form_state) {
 
$product_id = 0;
 
$sku = '';
 
//Get the SKU
 
if (isset($form_state['build_info']['args'][0]->line_item_label)) {
 
$sku = $form_state['build_info']['args'][0]->line_item_label;
 
$product_id = $form_state['build_info']['args'][0]->commerce_product['und'][0]['product_id'];
 
$product = commerce_product_load($product_id);
 
$producttitle = $product->title;
  }
 
// Set the redirect to the disabled product
 
$form_state['redirect'] = array(
 
'offerteaanvraag',
  array(
   
'query' => array(
     
'sku' => $sku,
     
'producttitel' => $producttitle,
    ),
  ),
);
}
?>

Greetings,
Martijn
Answer by: Summit
Posted: Jun 23, 2013

Comments