Find product in cart fix

This commit is contained in:
Mike Jolley 2011-08-22 12:57:50 +01:00
parent da6d4b9da9
commit 40897953a3
3 changed files with 56 additions and 27 deletions

View File

@ -104,22 +104,29 @@ class woocommerce_cart {
unset($_SESSION['coupons']);
}
/** Check if product is in the cart */
function find_product_in_cart( $product_id, $variation = '' ) {
/**
* Check if product is in the cart and return cart item key
*
* @param int $product_id
* @param int $variation_id optional variation id
* @param array $variation array of attributre values
* @return int|null
*/
function find_product_in_cart($product_id, $variation_id, $variation = array()) {
foreach (self::$cart_contents as $cart_item_key => $cart_item) :
if ($variation) :
if ($cart_item['product_id'] == $product_id && $cart_item['variation']==$variation) :
if (empty($variation_id) && $cart_item['product_id'] == $product_id) :
return $cart_item_key;
endif;
else :
if ($cart_item['product_id'] == $product_id) :
elseif ($cart_item['product_id'] == $product_id && $cart_item['variation_id'] == $variation_id) :
if($variation == $cart_item['variation']) :
return $cart_item_key;
endif;
endif;
endforeach;
return NULL;
}
/**
@ -130,7 +137,7 @@ class woocommerce_cart {
*/
function add_to_cart( $product_id, $quantity = 1, $variation = '', $variation_id = '' ) {
$found_cart_item_key = self::find_product_in_cart($product_id, $variation);
$found_cart_item_key = self::find_product_in_cart($product_id, $variation_id, $variation);
if (is_numeric($found_cart_item_key)) :

View File

@ -129,7 +129,29 @@ class woocommerce_product_variation extends woocommerce_product {
return $this->variation;
}
/** Returns the price in html format */
/**
* Get variation ID
*
* @return int
*/
function get_variation_id() {
return (int) $this->variation_id;
}
/**
* Get variation attribute values
*
* @return array of attributes and their values for this variation
*/
function get_variation_attributes() {
return $this->variation_data;
}
/**
* Get variation attribute values
*
* @return string containing the formatted price
*/
function get_price_html() {
if ($this->variation_has_price || $this->variation_has_sale_price) :
$price = '';