Monday, July 6, 2015

Programmatically create view mode for product in Drupal Commerce

1. First create the view mode by using the following hook or Entity View Mode module. 

2. Then create the template file commerce-product--product--{view-mode}.tpl.php
3. Inside the template file, you can render what ever you want.
4. Then clear the cache.


Monday, June 29, 2015

Generate SSH Key using GIT


Please follow the below steps to create the SSH key in GIT


1) Open Git Bash (type git bash in search bar of windows start)

2) Generate the key using following command
    $ ssh-keygen -t rsa -b 4096
  This will prompt you to enter file name, just press enter key.
  When prompting for passpharse, enter a passpharse. (Note the pharse
  which will be used while you clone the repository)

3) Open key file and copy 
  $ cat /d/Users/<yourloginusername>/.ssh/id_rsa.pub

Friday, June 26, 2015

Drupal commerce programmatically change the quantity of product in a cart by using product id

Programmatically change the quantity of product in a cart by using product id


function product_alter_quanity($product_id, $quantity, $param) {
  $line_item_id = product_in_cart($product_id); // product_in_cart() function - http://php-developer-informations.blogspot.co.uk/2015/06/drupal-commerce-add-line-item-delete.html
  if ($line_item_id != -1 && $line_item_id != -2) {
    if ($param == 'remove') {
      $product_quantity_in_cart = commerce_line_items_quantity(array($line_item_id));
      if ($quantity >= $product_quantity_in_cart) {
        product_delete_cart($product_id);
      }
      else {
        $line_item = commerce_line_item_load($line_item_id);
        $line_item->quantity = $line_item->quantity - $quantity;
        commerce_line_item_save($line_item);
      }
    }
    elseif ($param == 'add') {
      //$line_item_id = product_in_cart($product_id);
      $line_item = commerce_line_item_load($line_item_id);
      $line_item->quantity = $line_item->quantity + $quantity;
      commerce_line_item_save($line_item);
    }
  }
  else {
    return -1;
  }
}

Drupal commerce programmatically delete a line item from the cart by using product id

Programmatically delete a line item from the cart by using product id

function product_delete_cart($product_ids) {
  if (!is_array($product_ids)) {
    $line_item_id = product_in_cart($product_ids); // product_in_cart() function - http://php-developer-informations.blogspot.co.uk/2015/06/drupal-commerce-add-line-item-delete.html
    if ($line_item_id != -1 && $line_item_id != -2) {
      global $user;
      $current_order = commerce_cart_order_load($user->uid);
      commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
    } else
      return -1;
  }
  else {
    $line_item_ids = product_in_cart($product_ids);
    if ($line_item_ids != -1 && $line_item_ids != -2) {
      global $user;
      $current_order = commerce_cart_order_load($user->uid);
      foreach ($line_item_ids as $line_item_id) {
        commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
      }
    }
    else {
      return -1;
    }
  }
}

Drupal Commerce find the line item id by using product id

Programmatically find the line item id by using product id


function product_in_cart($product_id) {
  if (!is_array($product_id)) {
    global $user;
    $current_order = commerce_cart_order_load($user->uid);
    if (count($current_order->commerce_line_items) > 0) {
      $line_items = $current_order->commerce_line_items;
      foreach ($line_items['und'] as $key => $value) {
        $line_item = commerce_line_item_load($value['line_item_id']);
        $products = $line_item->commerce_product['und'];
        foreach ($products as $product_key => $product_value) {
          if ($product_id == $product_value['product_id']) {
            return $value['line_item_id'];
          }
        }
      }
      return -1;
    } else
      return -2;
  }
  else {
    global $user;
    $current_order = commerce_cart_order_load($user->uid);
    if (count($current_order->commerce_line_items) > 0) {
      $line_items = $current_order->commerce_line_items;
      foreach ($line_items['und'] as $key => $value) {
        $line_item = commerce_line_item_load($value['line_item_id']);
        $products = $line_item->commerce_product['und'][0]['product_id'];
        foreach ($product_id as $id) {
          if ($id == $products) {
            $line_item_ids[] = $value['line_item_id'];
          }
        }
      } if (isset($line_item_ids) >= 1) {
        return $line_item_ids;
      }
      else {
        return -1;
      }
    } else
      return -2;
  }
}

Drupal Commerce add line item

Function for programmatically add a product as line item by using product id

function product_cart_add($product_id, $quantity, $uid = NULL) {
  $line_item = NULL;
  if ($uid == NULL) {
    global $user;
    $uid = $user->uid;
  }
  if ($product = commerce_product_load($product_id)) {
    $line_item = commerce_product_line_item_new($product, $quantity);
    $line_item = commerce_cart_product_add($uid, $line_item);
  }
  return $line_item;
}

Wednesday, May 27, 2015

Drupal user login form customization

Customizing the user login page is very simple, and uses the following concepts:

Use preprocess to set variables
register you template file in hook_theme
create templates and render your variables

Step 1:
/**
 * Implements hook_theme().
 */
function hook_theme() {
  $items = array();
  $items['user_login'] = array(
      'render element' => 'form',
      'path' => PATH . '/templates',
      'template' => 'user-login',
      'preprocess functions' => array(
          'hook_preprocess_user_login'
      ),
  );
  return $items;
}

Step 2:
function hook_preprocess_user_login(&$variables) { 
  $variables['render_text'] = 'You can add extra text and message';
}

Step 3:
Create you template file in  your template folder
user-login.tpl.php

<div class="login-wrapper">

      <h2><?php print t('SIGN IN'); ?></h2>

      <?php
      // split the username and password from the submit button so we can put in links above
      print drupal_render($form['name']);
      print drupal_render($form['pass']);
      ?>

      <div class="login-links">
        <?php print l(t('FORGOT YOUR PASSWORD?'), 'user/password', array('attributes' => array('class' => 'login-link'))); ?>
      </div>

      <?php
      print drupal_render($form['form_build_id']);
      print drupal_render($form['form_id']);
      print drupal_render($form['actions']);
      ?>

    </div><!--//login-wrapper-->


Step 4:
Clear drupal cache

Wednesday, April 15, 2015

Add custom file extension in PHP application

We can add custom file extension in Apache PHP application. We can add our custom file extension instead of .php

For example, index.xyz instead of index.php

Open httpd.conf located in apache/conf folder
Find " <IfModule mime_module> "
Add " AddType application/x-httpd-php .xyz "
Restart your apache server


Thursday, March 5, 2015

Status messages not removing with Commerce Ajax Cart

Use ajax_command_remove() command to remove the existing error messages while submit ajax add to cart button

$commands[] = ajax_command_remove('div.messages');
$commands[] = ajax_command_after('#main-content', theme('status_messages'));

Friday, January 30, 2015

Create an commerce order programmatically

The following steps to describe about to create an commerce order programmatically in Drupal

global $user;
$product_id = 100;
$quantity = 1;

$order = commerce_order_new($user->uid, 'cart');
commerce_order_save($order);
$line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
commerce_order_save($order);