woocommerce_add_to_cart_variable_rc_callback

The Woocommerce Add to cart Ajax for variable products woocommerce add to cart variable rc callback function.

Description

woocommerce_add_to_cart_variable_rc_callback(); 

Usage

  1. if ( !function_exists( 'woocommerce_add_to_cart_variable_rc_callback' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-add-to-cart-ajax-for-variable-products/woocommerce-ajax-add-to-cart-variable-products.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = woocommerce_add_to_cart_variable_rc_callback(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/woocommerce-ajax-add-to-cart-variable-products.php  
  1. function woocommerce_add_to_cart_variable_rc_callback() { 
  2.  
  3. ob_start(); 
  4.  
  5. $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); 
  6. $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] ); 
  7. $variation_id = $_POST['variation_id']; 
  8. $variation = $_POST['variation']; 
  9. $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); 
  10.  
  11. if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) ) { 
  12. do_action( 'woocommerce_ajax_added_to_cart', $product_id ); 
  13. if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) { 
  14. wc_add_to_cart_message( $product_id ); 
  15.  
  16. // Return fragments 
  17. WC_AJAX::get_refreshed_fragments(); 
  18. } else { 
  19. $this->json_headers(); 
  20.  
  21. // If there was anerroradding to the cart, redirect to the product page to show any errors 
  22. $data = array( 
  23. error => true,  
  24. 'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ) 
  25. ); 
  26. echo json_encode( $data ); 
  27. die(); 
  28. }