settings_page_name = 'ajax-cart-autoupdate'; $this->settings_menu_name = 'Ajax Cart AutoUpdate'; // Initialize and register settings. add_action( 'admin_init', array( $this, 'register_settings' ) ); // Add settings page. add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); // Add settings link to plugins page. add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_settings_link' ) ); } // Load text domain. add_action( 'init', array( $this, 'load_textdomain' ) ); } public function load_textdomain() { load_plugin_textdomain( 'ajax-cart-autoupdate-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } public function register_settings() { register_setting( 'acau_settings', 'acau_settings', array( $this, 'sanitize' ) ); // ID / title / callback / page add_settings_section( 'configuration_section', null, array( $this, 'print_section_info' ), $this->settings_page_name ); // ID / title / callback / page / section add_settings_field( 'acau_update_delay', __( 'Update delay', 'ajax-cart-autoupdate-for-woocommerce' ), array( $this, 'acau_update_delay_callback' ), $this->settings_page_name, 'configuration_section' ); add_settings_field( 'acau_positive_qty', __( 'Cart minimum quantity', 'ajax-cart-autoupdate-for-woocommerce' ), array( $this, 'acau_positive_qty_callback' ), $this->settings_page_name, 'configuration_section' ); add_settings_field( 'acau_cart_notices_off', __( 'Cart page notices', 'ajax-cart-autoupdate-for-woocommerce' ), array( $this, 'acau_cart_notices_off_callback' ), $this->settings_page_name, 'configuration_section' ); } public function add_settings_page() { // This page will be under "Settings" add_options_page( 'Settings Admin', $this->settings_menu_name, 'manage_options', $this->settings_page_name, array( $this, 'create_settings_page' ) ); } /** * Get the option that is saved or the default. * * @param string $index. The option we want to get. */ public function acau_get_settings( $index = false ) { $defaults = array ( 'acau_update_delay' => 1000, 'acau_positive_qty' => true, 'acau_cart_notices_off' => true ); $settings = get_option( 'acau_settings', $defaults ); if ( $index && isset( $settings[ $index ] ) ) { return $settings[ $index ]; } return $settings; } public function create_settings_page() { $this->options = $this->acau_get_settings(); ?>

Ajax Cart AutoUpdate for WooCommerce

settings_page_name ); submit_button(); ?>
settings_page_name ) ) . '">' . __( 'Settings' ) . '' ), $links ); return $links; } /** * Sanitize each setting field as needed * * @param array $input Contains all settings fields as array keys */ public function sanitize( $input ) { $new_input = array(); if( isset( $input['acau_update_delay'] ) ) $new_input['acau_update_delay'] = absint( $input['acau_update_delay'] ); if( isset( $input['acau_positive_qty'] ) ) $new_input['acau_positive_qty'] = ( $input['acau_positive_qty'] == 1 ? 1 : 0 ); if( isset( $input['acau_cart_notices_off'] ) ) $new_input['acau_cart_notices_off'] = ( $input['acau_cart_notices_off'] == 1 ? 1 : 0 ); return $new_input; } /** * Print the Section text */ public function print_section_info() { return; // print 'Enter your settings below:'; } /** * Get the settings option array and print one of its values */ public function acau_update_delay_callback() { printf( '

%2$s

', isset( $this->options['acau_update_delay'] ) ? esc_attr( $this->options['acau_update_delay']) : '', __( 'Delay in miliseconds since last action affecting product quantity. Low values will trigger update when customers are in the middle of changing quantity. High values will make them wait and wonder why the cart doesn\'t recalculate.', 'ajax-cart-autoupdate-for-woocommerce' ) ); } public function acau_positive_qty_callback() { printf( '
', isset( $this->options['acau_positive_qty'] ) && ( 1 == $this->options['acau_positive_qty'] ) ? 'checked="checked" ':'', __( 'Change minimum product quantity on cart page from 0 to 1. If unchecked, 0 quantity is valid and such products are removed on cart update.', 'ajax-cart-autoupdate-for-woocommerce' ) ); } public function acau_cart_notices_off_callback() { printf( '
', isset( $this->options['acau_cart_notices_off'] ) && ( 1 == $this->options['acau_cart_notices_off'] ) ? 'checked="checked" ':'', __( 'Turn off notices on cart page. The most common are "Cart updated." and notice about product removed from cart.', 'ajax-cart-autoupdate-for-woocommerce' ) ); } } $acau_settingz_page = new ajax_cart_autoupdate_settings(); if( is_admin() ) { // Change plugin settings page CSS. add_action( 'admin_head-settings_page_ajax-cart-autoupdate', 'acau_settings_style' ); function acau_settings_style() { $my_style = " input[type=checkbox], input[type=radio] { margin: -4px 8px 0 0; } input, select { margin: 1px 1px 1px 0; } .form-table th { padding: 10px 10px 10px 0; width: 200px; } .form-table td { padding: 5px 10px; } .form-table td p { margin-bottom: 6px; } h2 { margin: 15px 0; } .acau_config_header { float: left; } .acau_admin_links { float: right; margin: 15px 50px 15px 0; vertical-align: middle; } "; echo ''; } } // Only if WooCommerce is active (doesn't work for Github installations which have version number in folder name). if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || ( get_site_option('active_sitewide_plugins') && array_key_exists( 'woocommerce/woocommerce.php', get_site_option('active_sitewide_plugins') ) ) ) { $args = array ( 'update_delay' => $acau_settingz_page->acau_get_settings('acau_update_delay'), 'positive_qty' => $acau_settingz_page->acau_get_settings('acau_positive_qty'), 'cart_notices_off' => $acau_settingz_page->acau_get_settings('acau_cart_notices_off'), ); // Run plugin. add_action('template_redirect', function() use ( $args ) { ajax_cart_autoupdate( $args ); }); function ajax_cart_autoupdate( $args ) { if (! is_cart() ) return; // Only if it's cart page. $plugin_slug = 'ajax-cart-autoupdate-for-woocommerce'; $plugin_short_slug = 'ajax-cart-autoupdate'; /*don't display default "Update cart" button, its behavior can still be triggered automatically, in WooCommerce 3.4.0 input element type was changed to button, class .button is present in all versions (at least from 2.6 when AJAX cart appeared in WooCommerce core) */ add_action('wp_head', 'acau_hide_update_cart_button', 20); // Enqueue js file. wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', array('jquery'), '', true); // js variable name => variable value $data = array ( 'acau_update_delay' => $args['update_delay'], ); // Localize to use php variables from plugin settings. wp_localize_script( $plugin_short_slug, 'acauPhpVars', $data ); // Cart page minimum qty = 1 instead of 0. if (1 == $args['positive_qty'] ) { add_filter( 'woocommerce_quantity_input_args', 'acau_cart_min_qty', 10, 2 ); } /* Don't display any notices, it includes: - "Cart updated." - removed cart item notice - the one when checkout session expires and cart items disappear (it duplicates no items in cart standard message). */ if (1 == $args['cart_notices_off'] ) { WC()->session->set( 'wc_notices', null ); } } function acau_hide_update_cart_button() { echo ""; } function acau_cart_min_qty( $args, $product ) { $args['min_value'] = 1; return $args; } } function acau_minimize($input) { // Remove whitespace $output = preg_replace('/\s*([{}|:;,])\s+/', '$1', $input); // Remove trailing whitespace at the start $output = preg_replace('/\s\s+(.*)/', '$1', $output); // Remove comments $output = preg_replace('#/\*.*?\*/#s', '', $input); return $output; }