Created
September 7, 2012 07:07
-
-
Save carwin/3664016 to your computer and use it in GitHub Desktop.
Drupal 7: Ubercart - Change the value of "Add to cart" when a product is already in a user's cart, or the product is out of stock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get the "Add to cart" array ready for rendering. | |
$add_to_cart = array( | |
'#theme' => 'uc_product_add_to_cart', | |
'#form' => drupal_get_form('uc_product_add_to_cart_form_' . $node->nid, $node), | |
); | |
/* | |
* Check the database to see the Stock availability of the product. | |
*/ | |
if(uc_stock_is_active($node->model)){ | |
$stock = uc_stock_level($node->model); | |
} | |
/* | |
* Check whether or not this product is already in the current | |
* user's cart. | |
*/ | |
$already_added = FALSE; | |
$cart = uc_cart_get_contents(); | |
foreach(element_children($cart) as $child){ | |
if($cart[$child]->nid == $node->nid){ | |
$already_added = TRUE; | |
} | |
} | |
/* | |
* If the Stock value is not 0 and the item is not in the current | |
* user's cart, render the "Add to cart" form normally. | |
* | |
* If the item is already in the user's cart, disable the button | |
* and replace the message. Products on this site are unique. | |
* | |
* If the Stock value is less than 1, disable the button and | |
* replace the message. This product has been sold out. | |
*/ | |
if($stock != 0 && $already_added == FALSE){ | |
print drupal_render($add_to_cart); | |
}elseif($already_added == TRUE){ | |
print '<input class="node-add-to-cart form-submit" type="submit" name="op" disabled value="Already in your cart!">'; | |
}elseif($stock < 1){ | |
print '<input class="node-add-to-cart form-submit" type="submit" name="op" disabled value="Sold Out">'; | |
} | |
/* | |
* A great compiment to this is the uc_out_of_stock module on drupal.org | |
* which uses Ajax to render messages to the user when they attempt | |
* to add more products to their cart than there is stock available. | |
* | |
* http://drupal.org/project/uc_out_of_stock | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment