/woocommerce-ajax-add-to-cart-variable-products.php

  1. <?php 
  2. /** 
  3. Plugin Name: Woocommerce Add to cart Ajax for variable products 
  4. Plugin URI: http://www.rcreators.com/woocommerce-ajax-add-to-cart-variable-products 
  5. Description: Ajax based add to cart for varialbe products in woocommerce. 
  6. Author: Rishi Mehta - Rcreators Websolutions 
  7. Version: 1.2.9 
  8. Author URI: http://rcreators.com 
  9. */ 
  10.  
  11. // If this file is called directly, abort. 
  12. if ( ! defined( 'WPINC' ) ) { 
  13. die; 
  14.  
  15. /** 
  16. * Check if WooCommerce is active 
  17. **/ 
  18. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || array_key_exists( 'woocommerce/woocommerce.php', get_site_option('active_sitewide_plugins') ) ) { 
  19.  
  20. /** 
  21. * Create the section beneath the products tab 
  22. **/ 
  23.  
  24. add_filter( 'woocommerce_get_sections_products', 'wc_ajax_add_to_cart_variable_add_section' ); 
  25. function wc_ajax_add_to_cart_variable_add_section( $sections ) { 
  26.  
  27. $sections['wc_ajax_add_to_cart_variable'] = __( 'WC Ajax for Variable Products', 'text-domain' ); 
  28. return $sections; 
  29.  
  30.  
  31. add_filter( 'woocommerce_get_settings_products', 'wc_ajax_add_to_cart_variable_all_settings', 10, 2 ); 
  32. function wc_ajax_add_to_cart_variable_all_settings( $settings, $current_section ) { 
  33.  
  34. /** 
  35. * Check the current section is what we want 
  36. **/ 
  37.  
  38. if ( $current_section == 'wc_ajax_add_to_cart_variable' ) { 
  39.  
  40. $settings_slider = array(); 
  41.  
  42. // Add Title to the Settings 
  43. $settings_slider[] = array( 'name' => __( 'WC Ajax for Variable Products Settings', 'text-domain' ), 'type' => 'title', 'desc' => __( 'The following options are used to configure WC Ajax for Variable Products', 'text-domain' ), 'id' => 'wc_ajax_add_to_cart_variable' ); 
  44.  
  45. // Add first checkbox option 
  46. $settings_slider[] = array( 
  47.  
  48. 'name' => __( 'Add Selection option to Category Page', 'text-domain' ),  
  49. 'desc_tip' => __( 'This will automatically insert variable selection options on product Category Archive Page', 'text-domain' ),  
  50. 'id' => 'wc_ajax_add_to_cart_variable_category_page',  
  51. 'type' => 'checkbox',  
  52. 'css' => 'min-width:300px;',  
  53. 'desc' => __( 'Enable Varition select option on Category Archive page', 'text-domain' ),  
  54.  
  55. ); 
  56.  
  57. $settings_slider[] = array( 'type' => 'sectionend', 'id' => 'wc_ajax_add_to_cart_variable' ); 
  58.  
  59. return $settings_slider; 
  60.  
  61. /** 
  62. * If not, return the standard settings 
  63. **/ 
  64.  
  65. } else { 
  66.  
  67. return $settings; 
  68.  
  69.  
  70.  
  71. $category_page = get_option( 'wc_ajax_add_to_cart_variable_category_page' ); 
  72.  
  73. if(isset($category_page) && $category_page == "yes" ) { 
  74.  
  75. if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) { 
  76.  
  77. function woocommerce_template_loop_add_to_cart() { 
  78. global $product; 
  79.  
  80. if ( $product ) { 
  81. $defaults = array( 
  82. 'quantity' => 1,  
  83. 'class' => implode( ' ', array_filter( array( 
  84. 'button',  
  85. 'product_type_' . $product->product_type,  
  86. $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',  
  87. $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '' 
  88. ) ) ) 
  89. ); 
  90.  
  91. $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product ); 
  92.  
  93. if ($product->product_type == "variable" ) { 
  94. woocommerce_variable_add_to_cart(); 
  95. else { 
  96. wc_get_template( 'loop/add-to-cart.php', $args ); 
  97.  
  98. function ajax_add_to_cart_script() { 
  99. wp_enqueue_script( 'add-to-cart-variation_ajax', plugins_url() . '/woocommerce-ajax-add-to-cart-for-variable-products/js/add-to-cart-variation.js', array('jquery'), '', true ); 
  100. add_action( 'wp_enqueue_scripts', 'ajax_add_to_cart_script', 99 ); 
  101.  
  102. /** AJAX add to cart variable added by Rishi Mehta - rishi@rcreators.com */ 
  103. add_action( 'wp_ajax_woocommerce_add_to_cart_variable_rc', 'woocommerce_add_to_cart_variable_rc_callback' ); 
  104. add_action( 'wp_ajax_nopriv_woocommerce_add_to_cart_variable_rc', 'woocommerce_add_to_cart_variable_rc_callback' ); 
  105.  
  106. function woocommerce_add_to_cart_variable_rc_callback() { 
  107.  
  108. ob_start(); 
  109.  
  110. $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); 
  111. $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] ); 
  112. $variation_id = $_POST['variation_id']; 
  113. $variation = $_POST['variation']; 
  114. $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); 
  115.  
  116. if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) ) { 
  117. do_action( 'woocommerce_ajax_added_to_cart', $product_id ); 
  118. if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) { 
  119. wc_add_to_cart_message( $product_id ); 
  120.  
  121. // Return fragments 
  122. WC_AJAX::get_refreshed_fragments(); 
  123. } else { 
  124. $this->json_headers(); 
  125.  
  126. // If there was an error adding to the cart, redirect to the product page to show any errors 
  127. $data = array( 
  128. 'error' => true,  
  129. 'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ) 
  130. ); 
  131. echo json_encode( $data ); 
  132. die(); 
  133. }  
  134. ?> 
.