diff --git a/assets/css/styles.css b/assets/css/styles.css old mode 100644 new mode 100755 diff --git a/assets/js/ipq_input_value_validation.js b/assets/js/ipq_input_value_validation.js index 63c45fd..785adf9 100755 --- a/assets/js/ipq_input_value_validation.js +++ b/assets/js/ipq_input_value_validation.js @@ -12,10 +12,10 @@ jQuery(document).ready( function($) { /* * Quantity Rule Validation - * - * If user enters a value that is out of bounds, + * + * If user enters a value that is out of bounds, * it will be auto corrected to a valid value. - */ + */ $(document).on('change', '.qty', function(e) { var $input = $(e.currentTarget); // Get values from input box @@ -32,84 +32,84 @@ jQuery(document).ready( function($) { return parseInt(mult); }; - + // Adjust default values if values are blank - if ( min == '' || typeof min == 'undefined' ) + if ( min == '' || typeof min == 'undefined' ) min = 1; - - if ( step == '' || typeof step == 'undefined') + + if ( step == '' || typeof step == 'undefined') step = 1; - + // Max Value Validation if ( +new_qty > +max && max != '' ) { new_qty = max; - + // Min Value Validation } else if ( +new_qty < +min && min != '' ) { new_qty = min; } - + // Calculate remainder step = step * multiplier(); new_qty = new_qty * multiplier(); min = min * multiplier(); max = max* multiplier(); - + var rem = ( new_qty - min ) % step; - + // Step Value Value Validation if ( rem != 0 ) { new_qty = +new_qty + (+step - +rem); - + // Max Value Validation if ( +new_qty > +max ) { new_qty = +new_qty - +step; } } - + // Set the new value $input.val( (new_qty/multiplier()).toFixed(stepOrig.getDecimals()) ); }); - + /* - * Make sure minimum equals value - * To Fix: when min = 0 and val = 1 + * Make sure minimum equals value + * To Fix: when min = 0 and val = 1 */ if ( $("body.single-product .qty").val() != $("body.single-product .qty").attr('min') && $("body.single-product .qty").attr('min') != '' ) { $("body.single-product .qty").val( $("body.single-product .qty").attr('min') ); } - + /* * Variable Product Support - * + * * Need to overwrite what WC changes with their js */ - + // Get localized Variables if ( typeof ipq_validation !== 'undefined' ) { var start_min = ipq_validation.min; var start_max = ipq_validation.max; var start_step = ipq_validation.step; } - + // Update input box after variaiton selects are blured $('.variations select').bind('blur',function() { - + // Update min if ( start_min != $('.qty').attr('min') && start_min != '' ) { $('.qty').attr('min', start_min ); } - + // Update max if ( start_max != $('.qty').attr('max') && start_max != '' ) { $('.qty').attr('max', start_max ); } - + // Update step if ( start_step != $('.qty').attr('step') && start_step != '' ) { $('.qty').attr('step', start_step ); } - + // Make sure intput value is in bounds if ( start_min > $('.qty').attr('value') && start_min != '' ) { $('.qty').attr('value', start_min ); diff --git a/includes/class-wcqu-advanced-rules.php b/includes/class-wcqu-advanced-rules.php index 9f096d8..7ef2641 100755 --- a/includes/class-wcqu-advanced-rules.php +++ b/includes/class-wcqu-advanced-rules.php @@ -1,42 +1,42 @@ -save_settings(); $url_parameters = 'updated=true'; @@ -44,41 +44,41 @@ public function page_loaded() { exit; } } - + /* * Update the settings based on the post values */ public function save_settings() { - + // Get Settings $settings = get_option( 'ipq_options' ); - - // Minimum Product Notification + + // Minimum Product Notification if ( isset( $_POST['ipq_show_qty_note'] ) and $_POST['ipq_show_qty_note'] == 'on' ) { $settings['ipq_show_qty_note'] = 'on'; } else { $settings['ipq_show_qty_note'] = ''; } - - // Minimum Note Text + + // Minimum Note Text if ( isset( $_POST['ipq_qty_text'] ) and $_POST['ipq_qty_text'] != '' ) { $settings['ipq_qty_text'] = stripslashes( $_POST['ipq_qty_text'] ); } else { $settings['ipq_qty_text'] = ''; } - + // Minimum Note Position if ( isset( $_POST['ipq_show_qty_note_pos'] ) and $_POST['ipq_show_qty_note_pos'] == 'below' ) { $settings['ipq_show_qty_note_pos'] = 'below'; } else { $settings['ipq_show_qty_note_pos'] = 'above'; } - + // Minimum Note Class if ( isset( $_POST['ipq_qty_class'] ) ) { $settings['ipq_qty_class'] = stripslashes( $_POST['ipq_qty_class'] ); - } - + } + // Active Rule if ( isset( $_POST['ipq_site_rule_active'] ) and $_POST['ipq_site_rule_active'] == 'on' ) { $settings['ipq_site_rule_active'] = 'on'; @@ -89,19 +89,19 @@ public function save_settings() { if ( isset( $_POST['ipq_site_min'] )) { $min = wcqu_validate_number( $_POST['ipq_site_min'] ); } - + if ( isset( $_POST['ipq_site_step'] )) { $step = wcqu_validate_number( $_POST['ipq_site_step'] ); } - + if ( isset( $_POST['ipq_site_max'] )) { $max = wcqu_validate_number( $_POST['ipq_site_max'] ); } - + if ( isset( $_POST['ipq_site_min_oos'] )) { $min_oos = wcqu_validate_number( $_POST['ipq_site_min_oos'] ); } - + if ( isset( $_POST['ipq_site_max_oos'] )) { $max_oos = wcqu_validate_number( $_POST['ipq_site_max_oos'] ); } @@ -112,14 +112,14 @@ public function save_settings() { $min = $step; } } - + // Make sure min <= max if ( isset( $step ) and isset( $max ) ) { if ( $min > $max and $max != '' and $max != 0 ) { $max = $min; } } - + // Make sure min_oos <= max and max_oos if ( isset( $min_oos ) and $min_oos != 0 ) { if ( isset( $max_oos ) and $max_oos != 0 and @@ -135,23 +135,23 @@ public function save_settings() { // Site Minimum if ( isset( $_POST['ipq_site_min'] ) ) { $settings['ipq_site_min'] = strip_tags( $min ); - } - - // Site Step + } + + // Site Step if ( isset( $_POST['ipq_site_step'] ) ) { $settings['ipq_site_step'] = strip_tags( $step ); - } - + } + // Site Max if( isset( $_POST['ipq_site_max'] )) { $settings['ipq_site_max'] = strip_tags( $max ); } - + // Site Min OOS if( isset( $_POST['ipq_site_min_oos'] )) { $settings['ipq_site_min_oos'] = strip_tags( $min_oos ); } - + // Site Max if( isset( $_POST['ipq_site_max_oos'] )) { $settings['ipq_site_max_oos'] = strip_tags( $max_oos ); @@ -161,12 +161,12 @@ public function save_settings() { $updated = update_option( 'ipq_options', $settings ); } - + /** * Advanced Rules Page Content */ public function advanced_rules_page_content() { - + $options = get_option( 'ipq_options' ); if ($options == false) { @@ -175,119 +175,119 @@ public function advanced_rules_page_content() { extract($options); $qty_text_default = "Minimum Qty: %MIN%"; - + ?> -

Advanced Rules

+

- + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + - + - + - - - + - - + - + - - + +
Activate Site Wide Rules? />
Site Wide Product Minimum ' step="any" />
Site Wide Product Maximum ' step="any" />
Site Wide Product Minimum Out of Stock ' step="any" />
Site Wide Product Maximum Out of Stock ' step="any" />
Site Wide Step Value ' step="any" />
- *Note - the minimum value must be greater then or equal to the step value. +
Show Quantity Notification on Product Page? + />
Notification Position
Quantity Notification Text + ' />
%MIN% = Minimum Value
- %MAX% = Maximum Value
- %STEP% = Step Value +

+
+
Custom Quantity Note HTML Class ' />
Message ShortcodePlace in product content to display message [wpbo_quantity_message][wpbo_quantity_message]",'qau'); ?>
- +

- - product_type != 'simple' ) { return $args; } @@ -40,40 +40,44 @@ public function woocommerce_loop_add_to_cart_args( $args, $product ) { // Get Value from Rule $min = wcqu_get_value_from_rule( 'min', $product, $rule ); + $min = isset($min['min']) ? $min['min'] : 1; + $step = wcqu_get_value_from_rule( 'step', $product, $rule ); - + $step = isset($step['step']) ? $step['step'] : 1; + if(!$min && $step > 0){ $args['quantity'] = $step; - } + } else if($min > 0){ $args['quantity'] = $min; } - - + + return $args; } /* * Filter Minimum Quantity Value for Input Boxes for Cart * - * @access public + * @access public * @param int default * @param obj product * @return int step * - */ + */ public function input_min_value( $default, $product ) { - // Return Defaults if it isn't a simple product + // Return Defaults if it isn't a simple product if( $product->product_type != 'simple' ) { return $default; } - + // Get Rule $rule = wcqu_get_applied_rule( $product ); // Get Value from Rule $min = wcqu_get_value_from_rule( 'min', $product, $rule ); + // Return Value if ( $min == '' or $min == null or (isset($min['min']) and $min['min'] == "")) { return $default; @@ -81,29 +85,29 @@ public function input_min_value( $default, $product ) { return $min; } } - + /* * Filter Maximum Quantity Value for Input Boxes for Cart * - * @access public + * @access public * @param int default * @param obj product * @return int step * - */ - public function input_max_value( $default, $product ) { - + */ + public function input_max_value( $default, $product ) { + // Return Defaults if it isn't a simple product if( $product->product_type != 'simple' ) { return $default; } - + // Get Rule $rule = wcqu_get_applied_rule( $product ); - + // Get Value from Rule $max = wcqu_get_value_from_rule( 'max', $product, $rule ); - + // Return Value if ( $max == '' or $max == null or (isset($max['max']) and $max['max'] == "")) { return $default; @@ -111,48 +115,48 @@ public function input_max_value( $default, $product ) { return $max; } } - + /* * Filter Step Quantity Value for Input Boxes woocommerce_quantity_input_step for Cart * - * @access public + * @access public * @param int default * @param obj product * @return int step * - */ + */ public function input_step_value( $default, $product ) { - + // Return Defaults if it isn't a simple product if( $product->product_type != 'simple' ) { return $default; } - + // Get Rule $rule = wcqu_get_applied_rule( $product ); - + // Get Value from Rule $step = wcqu_get_value_from_rule( 'step', $product, $rule ); - + // Return Value if ( $step == '' or $step == null or (isset($step['step']) and $step['step'] == "")) { return $default; } else { return isset($step['step']) ? $step['step'] : $step; } - } - + } + /* * Filter Step, Min and Max for Quantity Input Boxes on product pages * - * @access public + * @access public * @param array args * @param obj product * @return array vals * - */ + */ public function input_set_all_values( $args, $product ) { - + // Return Defaults if it isn't a simple product /* Commented out to allow for grouped and variable products * on their product pages @@ -170,40 +174,52 @@ public function input_set_all_values( $args, $product ) { if ( $values == null ) { return $args; } - + $vals = array(); $vals['input_name'] = 'quantity'; - - // Check if the product is out of stock + + // Check if the product is out of stock $stock = $product->get_stock_quantity(); - - // Check stock status and if Out try Out of Stock value + $on_sale = $product->is_on_sale(); + // Check stock status and if Out try Out of Stock value if ( strlen( $stock ) != 0 and $stock <= 0 and isset( $values['min_oos'] ) and $values['min_oos'] != '' ) { $args['min_value'] = $values['min_oos']; - - // Otherwise just check normal min - } elseif ( $values['min_value'] != '' ) { + // Otherwise just check normal min + } + elseif ( $on_sale and isset( $values['min_sale'] ) and $values['min_sale'] != '' ) { + $args['min_value'] = $values['min_sale']; + } + elseif ( $values['min_value'] != '' ) { $args['min_value'] = $values['min_value']; - - // If no min, try step - } elseif ( $values['min_value'] == '' and $values['step'] != '' ) { + + // If no min, try step + } + elseif ( $values['min_value'] == '' and $values['step'] != '' ) { $args['min_value'] = $values['step']; - } - - // Check stock status and if Out try Out of Stock value + } + + // Check stock status and if Out try Out of Stock value if ( $stock <= 0 and isset( $values['min_oos'] ) and $values['max_oos'] != '' ) { $args['max_value'] = $values['max_oos']; - - // Otherwise just check normal max + } elseif ( $on_sale and isset( $values['max_sale'] ) and $values['max_sale'] != '' ) { + $args['max_value'] = $values['max_sale']; + // Otherwise just check normal max } elseif ($values['max_value'] != '' ) { $args['max_value'] = $values['max_value']; } - + // Set step value if ( $values['step'] != '' ) { - $args['step'] = $values['step']; + //Fix step if near to Out of Stock + if(strlen( $stock ) != 0 and $stock > 0 && $values['step'] > $stock){ + $args['step'] = $stock; + $args['min_value'] = $stock; + $args['max_value'] = $stock; + } else { + $args['step'] = $values['step']; + } } - + return $args; } diff --git a/includes/class-wcqu-post-type.php b/includes/class-wcqu-post-type.php index c9d2029..2f75269 100755 --- a/includes/class-wcqu-post-type.php +++ b/includes/class-wcqu-post-type.php @@ -1,57 +1,55 @@ - 'Quantity Rules', - 'singular_name' => 'Quantity Rule', - 'add_new' => 'Add New', - 'add_new_item' => 'Add New Rule', - 'edit_item' => 'Edit Rule', - 'new_item' => 'New Rule', - 'all_items' => 'All Rules', - 'view_item' => 'View Rule', - 'search_items' => 'Search Ruless', - 'not_found' => 'No rules found', - 'not_found_in_trash' => 'No rules found in Trash', + 'name' => __('Quantity Rules','qau'), + 'singular_name' => __('Quantity Rule','qau'), + 'add_new' => __('Add New','qau'), + 'add_new_item' => __('Add New Rule','qau'), + 'edit_item' => __('Edit Rule','qau'), + 'new_item' => __('New Rule','qau'), + 'all_items' => __('All Rules','qau'), + 'view_item' => __('View Rule','qau'), + 'search_items' => __('Search Ruless','qau'), + 'not_found' => __('No rules found','qau'), + 'not_found_in_trash' => __('No rules found in Trash','qau'), 'parent_item_colon' => '', - 'menu_name' => 'Quantity Rules' + 'menu_name' => __('Quantity Rules','qau'), ); - + $args = array( 'labels' => $labels, 'public' => false, @@ -67,21 +65,21 @@ public function quantity_rule_init() { 'supports' => array( 'title' ), 'taxonomies' => array(), ); - + register_post_type( 'quantity-rule', $args ); } - + /* * Register Custom Columns for List View - */ + */ public function quantity_rule_columns( $column ) { - + unset( $column['date'] ); - + $new_columns['priority'] = __('Priority'); $new_columns['min'] = __('Minimum'); $new_columns['max'] = __('Maximum'); - $new_columns['step'] = __('Step Value'); + $new_columns['step'] = __('Step Value'); $new_columns['cats'] = __('Categories'); $new_columns['product_tags'] = __('Tags'); $new_columns['roles'] = __('Roles'); @@ -89,174 +87,182 @@ public function quantity_rule_columns( $column ) { return array_merge( $column, $new_columns ); } - + /* * Get Custom Columns Values for List View - */ + */ public function manage_quantity_rule_columns($column_name, $id) { - + switch ($column_name) { - + case 'priority': echo get_post_meta( $id, '_priority', true ); break; - + case 'min': echo get_post_meta( $id, '_min', true ); break; - + case 'max': echo get_post_meta( $id, '_max', true ); break; - + case 'step': - echo get_post_meta( $id, '_step', true ); + echo get_post_meta( $id, '_step', true ); break; - + case 'cats': $cats = get_post_meta( $id, '_cats', false); - if ( $cats != false and count( $cats[0] ) > 0 ) { + if ( $cats != false and count( $cats[0] ) > 0 ) { foreach ( $cats[0] as $cat ){ - - $taxonomy = 'product_cat'; + + $taxonomy = 'product_cat'; $term = get_term_by( 'id', $cat, $taxonomy ); - $link = get_term_link( $term ); - - echo "" . $term->name . "
"; + $link = get_term_link( $term ); + + echo "" . $term->name . "
"; } - } - break; - + } + break; + case 'product_tags': $tags = get_post_meta( $id, '_tags', false); - if ( $tags != null and count( $tags[0] ) > 0) { + if ( $tags != null and count( $tags[0] ) > 0) { foreach ( $tags[0] as $tag ){ - - $taxonomy = 'product_tag'; + + $taxonomy = 'product_tag'; $term = get_term_by( 'id', $tag, $taxonomy ); - $link = get_term_link( $term ); - - echo "" . $term->name . "
"; + $link = get_term_link( $term ); + + echo "" . $term->name . "
"; } - } + } break; - + case 'roles': $roles = get_post_meta( $id, '_roles', false); - if ( $roles != null and count( $roles[0] ) > 0) { + if ( $roles != null and count( $roles[0] ) > 0) { foreach ( $roles[0] as $role ){ - echo ucfirst( $role ) . "
"; + echo ucfirst( $role ) . "
"; } - } + } break; - + default: break; - } - } - + } + } + /* * Make Custom Columns Sortable - */ - public function sortable_quantity_rule_columns( $columns ) { - + */ + public function sortable_quantity_rule_columns( $columns ) { + $columns['priority'] = __('Priority'); $columns['min'] = __('Minimum'); $columns['max'] = __('Maximum'); $columns['step'] = __('Step Value'); - - return $columns; - } - + + return $columns; + } + /* * Register and Create Rule Options Meta Box for Quantity Rules - */ + */ public function quantity_rule_meta_init() { add_meta_box( - 'wpbo-quantity-rule-meta', - 'Set Quantity Rule Options', - array( $this, 'quantity_rule_meta' ), - 'quantity-rule', - 'normal', + 'wpbo-quantity-rule-meta', + 'Set Quantity Rule Options', + array( $this, 'quantity_rule_meta' ), + 'quantity-rule', + 'normal', 'high' ); } - + public function quantity_rule_meta( $post ) { - + $min = get_post_meta( $post->ID, '_min', true); $max = get_post_meta( $post->ID, '_max', true); $min_oos = get_post_meta( $post->ID, '_min_oos', true ); - $max_oos = get_post_meta( $post->ID, '_max_oos', true ); + $max_oos = get_post_meta( $post->ID, '_max_oos', true ); + $min_sale = get_post_meta( $post->ID, '_min_sale', true ); + $max_sale = get_post_meta( $post->ID, '_max_sale', true ); $step = get_post_meta( $post->ID, '_step', true); $priority = get_post_meta( $post->ID, '_priority', true); - + // Create Nonce Field wp_nonce_field( plugin_basename( __FILE__ ), '_wpbo_rule_value_nonce' ); - + ?>
- + - - + + - - + + - - + + - - + + + + + + + + - - - + + +
-

*Note - the minimum value must be greater then or equal to the step value.
- *Note - The rule with the lowest priority number will be used if multiple rules are applied to a single product.

-
+

+ ID, '_cats', false); - + if ( $cats != null ) { $cats = $cats[0]; } - + // Get all possible categories $tax_name = 'product_cat'; - - $args = array( + + $args = array( 'parent' => 0, 'hide_empty' => false ); - + $terms = get_terms( $tax_name, $args ); - + if ( $terms ){ - + // Create Nonce Field wp_nonce_field( plugin_basename( __FILE__ ), '_wpbo_tax_nonce' ); - + echo ''; } } - + /* * Will Recursivly Print all Product Categories with heirarcy included */ - public function print_tax_inputs( $term, $taxonomy_name, $cats, $level ) { - + public function print_tax_inputs( $term, $taxonomy_name, $cats, $level ) { + // Echo Single Item ?>
  • term_id, $cats )) echo 'checked="checked"' ?> />name; ?>
  • - term_id, $taxonomy_name ); - + // Continue to print children if they exist if ( $children ){ echo ''; } } - + /* * Allow users to apply rules by tags - */ + */ public function quantity_rule_tag_init() { - add_meta_box( - 'wpbo-quantity-rule-tag-meta', - 'Product Tags', - array( $this, 'quantity_rule_tag_meta' ), - 'quantity-rule', - 'normal', + add_meta_box( + 'wpbo-quantity-rule-tag-meta', + __('Product Tags','qau'), + array( $this, 'quantity_rule_tag_meta' ), + 'quantity-rule', + 'normal', 'high' ); } - + public function quantity_rule_tag_meta( $post ) { - + // Get all Tags $args = array( - 'orderby' => 'name', + 'orderby' => 'name', 'order' => 'ASC', - 'hide_empty' => false, - - ); + 'hide_empty' => false, + + ); $tags = get_terms( 'product_tag', $args ); - + $included_tags = get_post_meta( $post->ID, '_tags'); if ( $included_tags != false ){ $included_tags = $included_tags[0]; @@ -328,7 +334,7 @@ public function quantity_rule_tag_meta( $post ) { // If Tags exists, show them all if ( $tags ) { - + // Create Nonce Field wp_nonce_field( plugin_basename( __FILE__ ), '_wpbo_tag_nonce' ); @@ -341,30 +347,30 @@ public function quantity_rule_tag_meta( $post ) { - get_names(); - + // Get applied roles $applied_roles = get_post_meta( $post->ID, '_roles' ); if ( $applied_roles != false ){ @@ -372,7 +378,7 @@ public function quantity_rule_role( $post ) { } // Create Nonce Field wp_nonce_field( plugin_basename( __FILE__ ), '_wpbo_role_nonce' ); - + if ( $roles ): ?> WooCommerce Thumbnail Input Quantity installed.

    Installation is highly recommended as it shows an input box (with your quantity rules) from all product thumbnails such as on the shop page and in the related prodcuts section."; - - } - - /* - * Register and Create Meta Box to encourage user to install our thumbnail plugin - */ - public function company_notice() { - - add_meta_box( - 'wpqu-company-notice', - 'Message from WP BackOffice', - array( $this, 'company_notice_meta' ), - 'quantity-rule', - 'side', - 'low' - ); - } - - public function company_notice_meta( $post ) { - - ?> - -

    - WooCommerce Hosting, Customization, Support -

    - WooCommerce Thumbnail Input Quantity installed.

    Installation is highly recommended as it shows an input box (with your quantity rules) from all product thumbnails such as on the shop page and in the related prodcuts section.','qau'); } - + /* * Register and Create Meta Box to encourage user to install our thumbnail plugin - */ + */ public function rate_us_notice() { - - add_meta_box( - 'wpbo-additional-info', - 'Additional Information', - array( $this, 'additional_info_notice_meta' ), - 'quantity-rule', - 'side', + + add_meta_box( + 'wpbo-additional-info', + __("Additional Information",'qau'), + array( $this, 'additional_info_notice_meta' ), + 'quantity-rule', + 'side', 'low' ); } - + public function additional_info_notice_meta( $post ) { ?>
    -

    Enjoy this plugin?

    - Rate us on Wordpress.org! - -

    Need Support?

    - Visit our Support Forum +

    + + +

    +
    - get_names(); foreach ( $roles as $slug => $name ) { delete_transient( 'ipq_rules_' . $slug ); } - + //Also delete the guest transient, which is not a role delete_transient( 'ipq_rules_guest' ); @@ -501,188 +480,211 @@ public function save_quantity_rule_meta( $post_id ) { if( isset( $_POST['min'] ) ) { $min = $_POST['min']; } - - // Update Step + + // Update Step if ( isset( $_POST['step'] ) and isset( $min ) ) { if ( $min < $_POST['step'] && $min > 0) { $min = $_POST['step']; } } - + // Get Min Out of Stock if( isset( $_POST['min_oos'] ) ) { $min_oos = $_POST['min_oos']; update_post_meta( $post_id, '_min_oos', stripslashes( $min_oos ) ); } - + + if( isset( $_POST['min_sale'] ) ) { + $min_sale = $_POST['min_sale']; + update_post_meta( $post_id, '_min_sale', stripslashes( $min_sale ) ); + } + // Update Min if ( isset( $min ) ) { update_post_meta( $post_id, '_min', stripslashes( $min ) ); } - + // Update Max if ( isset( $_POST['max'] ) ) { $max = $_POST['max']; - + // Validate Max is not less then Min if ( isset( $min ) and $max < $min and $max != 0 ) { $max = $min; } - + update_post_meta( $post_id, '_max', wcqu_validate_number( $max ) ); } - + // Update Max OOS if ( isset( $_POST['max_oos'] ) ) { $max_oos = $_POST['max_oos']; - + // Max must be bigger then min if ( $max_oos != '' and isset( $min_oos ) and $min_oos != 0 ) { if ( $min_oos > $max_oos ) $max_oos = $min_oos; - + } elseif ( $max_oos != '' and isset( $min ) and $min != 0 ){ if ( $min > $max_oos ) { $max_oos = $min; } } - + update_post_meta( $post_id, '_max_oos', wcqu_validate_number( $max_oos ) ); } - + + // Update Max Sale + if ( isset( $_POST['max_sale'] ) ) { + $max_sale = $_POST['max_sale']; + + // Max must be bigger then min + if ( $max_sale != '' and isset( $min_sale ) and $min_sale != 0 ) { + if ( $min_sale > $max_sale ) + $max_sale = $min_sale; + + } elseif ( $max_sale != '' and isset( $min ) and $min != 0 ){ + if ( $min > $max_sale ) { + $max_sale = $min; + } + } + + update_post_meta( $post_id, '_max_sale', wcqu_validate_number( $max_sale ) ); + } + // Update Step if ( isset( $_POST['step'] ) ) { update_post_meta( $post_id, '_step', wcqu_validate_number( $_POST['step'] ) ); } - + // Update Priority if ( isset( $_POST['priority'] ) ) { update_post_meta( $post_id, '_priority', wcqu_validate_number( $_POST['priority'] ) ); } - + } - + /* * Save Rule Taxonomy Values - */ + */ public function save_quantity_rule_taxes( $post_id ) { - + // Validate Post Type if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'quantity-rule' ) { return; } - + // Validate User if ( !current_user_can( 'edit_post', $post_id ) ) { return; } - + // Verify Nonce if ( ! isset( $_POST["_wpbo_tax_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_tax_nonce"], plugin_basename( __FILE__ ) ) ) { return; } - + // Check which Categories have been selected $tax_name = 'product_cat'; $args = array( 'hide_empty' => false ); $terms = get_terms( $tax_name, $args ); $cats = array(); - + // See which terms were included foreach ( $terms as $term ) { $term_name = '_wpbo_cat_' . $term->term_id; if ( isset( $_POST[ $term_name ] ) and $_POST[ $term_name ] == 'on' ) { - array_push( $cats, $term->term_id ); - } + array_push( $cats, $term->term_id ); + } } - + // Add them to the post meta delete_post_meta( $post_id, '_cats' ); update_post_meta( $post_id, '_cats', $cats, false ); - } - + } + /* * Save Rule Tag Values - */ + */ public function save_quantity_rule_tags( $post_id ) { - + // Validate Post Type if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'quantity-rule' ) { return; } - + // Validate User if ( !current_user_can( 'edit_post', $post_id ) ) { return; } - + // Verify Nonce if ( ! isset( $_POST["_wpbo_tag_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_tag_nonce"], plugin_basename( __FILE__ ) ) ) { return; } - + // Get all Tags $args = array ( - 'orderby' => 'name', + 'orderby' => 'name', 'order' => 'ASC', - 'hide_empty' => false, - ); + 'hide_empty' => false, + ); $tags = get_terms( 'product_tag', $args ); - + $tags_included = array(); - + // If the tags are set, loop through them if ( $tags ) { foreach ( $tags as $tag ) { - + $tag_name = '_wpbo_tag_' . $tag->term_id; if ( isset( $_POST[ $tag_name ] ) and $_POST[ $tag_name ] == 'on' ) { - array_push( $tags_included, $tag->term_id ); - } + array_push( $tags_included, $tag->term_id ); + } } - + // Add them to the post meta delete_post_meta( $post_id, '_tags' ); update_post_meta( $post_id, '_tags', $tags_included, false ); } } - + /* * Save Rule Role Values - */ + */ public function save_quantity_rule_roles( $post_id ) { - + // Validate Post Type if ( ! isset( $_POST['post_type'] ) or $_POST['post_type'] !== 'quantity-rule' ) { return; } - + // Validate User if ( !current_user_can( 'edit_post', $post_id ) ) { return; } - + // Verify Nonce if ( ! isset( $_POST["_wpbo_role_nonce"] ) or ! wp_verify_nonce( $_POST["_wpbo_role_nonce"], plugin_basename( __FILE__ ) ) ) { return; } - + // Get available user roles global $wp_roles; $roles = $wp_roles->get_names(); $applied_roles = array(); - + // Loop through roles foreach ( $roles as $slug => $name ) { $role_name = '_wpbo_role_' . $slug; - + // If role is set add it to the applied list if ( isset( $_POST[ $role_name ] ) and $_POST[ $role_name ] == 'on' ) { array_push( $applied_roles, $slug ); } } - + // If guest role is set add it to the applied list if ( isset( $_POST[ '_wpbo_role_guest' ] ) and $_POST[ '_wpbo_role_guest' ] == 'on' ) { array_push( $applied_roles, 'guest' ); @@ -692,8 +694,8 @@ public function save_quantity_rule_roles( $post_id ) { // Add them to the post meta delete_post_meta( $post_id, '_roles' ); update_post_meta( $post_id, '_roles', $applied_roles, false ); - - } + + } } endif; diff --git a/includes/class-wcqu-product-meta-box.php b/includes/class-wcqu-product-meta-box.php index d3671f8..156fa8b 100755 --- a/includes/class-wcqu-product-meta-box.php +++ b/includes/class-wcqu-product-meta-box.php @@ -1,12 +1,12 @@ -post_type == 'product' ) { - + $product = get_product( $post->ID ); $unsupported_product_types = array( 'external', 'grouped' ); if ( ! in_array( $product->product_type, $unsupported_product_types ) ) { - + add_meta_box( - 'wpbo_product_info', - __('Product Quantity Rules', 'woocommerce'), - array( $this, 'product_meta_box_content' ), - 'product', - 'normal', - 'high' + 'wpbo_product_info', + __('Product Quantity Rules', 'qau'), + array( $this, 'product_meta_box_content' ), + 'product', + 'normal', + 'high' ); } } } - + /* * Display Rule Meta Box */ @@ -43,67 +43,66 @@ function product_meta_box_content( $post ) { global $product; global $woocommerce; global $wp_roles; - + // Get the product and see what rules are being applied - $pro = get_product( $post ); - + $pro = wc_get_product( $post ); + // Get applied rules by user role $roles = $wp_roles->get_names(); $roles['guest'] = "Guest"; - + $rules_by_role = array(); - + + $rule = null; // Loop through roles foreach ( $roles as $slug => $name ) { - $rule = wcqu_get_applied_rule( $pro, $slug ); + $newRule = wcqu_get_applied_rule( $pro, $slug ); - if ( $rule == 'inactive' or $rule == 'override' or $rule == 'sitewide' ) + // Set the latest $rule if its not null. This will + // be used later below in the if statements + $rule = $newRule ? $newRule : $rule; + + if ( $newRule == 'inactive' or (isset($newRule->rule_status) and $newRule->rule_status == 'override') or $newRule == 'sitewide' ) continue; - - $rules_by_role[$name] = $rule; - } - - // $rule_result = wcqu_get_applied_rule( $pro ); - - /* - // If there isn't a rule mark rule as null, otherwise get the id - if ( $rule_result != 'inactive' and $rule_result != 'override' ) { - $rule = $rule_result; - $values = wcqu_get_value_from_rule( 'all', $pro, $rule ); - } else { - $rule = $rule_result; + + $rules_by_role[$name] = $newRule; } - */ - + // Display Rule Being Applied if ( $rule == 'inactive' ) { - echo "
    No rule is being applied becasue you've deactivated the plugin for this product.
    "; - - } elseif ( $rule == 'override' ) { - echo "
    The values below are being used because you've chosen to override any applied rules for this product.
    "; - + echo "
    ". + __("No rule is being applied becasue you've deactivated the plugin for this product.", 'qau'). + "
    "; + + } elseif ( isset($rule->rule_status) and $rule->rule_status == 'override') { + echo "
    ". + __("The values below are being used because you've chosen to override any applied rules for this product.", 'qau'). + "
    "; + } elseif ( $rule == 'sitewide' ) { ?>
    - Active Rule: + '> - Site Wide Rule + - Show/Hide Active Rules by Role ▼ +
    - -
    + +
    - - - - - - - - + + + + + + + + + + @@ -112,39 +111,48 @@ function product_meta_box_content( $post ) { + +
    RoleRuleMinMaxMin OOSMax OOSStepPriority
    All
    - post_title ) or $rule->post_title == null ) { - echo "
    No rule is currently being applied to this product.
    "; - + post_title ) or $rule->post_title == null) ) { + echo "
    ". + __("No rule is currently being applied to this product.", 'qau'). + "
    "; + } else { ?>
    - Active Rule: - - post_title ?> - - Show/Hide Active Rules by Role ▼ + + + + + post_title ?> + + +
    - -
    + +
    - - - - - - - - + + + + + + + + + + $rule ): ?> @@ -153,11 +161,13 @@ function product_meta_box_content( $post ) { - + + + - + @@ -171,7 +181,7 @@ function product_meta_box_content( $post ) { ID, '_wpbo_deactive', true ); $step = get_post_meta( $post->ID, '_wpbo_step', true ); @@ -180,99 +190,109 @@ function product_meta_box_content( $post ) { $over = get_post_meta( $post->ID, '_wpbo_override', true ); $min_oos = get_post_meta( $post->ID, '_wpbo_minimum_oos', true ); $max_oos = get_post_meta( $post->ID, '_wpbo_maximum_oos', true ); - + $min_sale = get_post_meta( $post->ID, '_wpbo_minimum_sale', true ); + $max_sale = get_post_meta( $post->ID, '_wpbo_maximum_sale', true ); + // Create Nonce Field wp_nonce_field( plugin_basename( __FILE__ ), '_wpbo_product_rule_nonce' ); - - // Print the form ?> + + // Print the form ?>
    /> - Deactivate Quantity Rules on this Product? - + + /> - Override Quantity Rules with Values Below - + + > - + - - + + - - + + - - + + - - + + - - Note* Maximum values must be larger then minimums + + + + + + + +
    = step */ /* if ( isset( $step ) and isset( $min ) ) { @@ -281,87 +301,139 @@ public function save_quantity_meta_data( $post_id ) { } } */ - - if( isset( $_POST['_wpbo_step'] )) { - update_post_meta( - $post_id, - '_wpbo_step', + + //TODO: Per azzerare occorre mettere 0 e non vuoto, vuoto non entra per non + // scrivere dati inutili in wp_postmeta. + if( isset( $_POST['_wpbo_step'] ) && ! empty($_POST['_wpbo_step'])) { + update_post_meta( + $post_id, + '_wpbo_step', strip_tags( wcqu_validate_number( $_POST['_wpbo_step'] ) ) ); } - - if( isset( $_POST['_wpbo_minimum'] )) { + + if( isset( $_POST['_wpbo_minimum'] ) && ! empty($_POST['_wpbo_minimum'])) { if ( $min != 0 ) { $min = wcqu_validate_number( $min ); } - update_post_meta( - $post_id, - '_wpbo_minimum', + update_post_meta( + $post_id, + '_wpbo_minimum', strip_tags( $min ) ); } - + /* Make sure Max > Min */ - if( isset( $_POST['_wpbo_maximum'] )) { + if( isset( $_POST['_wpbo_maximum'] ) && ! empty($_POST['_wpbo_maximum']) ) { $max = $_POST['_wpbo_maximum']; if ( isset( $min ) and $max < $min and $max != 0 ) { $max = $min; } - - update_post_meta( - $post_id, - '_wpbo_maximum', + + update_post_meta( + $post_id, + '_wpbo_maximum', strip_tags( wcqu_validate_number( $max ) ) ); } - + + + // Update Out of Stock Minimum - if( isset( $_POST['_wpbo_minimum_oos'] )) { + if( isset( $_POST['_wpbo_minimum_oos'] ) && ! empty($_POST['_wpbo_minimum_oos']) ) { $min_oos = stripslashes( $_POST['_wpbo_minimum_oos'] ); - + if ( $min_oos != 0 ) { $min_oos = wcqu_validate_number( $min_oos ); } - update_post_meta( - $post_id, - '_wpbo_minimum_oos', + update_post_meta( + $post_id, + '_wpbo_minimum_oos', strip_tags( $min_oos ) ); } - + // Update Out of Stock Maximum - if( isset( $_POST['_wpbo_maximum_oos'] )) { - + if( isset( $_POST['_wpbo_maximum_oos'] ) && ! empty($_POST['_wpbo_maximum_oos'])) { + $max_oos = stripslashes( $_POST['_wpbo_maximum_oos'] ); - + // Allow the value to be unset if ( $max_oos != '' ) { - - // Validate the number + + // Validate the number if ( $max_oos != 0 ) { $max_oos = wcqu_validate_number( $max_oos ); - } - + } + // Max must be bigger then min if ( isset( $min_oos ) and $min_oos != 0 ) { if ( $min_oos > $max_oos ) $max_oos = $min_oos; - + } elseif ( isset( $min ) and $min != 0 ){ if ( $min > $max_oos ) { $max_oos = $min; } } - } - - update_post_meta( - $post_id, - '_wpbo_maximum_oos', + } + + update_post_meta( + $post_id, + '_wpbo_maximum_oos', strip_tags( $max_oos ) ); - } - + } + + // Update Sale Minimum + if( isset( $_POST['_wpbo_minimum_sale'] ) && ! empty($_POST['_wpbo_minimum_sale'])) { + $min_sale = stripslashes( $_POST['_wpbo_minimum_sale'] ); + + if ( $min_sale != 0 ) { + $min_sale = wcqu_validate_number( $min_sale ); + } + update_post_meta( + $post_id, + '_wpbo_minimum_sale', + strip_tags( $min_sale ) + ); + } + + // Update Sale Maximum + if( isset( $_POST['_wpbo_maximum_sale'] ) && ! empty($_POST['_wpbo_maximum_sale'])) { + + $max_sale = stripslashes( $_POST['_wpbo_maximum_sale'] ); + + // Allow the value to be unset + if ( $max_sale != '' ) { + + // Validate the number + if ( $max_sale != 0 ) { + $max_sale = wcqu_validate_number( $max_sale ); + } + + // Max must be bigger then min + if ( isset( $min_sale ) and $min_sale != 0 ) { + if ( $min_sale > $max_sale ) + $max_sale = $min_sale; + + } elseif ( isset( $min ) and $min != 0 ){ + if ( $min > $max_sale ) { + $max_sale = $min; + } + } + } + + update_post_meta( + $post_id, + '_wpbo_maximum_sale', + strip_tags( $max_sale ) + ); + + } + + } } diff --git a/includes/class-wcqu-product-unit.php b/includes/class-wcqu-product-unit.php index b816971..b12c154 100644 --- a/includes/class-wcqu-product-unit.php +++ b/includes/class-wcqu-product-unit.php @@ -48,7 +48,7 @@ public function get_unit_for_product( $product_id, $default = null ) { $unit = get_post_meta( $product_id, 'unit', true ); $unit = $unit ? $unit : $default; - return $unit ? apply_filters( 'wciu_default_price_suffix', __( $unit, 'woocommerce' ) ) : ''; + return $unit ? apply_filters( 'wciu_default_price_suffix', __( $unit, 'qau' ) ) : ''; } /** @@ -61,7 +61,7 @@ public function get_unit_for_product( $product_id, $default = null ) { */ public function get_price_suffix( $price_display_suffix, $product ) { // todo make default unit configuarble - if ( $unit = $this->get_unit_for_product( $product->id, apply_filters( 'wciu_default_price_suffix', __( '', 'woocommerce' ) ) ) ) { + if ( $unit = $this->get_unit_for_product( $product->id, apply_filters( 'wciu_default_price_suffix', __( '', 'qau' ) ) ) ) { $price_display_suffix = "/" . $unit . " " . $price_display_suffix; } @@ -102,11 +102,11 @@ public function price_html( $html, $product ) { public function add_order_item_meta( $item_id, $values ) { $unit = $this->get_unit_for_product( $values['product_id'] ); if ( $unit ) { - wc_add_order_item_meta( $item_id, __( "Unit", 'woocommerce' ) . " ($unit)", $values['quantity'] ); + wc_add_order_item_meta( $item_id, __( "Unit", 'qau' ) . " ($unit)", $values['quantity'] ); } } } endif; -return new WC_Quantities_and_Units_Product_Unit(); \ No newline at end of file +return new WC_Quantities_and_Units_Product_Unit(); diff --git a/includes/class-wcqu-units-box.php b/includes/class-wcqu-units-box.php index a62c83d..9f460ee 100644 --- a/includes/class-wcqu-units-box.php +++ b/includes/class-wcqu-units-box.php @@ -11,7 +11,7 @@ public function __construct() { } function units_box_tab() { - echo '
  • Unit
  • '; + echo '
  • '.__('Unit','qau').'
  • '; } public function units_box_create() { @@ -21,8 +21,8 @@ public function units_box_create() {

    - - + +

    @@ -66,4 +66,4 @@ public function save_unit_meta_data( $post_id ) { endif; -return new WC_Quantities_and_Units_Units_Box(); \ No newline at end of file +return new WC_Quantities_and_Units_Units_Box(); diff --git a/includes/class-wcqu-validations.php b/includes/class-wcqu-validations.php index 8b1461e..742c483 100755 --- a/includes/class-wcqu-validations.php +++ b/includes/class-wcqu-validations.php @@ -1,12 +1,12 @@ -validate_single_product( $passed, $product_id, $quantity, false, $variation_id, $variations ); - + } - + /* * Cart Update Validation to ensure quantity ordered follows the user's rules. * - * @access public + * @access public * @param boolean passed * @param string cart_item_key * @param array values @@ -46,14 +46,14 @@ public function add_to_cart_validation( $passed, $product_id, $quantity, $variat public function update_cart_validation( $passed, $cart_item_key, $values, $quantity ) { return $this->validate_single_product( $passed, $values['product_id'], $quantity, true, $values['variation_id'], $values['variation'] ); - + } - + /* * Validates a single product based on the quantity rules applied to it. * It will also validate based on the quantity in the cart. * - * @access public + * @access public * @param boolean passed * @param int product_id * @param int quantity @@ -61,146 +61,188 @@ public function update_cart_validation( $passed, $cart_item_key, $values, $quant * @param int variation_id * @param array variations * @return boolean - * + * */ public function validate_single_product( $passed, $product_id, $quantity, $from_cart, $variation_id = null, $variations = null ) { global $woocommerce, $product, $WC_Quantities_and_Units; - - $product = get_product( $product_id ); + + $product = wc_get_product( $product_id ); $title = $product->get_title(); - + // Get the applied rule and values - if they exist $rule = wcqu_get_applied_rule( $product ); + //var_dump($rule); $values = wcqu_get_value_from_rule( 'all', $product, $rule ); - + + //var_dump($values); if ( $values != null ) extract( $values ); // $min_value, $max_value, $step, $priority, $min_oos, $max_oos - + + //var_dump($values); // Inactive Products can be ignored if ( $values == null ) return true; - - // Check if the product is out of stock + + // Check if the product is out of stock $stock = $product->get_stock_quantity(); - + // Adjust min value if item is out of stock if ( strlen( $stock ) != 0 and $stock <= 0 and isset( $min_oos ) and $min_oos != null ) { $min_value = $min_oos; } - + // Adjust max value if item is out of stock if ( strlen( $stock ) != 0 and $stock <= 0 and isset( $max_oos ) and $max_oos != null ) { $max_value = $max_oos; } - + // Min Validation // added $min_value != 0 since List Items starts all products at 0 quantity. - if ( $min_value != null && $min_value != 0 && $quantity < $min_value ) { - - if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __( "You must add a minimum of %s %s's to your cart.", 'woocommerce' ), $min_value, $title ), 'error' ); - - // Old Validation Style Support - } else { - $woocommerce->add_error( sprintf( __( "You must add a minimum of %s %s's to your cart.", 'woocommerce' ), $min_value, $title ) ); - } - - return false; - } - + + // Max Validation if ( $max_value != null && $quantity > $max_value ) { - + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __( "You may only add a maximum of %s %s's to your cart.", 'woocommerce' ), $max_value, $title ), 'error' ); - - // Old Validation Style Support + wc_add_notice( sprintf( __( "You may only add a maximum of %s %s's to your cart.", 'qau' ), $max_value, $title ), 'error' ); + + // Old Validation Style Support } else { - $woocommerce->add_error( sprintf( __( "You may only add a maximum of %s %s's to your cart.", 'woocommerce' ), $max_value, $title ) ); + $woocommerce->add_error( sprintf( __( "You may only add a maximum of %s %s's to your cart.", 'qau' ), $max_value, $title ) ); } return false; } - + // Subtract the min value from quantity to calc remainder if min value exists if ( $min_value != 0 ) { $rem_qty = $quantity - $min_value; - } else { + } + else { $rem_qty = $quantity; } $rem_qty = (float)$rem_qty; $step = (float)$step; - - // Step Validation + + // Step Validation if ( $step != null && wcqu_fmod_round($rem_qty, $step) != 0 ) { - + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __( "You may only add a %s in multiples of %s to your cart.", 'woocommerce' ), $title, $step ), 'error' ); - - // Old Validation Style Support + wc_add_notice( sprintf( __( "You may only add a %s in multiples of %s to your cart.", 'qau' ), $title, $step ), 'error' ); + + // Old Validation Style Support } else { - $woocommerce->add_error( sprintf( __( "You may only add a %s in multiples of %s to your cart.", 'woocommerce' ), $title, $step ) ); + $woocommerce->add_error( sprintf( __( "You may only add a %s in multiples of %s to your cart.", 'qau' ), $title, $step ) ); } - + return false; } - + // Don't run Cart Validations if user is updating the cart if ( $from_cart != true ) { - + //Cart Categories default Quantity + $cart_cats = array( + 'qty' => 0, + 'terms_name' => '' + ); // Get Cart Quantity for the product foreach( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { + //Get Cart Categories Quantity + if($cats) { + $cart_prod_terms = get_the_terms( $values['product_id'], 'product_cat' ); + foreach ($cart_prod_terms as $term) { + if(in_array($term->term_id, $cats)) { + $cart_cats['qty'] += $values['quantity']; + if(strpos($cart_cats['terms_name'], $term->name) === FALSE) + $cart_cats['terms_name'] .= $term->name.', '; + } + } + } + $_product = $values['data']; if( $product_id == $_product->id ) { $cart_qty = $values['quantity']; } } - + $cart_cats['terms_name'] = trim($cart_cats['terms_name'] ,', '); + //var_dump($cart_cats); + //exit(); // If there aren't any items in the cart already, ignore these validations - if ( isset( $cart_qty ) and $cart_qty != null ) { - + if ( isset( $cart_qty ) and $cart_qty != null) { + // Total Cart Quantity Min Validation if ( $min_value != null && ( $quantity + $cart_qty ) < $min_value ) { - + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __( "Your cart must have a minimum of %s %s's to proceed.", 'woocommerce' ), $min_value, $title ), 'error' ); - - // Old Validation Style Support + wc_add_notice( sprintf( __( "Your cart must have a minimum of %s %s's to proceed.", 'qau' ), $min_value, $title ), 'error' ); + + // Old Validation Style Support } else { - $woocommerce->add_error( sprintf( __( "Your cart must have a minimum of %s %s's to proceed.", 'woocommerce' ), $min_value, $title ) ); + $woocommerce->add_error( sprintf( __( "Your cart must have a minimum of %s %s's to proceed.", 'qau' ), $min_value, $title ) ); } return false; } - + // Total Cart Quantity Max Validation if ( $max_value != null && ( $quantity + $cart_qty ) > $max_value ) { - + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __( "You can only purchase a maximum of %s %s's at once and your cart has %s %s's in it already.", 'woocommerce' ), $max_value, $title, $cart_qty, $title ), 'error' ); - - // Old Validation Style Support + wc_add_notice( sprintf( __( "You can only purchase a maximum of %s %s's at once and your cart has %s %s's in it already.", 'qau' ), $max_value, $title, $cart_qty, $title ), 'error' ); + + // Old Validation Style Support } else { - $woocommerce->add_error( sprintf( __( "You can only purchase a maximum of %s %s's at once and your cart has %s %s's in it already.", 'woocommerce' ), $max_value, $title, $cart_qty, $title ) ); + $woocommerce->add_error( sprintf( __( "You can only purchase a maximum of %s %s's at once and your cart has %s %s's in it already.", 'qau' ), $max_value, $title, $cart_qty, $title ) ); } return false; } - + + // Total Cart Quantity Max Sale Validation + if ( $max_sale != null && ( $quantity + $cart_qty ) > $max_sale ) { + + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { + wc_add_notice( sprintf( __( "You can only purchase a maximum of %s %s's in sale at once and your cart has %s %s's in it already.", 'qau' ), $max_sale, $title, $cart_qty, $title ), 'error' ); + + // Old Validation Style Support + } else { + $woocommerce->add_error( sprintf( __( "You can only purchase a maximum of %s %s's in sale at once and your cart has %s %s's in it already.", 'qau' ), $max_sale, $title, $cart_qty, $title ) ); + } + return false; + } + // Subtract the min value from cart quantity to calc remainder if min value exists if ( $min_value != 0 ) { $cart_qty_rem = $quantity + $cart_qty - $min_value; } else { $cart_qty_rem = $quantity + $cart_qty; } - + // Total Cart Quantity Step Validation $cart_qty_rem = (float)$cart_qty_rem; if ( $step != null && $step != 0 && $cart_qty_rem != 0 && wcqu_fmod_round($cart_qty_rem, $step) != 0 ) { if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { - wc_add_notice( sprintf( __("You may only purchase %s in multiples of %s.", 'woocommerce' ), $title, $step ), 'error' ); - - // Old Validation Style Support + wc_add_notice( sprintf( __("You may only purchase %s in multiples of %s.", 'qau' ), $title, $step ), 'error' ); + + // Old Validation Style Support + } else { + $woocommerce->add_error( sprintf( __("You may only purchase %s in multiples of %s.", 'qau' ), $title, $step ) ); + } + return false; + } + } + + if($cart_cats['qty'] > 0) { + $max_value = $max_value ? $max_value : $max_value_p; + //$min_value = $min_value ? $min_value : $min_value_p; + + // Total Cart Quantity Max Validation + if ( $max_value != null && ( $quantity + $cart_cats['qty'] ) > $max_value ) { + + if ( $WC_Quantities_and_Units->wc_version >= 2.1 ) { + wc_add_notice( sprintf( __( "You can only purchase a maximum of %s products from %s categories at once and your cart has %s items from %s categories in it already.", 'qau' ), $max_value, $cart_cats['terms_name'], $cart_cats['qty'], $cart_cats['terms_name'] ), 'error' ); + + // Old Validation Style Support } else { - $woocommerce->add_error( sprintf( __("You may only purchase %s in multiples of %s.", 'woocommerce' ), $title, $step ) ); + $woocommerce->add_error( sprintf( __( "You can only purchase a maximum of %s products from %s categories at once and your cart has %s items from %s categories in it already.", 'qau' ), $max_value, $cart_cats['terms_name'], $cart_cats['qty'], $cart_cats['terms_name'] ) ); } return false; } diff --git a/includes/wcqu-functions.php b/includes/wcqu-functions.php index b150f58..9eef764 100755 --- a/includes/wcqu-functions.php +++ b/includes/wcqu-functions.php @@ -1,27 +1,29 @@ -id, '_wpbo_deactive', true ) == 'on' ) { return 'inactive'; - + } elseif ( get_post_meta( $product->id, '_wpbo_override', true ) == 'on' ) { - return 'override'; - + $rule = wcqu_get_applied_rule_obj( $product, $role ); + $rule->rule_status = 'override'; + return $rule; + } elseif ( isset( $options['ipq_site_rule_active'] ) and $options['ipq_site_rule_active'] == 'on' ) { return 'sitewide'; - + } else { return wcqu_get_applied_rule_obj( $product, $role ); } @@ -33,13 +35,13 @@ function wcqu_get_applied_rule( $product, $role = null ) { * * @params object $product WC_Product object * @param string User role to get rule from, otherwise current user role is used -* @return mixed Null if no rule applies / Object top rule post +* @return mixed Null if no rule applies / Object top rule post */ function wcqu_get_applied_rule_obj( $product, $role = null ) { // Get Product Terms $product_cats = wp_get_post_terms( $product->id, 'product_cat' ); - $product_tags = wp_get_post_terms( $product->id, 'product_tag' ); + $product_tags = wp_get_post_terms( $product->id, 'product_tag' ); // Get role if not passed if(!is_user_logged_in()) { @@ -48,65 +50,63 @@ function wcqu_get_applied_rule_obj( $product, $role = null ) { $user_data = get_userdata( get_current_user_id() ); if ( $user_data->roles ) { foreach ( $user_data->roles as $cap => $val ) { - $role = $cap; + $role = $val; } } } // Combine all product terms $product_terms = array_merge( $product_cats, $product_tags ); - // Check for rule / role transient if ( false === ( $rules = get_transient( 'ipq_rules_' . $role ) ) ) { - // Get all Rules $args = array( 'posts_per_page' => -1, 'post_type' => 'quantity-rule', 'post_status' => 'publish', - ); - + ); + $rules = get_posts( $args ); - + // Remove rules not applied to current user role - $cnt = 0; + $cnt = 0; $rules_to_unset = array(); - + while ( $cnt < count( $rules ) ) { - + $roles = get_post_meta( $rules[$cnt]->ID, '_roles' ); if ( !in_array( $role, $roles[0] ) && !empty($roles[0])) { array_push( $rules_to_unset, $cnt ); - } - + } + $cnt++; - } + } $duration = 60 * 60 * 12; // 12 hours arsort( $rules_to_unset ); - + foreach ( $rules_to_unset as $single_unset ) { unset( $rules[$single_unset] ); - } - set_transient( 'ipq_rules_' . $role, $rules, $duration ); - } + } + set_transient( 'ipq_rules_' . $role, $rules, $duration ); + } $top = null; - $top_rule = null; + $top_rule = new StdClass(); // Loop through the rules and find the ones that apply foreach ( $rules as $rule ) { - + $apply_rule = false; - + // Get the Rule's Cats and Tags $cats = get_post_meta( $rule->ID, '_cats' ); $tags = get_post_meta( $rule->ID, '_tags' ); $roles = get_post_meta( $rule->ID, '_roles' ); - + if( $cats != false ) $cats = $cats[0]; - + if( $tags != false ) $tags = $tags[0]; @@ -122,11 +122,11 @@ function wcqu_get_applied_rule_obj( $product, $role = null ) { $apply_rule = true; } } - + // If the rule applies, check the priority if ( $apply_rule == true ) { - - $priority = get_post_meta( $rule->ID, '_priority', true ); + + $priority = get_post_meta( $rule->ID, '_priority', true ); if( $priority != '' and $top > $priority or $top == null ) { $top = $priority; @@ -135,48 +135,51 @@ function wcqu_get_applied_rule_obj( $product, $role = null ) { } } - return $top_rule; + return $top_rule; } /* * Get the Input Value (min/max/step/priority/role/all) for a product given a rule * * @params string $type Product type -* @params object $product Product Object +* @params object $product Product Object * @params object $rule Quantity Rule post object * @return mixed */ function wcqu_get_value_from_rule( $type, $product, $rule ) { - // Validate $type - if ( $type != 'min' and - $type != 'max' and - $type != 'step' and - $type != 'all' and - $type != 'priority' and + if ( $type != 'min' and + $type != 'max' and + $type != 'step' and + $type != 'all' and + $type != 'priority' and $type != 'role' and $type != 'min_oos' and - $type != 'max_oos' + $type != 'max_oos' and + $type != 'cats' and + $type != 'tags' and + $type != 'min_sale' and + $type != 'max_sale' ) { return null; - - // Validate for missing rule + + // Validate for missing rule } elseif ( $rule == null ) { return null; - + // Return Null if Inactive } elseif ( $rule == 'inactive' ) { return null; - + // Return Product Meta if Override is on - } elseif ( $rule == 'override' ) { - +} elseif ( isset($rule->rule_status) && $rule->rule_status == 'override' ) { + // Check if the product is out of stock $stock = $product->get_stock_quantity(); // Check if the product is under stock management and out of stock if ( strlen( $stock ) != 0 and $stock <= 0 ) { - + // Return Out of Stock values if they exist switch ( $type ) { case 'min': @@ -184,56 +187,90 @@ function wcqu_get_value_from_rule( $type, $product, $rule ) { if ( $min_oos != '' ) return $min_oos; break; - + case 'max': $max_oos = get_post_meta( $product->id, '_wpbo_maximum_oos', true ); if ( $max_oos != '' ) return $max_oos; - break; - } + break; + } // If nothing was returned, proceed as usual } - + switch ( $type ) { case 'all': - return array( - 'min_value' => get_post_meta( $product->id, '_wpbo_minimum', true ), - 'max_value' => get_post_meta( $product->id, '_wpbo_maximum', true ), - 'step' => get_post_meta( $product->id, '_wpbo_step', true ), - 'min_oos' => get_post_meta( $product->id, '_wpbo_minimum_oos', true ), - 'max_oos' => get_post_meta( $product->id, '_wpbo_maximum_oos', true ), - ); + $value = array( + 'min_value' => get_post_meta( $product->id, '_wpbo_minimum', true ), + 'max_value' => get_post_meta( $product->id, '_wpbo_maximum', true ), + 'step' => get_post_meta( $product->id, '_wpbo_step', true ), + 'min_oos' => get_post_meta( $product->id, '_wpbo_minimum_oos', true ), + 'max_oos' => get_post_meta( $product->id, '_wpbo_maximum_oos', true ), + 'min_sale' => get_post_meta( $product->id, '_wpbo_minimum_sale', true ), + 'max_sale' => get_post_meta( $product->id, '_wpbo_maximum_sale', true ), + //'roles' => get_post_meta( $rule->ID, '_roles', true ), + 'cats' => get_post_meta( $rule->ID, '_cats', true ), + 'tags' => get_post_meta( $rule->ID, '_tags', true ), + ); + + if(isset($value['cats']) || isset($value['tags'])) { + $value['min_value_p'] = get_post_meta( $rule->ID, '_min', true ); + $value['max_value_p'] = get_post_meta( $rule->ID, '_max', true ); + $value['min_oos_p'] = get_post_meta( $rule->ID, '_min_oos', true ); + $value['max_oos_p'] = get_post_meta( $rule->ID, '_max_oos', true ); + $value['step_p'] = get_post_meta( $rule->ID, '_step', true ); + //$value['min_sale_p'] = get_post_meta( $rule->ID, '_min_sale', true ); + //$value['max_sale_p'] = get_post_meta( $rule->ID, '_max_sale', true ); + } + + return $value; + break; case 'min': return get_post_meta( $product->id, '_wpbo_minimum', true ); break; - - case 'max': + + case 'max': return get_post_meta( $product->id, '_wpbo_maximum', true ); break; - + case 'step': return get_post_meta( $product->id, '_wpbo_step', true ); break; - + case 'min_oos': return get_post_meta( $product->id, '_wpbo_minimum_oos', true ); break; - + case 'max_oos': return get_post_meta( $product->id, '_wpbo_maximum_oos', true ); break; - + + case 'cats': + return get_post_meta( $rule->ID, '_tags', true ); + break; + + case 'tags': + return get_post_meta( $rule->ID, '_cats', true ); + break; + + case 'min_sale': + return get_post_meta( $product->id, '_wpbo_minimum_sale', true ); + break; + + case 'max_sale': + return get_post_meta( $product->id, '_wpbo_maximum_sale', true ); + break; + case 'priority': return null; break; - } - + } + // Check for Site Wide Rule } elseif ( $rule == 'sitewide' ) { $options = get_option( 'ipq_options' ); - + if( isset( $options['ipq_site_min'] ) ) { $min = $options['ipq_site_min']; } else { @@ -251,100 +288,123 @@ function wcqu_get_value_from_rule( $type, $product, $rule ) { } else { $min_oos = ''; } - + if( isset( $options['ipq_site_max_oos'] ) ) { $max_oos = $options['ipq_site_max_oos']; } else { $max_oos = ''; } - + if( isset( $options['ipq_site_step'] ) ) { $step = $options['ipq_site_step']; } else { - $step = ''; + $step = ''; } switch ( $type ) { case 'all': - return array( - 'min_value' => $min, + return array( + 'min_value' => $min, 'max_value' => $max, 'min_oos' => $min_oos, 'max_oos' => $max_oos, 'step' => $step ); break; - + case 'min': - return array( 'min' => $min ); + return array( 'min' => $min ); break; - - case 'max': - return array( 'max' => $max ); + + case 'max': + return array( 'max' => $max ); break; - - case 'min_oos': - return array( 'min_oos' => $min_oos ); + + case 'min_oos': + return array( 'min_oos' => $min_oos ); break; - - case 'max_oos': - return array( 'max_oos' => $max_oos ); + + case 'max_oos': + return array( 'max_oos' => $max_oos ); break; - + case 'step': - return array( 'step' => $step ); + return array( 'step' => $step ); break; - + case 'priority': return null; break; - + } - + // Return Values from the Rule based on $type requested } else { - + switch ( $type ) { case 'all': - return array( + //TODO: why get all and filter array? + //$meta = get_post_meta( $rule->ID, null, true ); + + return array( 'min_value' => get_post_meta( $rule->ID, '_min', true ), 'max_value' => get_post_meta( $rule->ID, '_max', true ), 'min_oos' => get_post_meta( $rule->ID, '_min_oos', true ), 'max_oos' => get_post_meta( $rule->ID, '_max_oos', true ), 'step' => get_post_meta( $rule->ID, '_step', true ), 'priority' => get_post_meta( $rule->ID, '_priority', true ), - 'roles' => get_post_meta( $rule->ID, '_roles', true ) + 'roles' => get_post_meta( $rule->ID, '_roles', true ), + 'cats' => get_post_meta( $rule->ID, '_cats', true ), + 'tags' => get_post_meta( $rule->ID, '_tags', true ), + 'min_sale' => get_post_meta( $rule->ID, '_min_sale', true ), + 'max_sale' => get_post_meta( $rule->ID, '_max_sale', true ), ); break; - + case 'min': return get_post_meta( $rule->ID, '_min', true ); break; - - case 'max': + + case 'max': return get_post_meta( $rule->ID, '_max', true ); break; - - case 'min_oos': + + case 'min_oos': return get_post_meta( $rule->ID, '_min_oos', true ); break; - - case 'max_oos': + + case 'max_oos': return get_post_meta( $rule->ID, '_max_oos', true ); break; - + case 'step': return get_post_meta( $rule->ID, '_step', true ); break; - + case 'role': return get_post_meta( $rule->ID, '_roles', true ); break; - + case 'priority': return get_post_meta( $rule->ID, '_priority', true ); break; - } + + case 'cats': + return get_post_meta( $rule->ID, '_tags', true ); + break; + + case 'tags': + return get_post_meta( $rule->ID, '_cats', true ); + break; + + case 'min_sale': + return get_post_meta( $rule->ID, '_min_sale', true ); + break; + + case 'max_sale': + return get_post_meta( $rule->ID, '_max_sale', true ); + break; + } } } @@ -352,22 +412,21 @@ function wcqu_get_value_from_rule( $type, $product, $rule ) { * Validate inputs as numbers and set them to null if 0 */ function wcqu_validate_number( $number ) { - $number = stripslashes( $number ); -// $number = intval( $number ); - + // $number = intval( $number ); + if ( $number == 0 ) { return null; } elseif ( $number < 0 ) { return null; - } - + } + return $number; } /** * Provides a fmod function that actually works as intended. - * + * * @param float $x The dividend * @param float $y The divisor * @@ -416,8 +475,8 @@ function wpbo_get_value_from_rule( $type, $product, $rule ) { /** - * This is ugly, but we need to pretend that the pre-forked version of this plugin is active since Thumbnail Quantities - * does a check whether it active. It'd be nice if they just made it a filter instead. Also TQ has some incorrect + * This is ugly, but we need to pretend that the pre-forked version of this plugin is active since Thumbnail Quantities + * does a check whether it active. It'd be nice if they just made it a filter instead. Also TQ has some incorrect * javascript rounding on decimal values, so we may just have to fork it at some point as well. */ add_filter( 'active_plugins', function($plugins){ diff --git a/languages/default.pot b/languages/default.pot new file mode 100644 index 0000000..84148a4 --- /dev/null +++ b/languages/default.pot @@ -0,0 +1,103 @@ +msgid "" +msgstr "" +"Project-Id-Version: Quantities and units for woocommerce\n" +"POT-Creation-Date: 2016-08-13 18:01+0100\n" +"PO-Revision-Date: 2016-08-13 18:05+0100\n" +"Last-Translator: steppade\n" +"Language-Team: \n" +"Language: English\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.5.4\n" +"X-Poedit-KeywordsList: _;gettext;gettext_noop;__\n" +"X-Poedit-Basepath: /home/gine/Code/01_wordpress/www.tiportolaspesa.it/wp-" +"content/plugins/quantities-and-units-for-woocommerce/\n" +"X-Poedit-SourceCharset: UTF-8\n" +"X-Poedit-SearchPath-0: includes\n" + +#: includes/class-wcqu-product-unit.php:105 +msgid "Unit" +msgstr "" + +#: includes/class-wcqu-validations.php:103 +#: includes/class-wcqu-validations.php:107 +#, php-format +msgid "You must add a minimum of %s %s's to your cart." +msgstr "" + +#: includes/class-wcqu-validations.php:117 +#: includes/class-wcqu-validations.php:121 +#, php-format +msgid "You may only add a maximum of %s %s's to your cart." +msgstr "" + +#: includes/class-wcqu-validations.php:140 +#: includes/class-wcqu-validations.php:144 +#, php-format +msgid "You may only add a %s in multiples of %s to your cart." +msgstr "" + +#: includes/class-wcqu-validations.php:185 +#: includes/class-wcqu-validations.php:189 +#, php-format +msgid "Your cart must have a minimum of %s %s's to proceed." +msgstr "" + +#: includes/class-wcqu-validations.php:198 +#: includes/class-wcqu-validations.php:202 +#, php-format +msgid "" +"You can only purchase a maximum of %s %s's at once and your cart has %s %s's " +"in it already." +msgstr "" + +#: includes/class-wcqu-validations.php:218 +#: includes/class-wcqu-validations.php:222 +#, php-format +msgid "You may only purchase %s in multiples of %s." +msgstr "" + +#: includes/class-wcqu-validations.php:233 +#: includes/class-wcqu-validations.php:237 +#, php-format +msgid "" +"You can only purchase a maximum of %s products from %s categories at once " +"and your cart has %s items from %s categories in it already." +msgstr "" + +#: includes/class-wcqu-post-type.php:81 includes/class-wcqu-post-type.php:163 +msgid "Priority" +msgstr "" + +#: includes/class-wcqu-post-type.php:82 includes/class-wcqu-post-type.php:164 +msgid "Minimum" +msgstr "" + +#: includes/class-wcqu-post-type.php:83 includes/class-wcqu-post-type.php:165 +msgid "Maximum" +msgstr "" + +#: includes/class-wcqu-post-type.php:84 includes/class-wcqu-post-type.php:166 +msgid "Step Value" +msgstr "" + +#: includes/class-wcqu-post-type.php:85 +msgid "Categories" +msgstr "" + +#: includes/class-wcqu-post-type.php:86 +msgid "Tags" +msgstr "" + +#: includes/class-wcqu-post-type.php:87 +msgid "Roles" +msgstr "" + +#: includes/class-wcqu-post-type.php:88 +msgid "Date" +msgstr "" + +#: includes/class-wcqu-product-meta-box.php:29 +msgid "Product Quantity Rules" +msgstr "" diff --git a/languages/qau-it_IT.mo b/languages/qau-it_IT.mo new file mode 100644 index 0000000..b971730 Binary files /dev/null and b/languages/qau-it_IT.mo differ diff --git a/languages/qau-it_IT.po b/languages/qau-it_IT.po new file mode 100644 index 0000000..635938e --- /dev/null +++ b/languages/qau-it_IT.po @@ -0,0 +1,129 @@ +msgid "" +msgstr "" +"Project-Id-Version: Quantities and units for woocommerce\n" +"POT-Creation-Date: 2016-08-13 18:01+0100\n" +"PO-Revision-Date: 2016-08-13 18:19+0100\n" +"Last-Translator: steppade\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.5.4\n" +"X-Textdomain-Support: yes\n" +"X-Poedit-KeywordsList: _;gettext;gettext_noop;__\n" +"X-Poedit-Basepath: ..\n" +"X-Poedit-SourceCharset: UTF-8\n" +"Language: Italian\n" +"X-Poedit-SearchPath-0: includes\n" + +#@ qau +#: ../includes/class-wcqu-product-unit.php:105 +msgid "Unit" +msgstr "Unità" + +#@ qau +#: ../includes/class-wcqu-validations.php:103 +#: ../includes/class-wcqu-validations.php:107 +#, php-format +msgid "You must add a minimum of %s %s's to your cart." +msgstr "Devi aggiungere un minimo di %s %s al tuo carrello." + +#@ qau +#: ../includes/class-wcqu-validations.php:117 +#: ../includes/class-wcqu-validations.php:121 +#, php-format +msgid "You may only add a maximum of %s %s's to your cart." +msgstr "Puoi aggiungere un massimo di %s %s al tuo carrello." + +#@ qau +#: ../includes/class-wcqu-validations.php:140 +#: ../includes/class-wcqu-validations.php:144 +#, php-format +msgid "You may only add a %s in multiples of %s to your cart." +msgstr "Puoi aggiungere solo un multiplo di %s di %s al tuo carrello." + +#@ qau +#: ../includes/class-wcqu-validations.php:185 +#: ../includes/class-wcqu-validations.php:189 +#, php-format +msgid "Your cart must have a minimum of %s %s's to proceed." +msgstr "" +"Il tuo carrello deve avere un minimo di %s %s per procedere all'acquisto." + +#@ qau +#: ../includes/class-wcqu-validations.php:198 +#: ../includes/class-wcqu-validations.php:202 +#, php-format +msgid "" +"You can only purchase a maximum of %s %s's at once and your cart has %s %s's " +"in it already." +msgstr "" +"Puoi acquistare solo un massimo di %s %s alla volta e nel tuo carrello hai " +"già %s %s." + +#@ qau +#: ../includes/class-wcqu-validations.php:218 +#: ../includes/class-wcqu-validations.php:222 +#, php-format +msgid "You may only purchase %s in multiples of %s." +msgstr "Puoi acquistare solo %s in multipli di %s." + +#@ qau +#: ../includes/class-wcqu-validations.php:233 +#: ../includes/class-wcqu-validations.php:237 +#, php-format +msgid "" +"You can only purchase a maximum of %s products from %s categories at once " +"and your cart has %s items from %s categories in it already." +msgstr "" +"Puoi comprare solo un massimo di %s prodotti alla volta appartenenti alle " +"categorie %s. Nel tuo carrello hai già %s prodotti dalle categorie %s." + +#@ qau +#: ../includes/class-wcqu-post-type.php:81 +#: ../includes/class-wcqu-post-type.php:163 +msgid "Priority" +msgstr "Priorità" + +#@ qau +#: ../includes/class-wcqu-post-type.php:82 +#: ../includes/class-wcqu-post-type.php:164 +msgid "Minimum" +msgstr "Minimo" + +#@ qau +#: ../includes/class-wcqu-post-type.php:83 +#: ../includes/class-wcqu-post-type.php:165 +msgid "Maximum" +msgstr "Massimo" + +#@ qau +#: ../includes/class-wcqu-post-type.php:84 +#: ../includes/class-wcqu-post-type.php:166 +msgid "Step Value" +msgstr "Valore multipli" + +#@ qau +#: ../includes/class-wcqu-post-type.php:85 +msgid "Categories" +msgstr "Categorie" + +#@ qau +#: ../includes/class-wcqu-post-type.php:86 +msgid "Tags" +msgstr "Tags" + +#@ qau +#: ../includes/class-wcqu-post-type.php:87 +msgid "Roles" +msgstr "Ruoli" + +#@ qau +#: ../includes/class-wcqu-post-type.php:88 +msgid "Date" +msgstr "Data" + +#@ qau +#: ../includes/class-wcqu-product-meta-box.php:29 +msgid "Product Quantity Rules" +msgstr "Regole di quantità per i prodotti" diff --git a/quantites-and-units.php b/quantites-and-units.php index 6c3cc97..b1bf71a 100755 --- a/quantites-and-units.php +++ b/quantites-and-units.php @@ -6,22 +6,24 @@ Version: 1.0.10 Author: Nicholas Verwymeren Author URI: https://www.nickv.codes -*/ +Text Domain: qau +Domain Path: /languages +*/ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! class_exists( 'WC_Quantities_and_Units' ) ) : class WC_Quantities_and_Units { - + public $wc_version; - + /** * @var WooCommerce Supplier instance * @since 2.1 */ protected static $_instance = null; - + /** * Main Incremental Product Quantities Instance * @@ -33,14 +35,14 @@ public static function instance() { } return self::$_instance; } - + public function __construct() { - + // Activation / Deactivation Hooks register_activation_hook( __FILE__, array( $this, 'activation_hook' ) ); register_activation_hook( __FILE__, array( $this, 'update_rules_with_roles' ) ); register_deactivation_hook( __FILE__, array( $this, 'deactivation_hook' ) ); - + // Include Required Files require_once( 'includes/wcqu-functions.php' ); require_once( 'includes/class-wcqu-filters.php' ); @@ -51,18 +53,20 @@ public function __construct() { require_once( 'includes/class-wcqu-advanced-rules.php' ); require_once( 'includes/class-wcqu-units-box.php' ); require_once( 'includes/class-wcqu-product-unit.php' ); - - // Add Scripts and styles + + // Add Scripts and styles add_action( 'wp_enqueue_scripts', array( $this, 'input_value_validation' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_action( 'admin_init', array( $this, 'quantity_styles' ) ); - - // Set WC Version Number + + // Set WC Version Number add_action( 'init', array( $this, 'get_wc_version' ) ); - + add_action( 'init', array( $this, 'text_domain' ) ); + // Control Admin Notices // add_action( 'admin_notices', array( $this, 'thumbnail_plugin_notice' ) ); add_action( 'admin_init', array( $this, 'thumbnail_plugin_notice_ignore' ) ); + add_action( 'admin_init', array( $this, 'text_domain' ) ); add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); } @@ -72,17 +76,21 @@ public function plugins_loaded() { remove_filter( 'woocommerce_stock_amount', 'intval' ); // allow decimal floats - add_filter( 'woocommerce_stock_amount', 'floatval' ); + add_filter( 'woocommerce_stock_amount', 'floatval' ); + } + + public function text_domain() { + load_plugin_textdomain( 'qau', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } /* * Adds default option values - */ + */ public function activation_hook() { $options = get_option( 'ipq_options' ); - - $defaults = array ( + + $defaults = array ( 'ipq_site_rule_active' => '', 'ipq_site_min' => '', 'ipq_site_max' => '', @@ -90,18 +98,18 @@ public function activation_hook() { 'ipq_site_rule_active' => '', 'ipq_show_qty_note' => '', 'ipq_qty_text' => 'Minimum Qty: %MIN%', - 'ipq_show_qty_note_pos' => 'below', + 'ipq_show_qty_note_pos' => 'below', 'ipq_qty_class' => '' ); // If no options set the defaults if ( $options == false ) { add_option( 'ipq_options', $defaults, '', false ); - + // Otherwise check that all option are set } else { $needs_update = FALSE; - + // Check and assign each unset value foreach ( $defaults as $key => $value ) { if ( !isset( $options[$key] ) ) { @@ -109,7 +117,7 @@ public function activation_hook() { $needs_update = TRUE; } } - + // If values are missing update the options if ( $needs_update === TRUE ) { update_option( 'ipq_options', $options ); @@ -121,48 +129,48 @@ public function activation_hook() { * 'Checks' all user roles for pre-2.1 rules * * @status To be depricated in version 2.3 - */ + */ public function update_rules_with_roles() { - + // Construct default roles list to apply global $wp_roles; $roles = $wp_roles->get_names(); $applied_roles = array(); foreach ( $roles as $slug => $name ) { array_push( $applied_roles, $slug ); - } - + } + $args = array ( 'posts_per_page' => -1, 'post_type' => 'quantity-rule', 'post_status' => 'publish', ); - + $rules = get_posts( $args ); - + // Loop through rules foreach ( $rules as $rule ) { - // If their rule value is false, apply all roles + // If their rule value is false, apply all roles $roles = get_post_meta( $rule->ID, '_roles', true ); - + if ( $roles == false ) { update_post_meta( $rule->ID, '_roles', $applied_roles, false ); } } } - + /* * Remove thumbnail plugin notice meta value - */ + */ public function deactivation_hook() { - + $args = array( 'meta_key' => 'wpbo_thumbnail_input_notice', 'meta_value' => 'true', ); - + $admins = get_users( $args ); - + foreach ( $admins as $admin ) { delete_user_meta( $admin->ID, 'wpbo_thumbnail_input_notice' ); } @@ -176,130 +184,131 @@ public function enqueue_styles() { } /* - * Include JS to round any value that isn't a multiple of the + * Include JS to round any value that isn't a multiple of the * step up. - */ + */ public function input_value_validation() { - + global $post, $woocommerce; - + // Only display script if we are on a single product or cart page if ( is_object( $post ) and $post->post_type == 'product' or is_cart() ) { - - wp_enqueue_script( - 'ipq_validation', + + wp_enqueue_script( + 'ipq_validation', plugins_url( '/assets/js/ipq_input_value_validation.js', __FILE__ ), array( 'jquery' ) ); // Only localize parameters for variable products if ( ! is_cart() ) { - + // Get the product $pro = get_product( $post ); - + // Check if variable if ( $pro->product_type == 'variable' ) { // See what rules are being applied $rule_result = wcqu_get_applied_rule( $pro ); - + // If the rule result is inactive, we're done if ( $rule_result == 'inactive' or $rule_result == null ) { return; - + // Get values for Override, Sitewide and Rule Controlled Products } else { $values = wcqu_get_value_from_rule( 'all', $pro, $rule_result ); } - - // Check if the product is out of stock + + // Check if the product is out of stock $stock = $pro->get_stock_quantity(); - + // Check if the product is under stock management and out of stock if ( strlen( $stock ) != 0 and $stock <= 0 ) { - + if ( $values['min_oos'] != '' ) { $values['min_value'] = $values['min_oos']; } - + if ( $values['max_oos'] != '' ) { $values['max_value'] = $values['max_oos']; } - + } - + // Output admin-ajax.php URL with sma eprotocol as current page $params = array ( 'min' => $values['min_value'], 'max' => $values['max_value'], 'step' => $values['step'] - ); - + ); + wp_localize_script( 'ipq_validation', 'ipq_validation', $params ); } - } - } + } + } } - + /* * Include Styles - */ + */ public function quantity_styles() { - + if ( is_admin() ) { - - wp_enqueue_style( - 'wcqu_admin_quantity_styles', + + wp_enqueue_style( + 'wcqu_admin_quantity_styles', plugins_url( '/assets/css/admin-styles.css', __FILE__ ) ); - - wp_enqueue_script( - 'wcqu_admin_script', + + wp_enqueue_script( + 'wcqu_admin_script', plugins_url( '/assets/js/ipq_admin_script.js', __FILE__ ), array( 'jquery' ) ); } } - + /* - * Set what version of WooCommerce the user has installed - */ + * Set what version of WooCommerce the user has installed + */ public function get_wc_version() { - + if ( ! function_exists( 'get_plugins' ) ) require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - + $plugin_folder = get_plugins( '/' . 'woocommerce' ); $plugin_file = 'woocommerce.php'; - + if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) { $this->wc_version = $plugin_folder[$plugin_file]['Version']; } else { $this->wc_version = NULL; } } - + /* * General Admin Notice to Encourage users to download thumbnail input as well - */ + */ public function thumbnail_plugin_notice() { global $current_user; - $user_id = $current_user->ID; + $user_id = $current_user->ID; - // Check if Thumbnail Plugin is activated + // Check if Thumbnail Plugin is activated if ( !in_array( 'woocommerce-thumbnail-input-quantities/woocommerce-thumbnail-input-quantity.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { - + // Check if User has Dismissed this message already if ( ! get_user_meta( $user_id, 'wpbo_thumbnail_input_notice' ) ) { - - echo '
    -

    Notice: It is highly recommended you install and activate the WooCommerce Thumbnail Input Quantites plugin to display input boxes on products thumbnails. '. + __('

    Notice: It is highly recommended you install and activate the WooCommerce Thumbnail Input Quantites plugin to display input boxes on products thumbnails.', 'qau') + .' Dismiss Notice

    '; + + echo '">'.__("Dismiss Notice",'qau').'

    '; } - } + } } - + /* * Make Admin Notice Dismissable - */ + */ public function thumbnail_plugin_notice_ignore() { global $current_user; $user_id = $current_user->ID; - + if ( isset($_GET['wpbo_thumbnail_plugin_dismiss']) && '0' == $_GET['wpbo_thumbnail_plugin_dismiss'] ) { add_user_meta($user_id, 'wpbo_thumbnail_input_notice', 'true', true); }
    RoleRuleMinMaxMin OOSMax OOSStepPriority
    None