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

@ -27,7 +27,7 @@ jQuery(function(){
jQuery('body').trigger('adding_to_cart');
// Block widget
jQuery('.widget_shopping_cart, .shop_table.cart').block({ message: null, overlayCSS: { background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
jQuery('.widget_shopping_cart, .shop_table.cart').block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
jQuery.post( params.ajax_url, data, function(response) {
@ -53,7 +53,7 @@ jQuery(function(){
fragments = jQuery.parseJSON( response );
if (fragments) {
jQuery.each(fragments, function(key, value) {
jQuery.each(fragments, function(key, value) {
jQuery(key).replaceWith(value);
});
}
@ -148,7 +148,7 @@ jQuery(function(){
if (currentVal > 0)
{
jQuery(this).next(".qty").val(currentVal - 1);
}
}
});
/* states */
@ -208,12 +208,12 @@ jQuery(function(){
jQuery('.shipping-calculator-button').click(function() {
jQuery('.shipping-calculator-form').slideToggle('slow', function() {
// Animation complete.
});
});
});
// Stop anchors moving the viewport
jQuery(".shipping-calculator-button").click(function() { return false; });
jQuery(".shipping-calculator-button").click(function() {return false;});
// Variations
@ -229,7 +229,7 @@ jQuery(function(){
if (!not_set) {
jQuery('.variations').block({ message: null, overlayCSS: { background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
jQuery('.variations').block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
var data = {
action: 'woocommerce_get_variation',
@ -317,7 +317,7 @@ if (params.is_checkout==1) {
var s_postcode = jQuery('input#shipping-postcode').val();
}
jQuery('#order_methods, #order_review').block({ message: null, overlayCSS: { background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
jQuery('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
var data = {
action: 'woocommerce_update_order_review',
@ -403,7 +403,7 @@ if (params.is_checkout==1) {
/* AJAX Form Submission */
jQuery('form.checkout').submit(function(){
var form = this;
jQuery(form).block({ message: null, overlayCSS: { background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
jQuery(form).block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
jQuery.ajax({
type: 'POST',
url: params.checkout_url,
@ -417,7 +417,7 @@ if (params.is_checkout==1) {
catch(err) {
jQuery(form).prepend( code );
jQuery(form).unblock();
jQuery.scrollTo(jQuery(form).parent(), { easing:'swing' });
jQuery.scrollTo(jQuery(form).parent(), {easing:'swing'});
}
},
dataType: "html"

View File

@ -104,23 +104,30 @@ class woocommerce_cart {
unset($_SESSION['coupons']);
}
/** Check if product is in the cart */
function find_product_in_cart( $product_id, $variation = '' ) {
foreach (self::$cart_contents as $cart_item_key => $cart_item) :
if ($variation) :
if ($cart_item['product_id'] == $product_id && $cart_item['variation']==$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 (empty($variation_id) && $cart_item['product_id'] == $product_id) :
return $cart_item_key;
elseif ($cart_item['product_id'] == $product_id && $cart_item['variation_id'] == $variation_id) :
if($variation == $cart_item['variation']) :
return $cart_item_key;
endif;
else :
if ($cart_item['product_id'] == $product_id) :
return $cart_item_key;
endif;
endif;
endforeach;
}
endif;
endif;
endforeach;
return NULL;
}
/**
* Add a product to the cart
@ -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 = '';