wordpress - Woocommerce - can't add to cart as a guest using Quick Order One Page Shop? -
using plugin @ http://products.solvercircle.com/woocommerce-one-page-quick-shop/quick-shop/
it seems if i'm logged in regular user in admin, can add items cart fine. if add cart guest ajax request , returns '1' item never added cart.
does know why might so? other 2 plugins have on site deactivated , can still reproduce this, believe bug in plugin.
edit: confirmed 'guests can checkout' settings box applied, doesn't seem it.
edit #2: here's add cart fn:
function wqo_add_prod(pid,vid){ var qty= jquery('#product_qty_'+vid).val(); if(qty==0){ jquery('#wqo_alert_info').text('out of stock'); jquery('#wqo_alert_info').show() settimeout(function(){jquery('#wqo_alert_info').hide()}, 1500); return false; } if(vid==0){ qty= jquery('#product_qty_'+pid).val(); } var ajax_url = 'http://domain.com/wp-admin/admin-ajax.php'; jquery.ajax({ type: "post", url:ajax_url, data : { 'action': 'wqo_addtocart', 'wqo_prod_id': pid, 'wqo_prod_var_id': vid, 'wqo_prod_qty': qty }, success: function(response){ if(response==1){ jquery('#wqo_alert_info').text('added cart'); }else{ jquery('#wqo_alert_info').text(response); } jquery.ajax({ type: "post", url:ajax_url, data : {'action': 'wqo_cart_amount'}, success: function(data){ jquery('#wqo_cart_price').html(data); } }); jquery('#wqo_alert_info').show() settimeout(function(){jquery('#wqo_alert_info').hide()}, 2000); } }); }
edit #3: source php callback
function wqo_addtocart() { global $woocommerce; $vid=$_post['wqo_prod_var_id']; $pid=$_post['wqo_prod_id']; $vid=$_post['wqo_prod_var_id']; $pqty=$_post['wqo_prod_qty']; if($vid==0){ $product = wc_product_factory::get_product($pid); }else{ $product = wc_product_factory::get_product($vid); } $stock=$product->get_stock_quantity(); $availability = $product->get_availability(); if($availability['class']=='out-of-stock'){ echo 'out of stock'; exit; } if($stock!=''){ foreach($woocommerce->cart->cart_contents $cart_item_key => $values ) { $c_item_id=''; $c_stock=''; if($values['variation_id']!=''){ $c_item_id=$values['variation_id']; }else{ $c_item_id=$values['product_id']; } $c_stock=$values['quantity']+$pqty; if($vid==0 && $pid==$c_item_id && $c_stock>$stock){ $product = wc_product_factory::get_product($pid); echo 'you have cross stock limit'; exit; }else if($vid==$c_item_id && $c_stock>$stock){ $product = wc_product_factory::get_product($vid); echo 'you have cross stock limit'; exit; } } } if($vid==0){ $z=$woocommerce->cart->add_to_cart($pid,$pqty,null, null, null ); }else{ $z=$woocommerce->cart->add_to_cart($pid, $pqty, $vid, $product->get_variation_attributes(),null); } echo '1'; exit; }
registers @ bottom of woo-quick-order/includes/wqo-view.php ( above fns stolen )
675 add_action( 'wp_ajax_nopriv_wqo_addtocart','wqo_addtocart' ); 676 add_action( 'wp_ajax_wqo_addtocart', 'wqo_addtocart' );
edit #4: think it's session issue. in first ajax call calls wqo_addtocart
if do:
var_dump($woocommerce->cart->cart_contents);
it returns array , main key hash $z
returns.
on second ajax request however, lost:
59 function wqo_cart_amount(){ 60 global $woocommerce; 61 var_dump($woocommerce->cart->cart_contents); 62 echo $woocommerce->cart->get_cart_total(); 63 exit; 64 }
this returns empty array. in between first , second ajax requests gets lost.
there answer :),
// hook after add cart add_action( 'woocommerce_add_to_cart' , 'repair_woocommerce_2_2_8_session_add_to_cart'); function repair_woocommerce_2_2_8_session_add_to_cart( ){ if ( defined( 'doing_ajax' ) ) { wc_setcookie( 'woocommerce_items_in_cart', 1 ); wc_setcookie( 'woocommerce_cart_hash', md5( json_encode( wc()->cart->get_cart() ) ) ); do_action( 'woocommerce_set_cart_cookies', true ); } }
woocommerce have made change on add_to_cart function problem here : https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-cart.php#l978
the right code should :
if ( did_action( 'wp' ) || defined( 'doing_ajax' ) ) { $this->set_cart_cookies( sizeof( $this->cart_contents ) > 0 ); }
Comments
Post a Comment