Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revamp Price entry/storage of

New tiered pricing mechanism, unlimited tiers with labels.
Add css and js to support adding/deleting rows
  • Loading branch information...
commit 2e3174d714cfc12b77835151fed6df7713279b46 1 parent 92d75d4
@1bigidea authored
View
BIN  images/Add.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/Delete.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/price-table.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/price-table@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
34 inc/class.shortcodes.php
@@ -28,35 +28,21 @@ static function this(){
function pricetable($atts){
global $woocommerce, $product;
- extract( shortcode_atts(array('id' => 0, 'qty' => "50,100,250,500,1000"), $atts) );
+ extract( shortcode_atts(array('id' => 0, 'price_levels' => "50,100,250,500,1000"), $atts) );
if( 0 == $id ) {
- $this_product = $product;
- } else {
- // get the product
+ $id = $product->id;
}
+ $rows = explode(',', $price_levels);
- $variations = $product->get_available_variations();
-
- foreach ($variations as $variation){
- $v_product = new WC_Product_Variation($variation['variation_id']);
-
- $method = $variation['attributes']['attribute_pa_printing-method'];
- $prices[$method] = array(
- 'min' => get_post_meta($variation['variation_id'], '_nono_min_order_qty', true),
- 'price' => $v_product->price,
- 'sale_price'=> $v_product->sale_price,
- 'addl_pieces' => get_post_meta($variation['variation_id'], '_nono_price_per_piece', true),
- );
- }
-
- $rows = explode(',', $qty);
+ $prices = get_post_meta($id, NonoPrintPricing::$pricing_table_key, true);
+ ksort($prices, SORT_NUMERIC);
$output = '<table class="price-table">';
$output .= sprintf('<thead><th>%s</th><th>%s</th></thead>', __('Qty', 'nono-per-unit'), __('Price', 'nono-per-unit'));
foreach( $rows as $row ){
$row_text = '<tr><td class="price-table-qty">%d</td><td class="price-table-amount">%s</td></tr>';
- $output .= sprintf($row_text, (int) $row, number_format(self::determine_price($row, $prices), 2, ',', '') );
+ $output .= sprintf($row_text, (int) $row, number_format(self::determine_price($row, $prices), 2, ',', '.') );
}
$output .= '</table>';
@@ -65,11 +51,9 @@ function pricetable($atts){
function determine_price($qty, $prices){
$pricing = 0;
- foreach($prices as $method => $details ){
- if( $qty >= $details['min'] ) {
- $calc = ($details['min'] * $details['price']) + ( ($qty - $details['min']) * $details['addl_pieces'] );
- if( $qty >= $details['min'] )
- $pricing = $calc;
+ foreach($prices as $min => $details ){
+ if( $qty >= $min ) {
+ $pricing = (($min * $details['price']) + ( ($qty - $min) * $details['price'] ));
}
}
return $pricing;
View
14 js/product-editor.js
@@ -0,0 +1,14 @@
+jQuery(function($){
+ $('#nono-pricing-table-edit').delegate('.add-row', 'click', function(e){
+ row = $(this).closest('tr');
+ new_row = $(row).clone();
+ $(new_row).find('input').val('');
+ $(row).after(new_row);
+ return false;
+ });
+
+ $('#nono-pricing-table-edit').delegate('.delete-row', 'click', function(e){
+ row = $(this).closest('tr').remove();
+ return false;
+ });
+});
View
101 loader.php
@@ -33,6 +33,7 @@ class NonoPrintPricing {
var $min_order_key = '_nono_min_order_qty';
var $price_per_piece_key = '_nono_price_per_piece';
+ public static $pricing_table_key = '_nono_pricing_table';
function __construct() {
@@ -91,38 +92,86 @@ function initiate_product($classname, $product_type, $post_type, $product_id){
}
function admin_init(){
- add_action('woocommerce_product_after_variable_attributes', array($this, 'variation_admin'), 10, 3);
- add_action('woocommerce_save_product_variation', array($this, 'save_pricing_info') );
+ add_action('woocommerce_product_write_panel_tabs', array($this, 'add_panel') );
+ add_action('woocommerce_product_write_panels', array($this, 'show_panel'));
+
+// add_action('woocommerce_product_after_variable_attributes', array($this, 'variation_admin'), 10, 3);
+ add_action('woocommerce_process_product_meta_variable', array($this, 'save_pricing_info'), 10 ,1);
+
+ add_action('admin_enqueue_scripts', array($this, 'enqueue') );
+ }
+ function enqueue($hook){
+ global $post;
+
+ if( ! in_array($hook, array('post-new.php', 'post.php')) || 'product' != $post->post_type ) return;
+
+ wp_enqueue_script('nono-per-unit-js', plugins_url('/js/product-editor.js', __FILE__), array('jquery'));
+ wp_enqueue_style('nono-per-unit', plugins_url('woo-per-unit-pricing.css', __FILE__) );
}
/**
- * Display fields for Min Qty and Price per Piece
+ * Insert Custom Write Panel in Woo Product Editor
*/
- function variation_admin($loop, $variation_data, $variation){
-
- $_min_qty = get_post_meta($variation->ID, $this->min_order_key, true);
- $_per_piece_price = get_post_meta($variation->ID, $this->price_per_piece_key, true);
-?>
- <tr>
- <td>
- <div>
- <label><?php _e( 'Min Qty:', 'nono-print-pricing' ); ?> <a class="tips" data-tip="<?php _e( 'Minimum number of pieces at above price', 'nono-print-pricing' ); ?>" href="#">[?]</a></label>
- <input type="number" size="5" name="variable_min_qty[<?php echo $loop; ?>]" value="<?php if ( isset( $_min_qty ) ) echo esc_attr( $_min_qty ); ?>" />
- </div>
- </td>
- <td>
- <div>
- <label><?php echo __( 'Price per piece:', 'nono-print-pricing' ) . ' ('.get_woocommerce_currency_symbol().')'; ?></label>
- <input type="number" size="5" name="variable_per_piece_price[<?php echo $loop; ?>]" value="<?php if ( isset( $_per_piece_price ) ) echo number_format( $_per_piece_price, 2, '.', '' ); ?>" step="any" min="0" placeholder="<?php _e( 'Per piece price (required)', 'nono-print-pricing' ); ?>" />
- </div>
- </td>
- </tr>
-<?php
+ function add_panel(){
+ printf('<li class="nono-price-panel nono-pricing-option"><a href="#nono_price_panel">%s</a></li>', __( 'Price Tables', 'nono-per-unit' ) );
}
- function save_pricing_info($variation_id){
+ function show_panel(){
+ global $post;
+
+ $prices = get_post_meta($post->ID, self::$pricing_table_key, true);
+ if( empty($prices) ){
+ $prices = array(
+ 'empty1' => array('price' => '', 'label' => 'Digital'),
+ 'empty2' => array('price' => '', 'label' => 'Offset')
+ );
+ }
- $variation_ids = $_POST['variable_post_id'];
+ echo '<div id="nono_price_panel" class="panel woocommerce_options_panel">';
+
+ echo '<div class="options_group nono-price-table">';
+ echo '<table id="nono-pricing-table-edit">';
+ printf('<thead><td>%s</td><td>%s</td><td>%s</td><td>&nbsp;</td></thead>',
+ __('Min Qty', 'nono-per-unit'),
+ __('Price Each', 'nono-per-unit'),
+ __('Label', 'nono-per-unit')
+ );
+ foreach( $prices as $min => $price_row){
+ // handle the case where pricing hasn't been set yet (new products etc.)
+ if(is_nan($min)){
+ $min = 0;
+ }
+
+ echo '<tr>';
+ printf('<td><input type="text" class="value-field" value="%d" size="8" name="nono_price_table_qty[]"></td>', $min);
+ printf('<td><input type="text" class="value-field" value="%s" size="10" name="nono_price_table_price[]"></td>', number_format ( $price_row['price'], 2, ',', '.') );
+ printf('<td><input type="text" value="%s" size="20" name="nono_price_table_label[]"></td>', $price_row['label']);
+ echo '<td><a href="#" class="add-row action-icon" /><a href="#" class="delete-row action-icon" /></td>';
+ echo '</tr>';
+ }
+ echo '</table>';
+ echo '</div>';
+ echo '</div>';
+ }
+
+
+ function save_pricing_info($post_id){
+
+ if( !isset($_POST) || !isset($_POST['nono_price_table_qty']) ) return; // Not a Variable product with pricing table
+ $pricing = array();
+ for( $i=0;$i<count($_POST['nono_price_table_qty']);$i++ ){
+ $index = absint(str_replace(',', '.', str_replace('.', '', $_POST['nono_price_table_qty'][$i])));
+ if( 0 == $index ) continue; // zero quantities are not allowed
+
+ $price = floatval(str_replace(',', '.', str_replace('.', '', $_POST['nono_price_table_price'][$i])));
+ $pricing[$index] = array(
+ 'price' => $price,
+ 'label' => stripslashes ( $_POST['nono_price_table_label'][$i] )
+ );
+ }
+ ksort($pricing, SORT_NUMERIC);
+ update_post_meta($post_id, self::$pricing_table_key, $pricing);
+kickout('save_pricing_info', $_POST, $post_id, $sorted_pricing);
- $index = array_search($variation_id, $_POST['variable_post_id']);
+ return;
if( false === $index ) return;
View
BIN  price-table.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
38 woo-per-unit-pricing.css
@@ -0,0 +1,38 @@
+#woocommerce-product-data ul.product_data_tabs li.nono-pricing-option a{
+ padding: 5px 5px 5px 28px;
+ background: #f1f1f1 url(images/price-table.png) no-repeat 5px 5px;
+}
+
+#nono-pricing-table-edit {}
+ #nono-pricing-table-edit thead td {
+ font-weight: bold;
+ }
+
+ #nono-pricing-table-edit .value-field {
+ text-align: right;
+ }
+ #nono-pricing-table-edit .action-icon {
+ display: inline-block;
+ width: 1rem;
+ height: 1rem;
+ background-size: contain;
+ background-repeat: no-repeat;
+ margin-right: .25rem;
+ }
+ #nono-pricing-table-edit .add-row {
+ background-image: url(images/Add.png);
+ }
+ #nono-pricing-table-edit .delete-row {
+ background-image: url(images/Delete.png);
+ }
+
+// HiDPI optimisation
+@media
+(-webkit-min-device-pixel-ratio: 2),
+(min-resolution: 192dpi) {
+ #woocommerce-product-data ul.product_data_tabs li.nono-pricing-option a {
+ background-image: url(images/price-table@2x.png) !important;
+ background-size: 17px 17px !important;
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.