Skip to content

Instantly share code, notes, and snippets.

@nguyenthanhxuan
Created December 13, 2015 02:48
Show Gist options
  • Save nguyenthanhxuan/2376aac9fc09b9a58e93 to your computer and use it in GitHub Desktop.
Save nguyenthanhxuan/2376aac9fc09b9a58e93 to your computer and use it in GitHub Desktop.
<?php
/*
1. Add a product programmatically to cart.
2. Get line item id for a product in cart from product id.
3. Delete a product programmatically from cart
4. Alter product quantity in cart(add/remove).
*/
/**
*
* function to check if a product exist in cart or not
* @param
* $product_id takes product id we are checking if exists or not as input.
* type take product id as integer or array of product ids
* @return
* return -1 if product does not exists
* returns -2 if cart if empty
* returns line item ids for the searched product(array of line item ids incase of multiple search of products else single line item 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;
}
}
/**
* function to delete a product from cart.
* @param
* $product_ids takes product id to be deleted as argument
* type it can be array of product ids or can be single product ids.
* @return
* returns -1 on failure.
* faliure is possible because of
* given product does not exist
* current cart object is empty
*/
function product_delete_cart($product_ids) {
if (!is_array($product_ids)) {
$line_item_id = product_in_cart($product_ids);
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;
}
}
}
/**
* function to add a product to cart
* @param
* $product_id takes product id to be added to cart as argument
* @param
* $quantity to be added for the given product
* @param
* $uid user id
* this argument is optional,incase no value is passed for $uid, function takes the uid of the current user.
* @return
* returns line item id for the product added to cart
*/
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;
}
/**
* function to alter quantity of a product in the cart
* @param
* $product_id takes product id for which you need to alter the quantity in the cart
* @param
* $quantity takes the quantity to be removed or added to existing quantity of the product in the cart.
* @param
* $param takes remove/add as param to differenciate between adding of quantity or removing of quantity.
* @return
* returns -1 in case of failure.
*/
function product_alter_quanity($product_id, $quantity, $param) {
$line_item_id = product_in_cart($product_id);
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment