Skip to content

Commit

Permalink
Refactor to use new WritePanel with Sale Prices and Simple Product
Browse files Browse the repository at this point in the history
Don't need variants to make this work. In fact, it makes it harder
  • Loading branch information
1bigidea committed Apr 19, 2013
1 parent 2e3174d commit 8533fc9
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 121 deletions.
170 changes: 170 additions & 0 deletions inc/class.admin_product.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

class Nono_ProductAdmin {

function __construct(){
add_action('woocommerce_product_write_panel_tabs', array($this, 'add_panel') );
add_action('woocommerce_product_write_panels', array($this, 'show_panel'));

add_action('woocommerce_process_product_meta_simple', array($this, 'save_pricing_info'), 10 ,1);
}

/**
* Insert Custom Write Panel in Woo Product Editor
*/
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 show_panel(){
global $post;

$prices = get_post_meta($post->ID, NonoPrintPricing::$pricing_table_key, true);
if( empty($prices) ){
$prices = array(
'enabled' => false,
'enabled_sale' => false,
'regular' => array(
'empty1' => array('price' => '', 'label' => 'Digital'),
'empty2' => array('price' => '', 'label' => 'Offset')
),
'sale' => array(
'empty1' => array('price' => '', 'label' => 'Digital'),
'empty2' => array('price' => '', 'label' => 'Offset')
),
'sale_dates' => array( 'from' => '', 'thru' => '')
);
}

$show_per_unit = ($prices['enabled'] ) ? "visible" : "none";
$show_sales = ($prices['enabled_sale']) ? "visible" : "none";

echo '<div id="nono_price_panel" class="panel woocommerce_options_panel">';

echo '<div class="options_group nono-price-table">';

echo '<div class="options_group">';
echo '<p class="form-field">';
echo '<label for="enable-per-unit-pricing">';
_e('Enable Per Unit Pricing', 'nono-per-unit');
echo '</label>';
printf('<input type="checkbox" class="checkbox" id="enable-per-unit-pricing" name="enable_per_unit_pricing" value="1" %s /><span class="description">%s</span>', checked($prices['enabled'], '1', false), __('Use Qty-based Pricing', 'nono-per-unit') );
echo '</p>';

echo '</div>';

// On Sale Fields
printf ('<div class="options_group enable-per-unit-pricing" style="visibility:%s;">', $show_per_unit);
echo '<p class="form-field">';
echo '<label for="enable-sales-pricing">';
_e('Show Sale Pricing', 'nono-per-unit');
echo '</label>';
printf('<input type="checkbox" class="checkbox" id="enable-sales-pricing" name="per_unit_on_sale" value="1" %s /><span class="description">%s</span>', checked($prices['enabled_sale'], '1', false), '' );
echo '</p>';
printf('<div class="on-sale" style="visibility:%s;">', $show_sales);
echo '<p class="form-field sale_price_dates_fields">';
echo '<label for="sales-pricing-from">';
_e('Sale Price Dates:', 'nono-per-unit');
echo '</label>';
printf('<input type="text" class="short sale_price_dates_from" name="per_unit_sales_pricing_from" id="per-unit-sales-pricing-from" value="%s" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />', $prices['sale_dates']['from']);

printf('<input type="text" class="short" name="per_unit_sales_pricing_thru" id="per-unit-sales-pricing-thru" value="%s" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />', $prices['sale_dates']['thru']);
echo '</p>';
echo '</div>';
echo '</div>';
// Show Pricing Table
printf ('<div class="options_group enable-per-unit-pricing" style="visbility:%s;">', $show_per_unit);
echo '<table id="nono-pricing-table-edit">';
printf('<thead><td class="price-min">%s</td><td class="price-each">%s</td><td class="price-addl">%s</td><td class="price-label">%s</td><td class="price-row-action">&nbsp;</td></thead>',
__('Min Qty', 'nono-per-unit'),
__('Price Each', 'nono-per-unit'),
__('Price Add&rsquo;l', 'nono-per-unit'),
__('Label', 'nono-per-unit')
);
foreach( $prices['regular'] as $min => $price_row){
// handle the case where pricing hasn't been set yet (new products etc.)
if( ! is_numeric( $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" class="value-field" value="%s" size="10" name="nono_price_table_addl[]"></td>', number_format ( $price_row['addl'], 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>';

printf('<tr class="on-sale" style="visbility:%s;">', $show_sales) ;
printf('<td>%s</td>', __('Sale Prices', 'nono-per-unit'));
printf('<td><input type="text" class="value-field" value="%s" size="10" name="nono_price_table_saleprice[]"></td>', number_format ( $prices['sale'][$min]['price'], 2, ',', '.') );
printf('<td><input type="text" class="value-field" value="%s" size="10" name="nono_price_table_saleaddl[]"></td>', number_format ( $prices['sale'][$min]['addl'], 2, ',', '.') );
echo '<td></td>';
echo '</tr>';
}
echo '</table>';
echo '</div>';
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
if( !isset($_POST['enable_per_unit_pricing']) ) { // Per-unit pricing is unchecked
$per_unit_table = get_post_meta($post_id, NonoPrintPricing::$pricing_table_key, true);
if( is_array($per_unit_table) ){ // There is something stored here
$per_unit_table['enabled'] = false;
update_post_meta($post_id, NonoPrintPricing::$pricing_table_key, $per_unit_table);
}
return;
}

$pricing = $sales_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])));
$addl = floatval(str_replace(',', '.', str_replace('.', '', $_POST['nono_price_table_addl'][$i])));
$pricing[$index] = array(
'price' => $price,
'addl' => $addl,
'label' => stripslashes ( $_POST['nono_price_table_label'][$i] )
);

$sale_price = floatval(str_replace(',', '.', str_replace('.', '', $_POST['nono_price_table_saleprice'][$i])));
$sale_addl = floatval(str_replace(',', '.', str_replace('.', '', $_POST['nono_price_table_saleaddl'][$i])));
$sales_pricing[$index] = array(
'price' => $sale_price,
'addl' => $sale_addl
);
}
ksort($pricing, SORT_NUMERIC);
ksort($sales_pricing, SORT_NUMERIC);

$eff_date = $thru_date = '';
$on_sale = (isset($_POST['per_unit_on_sale']) ) ? true : false;
$eff_date = date('Y-m-d', strtotime($_POST['per_unit_sales_pricing_from']) ); // sanitize date fields
$thru_date = date('Y-m-d', strtotime($_POST['per_unit_sales_pricing_thru']) );

$per_unit_table = array(
'enabled' => true,
'enabled_sale' => $on_sale,
'regular' => $pricing,
'sale' => $sales_pricing,
'sale_dates'=> array(
'from' => $eff_date,
'thru' => $thru_date
)
);


update_post_meta($post_id, NonoPrintPricing::$pricing_table_key, $per_unit_table);
kickout('save_pricing_info', $_POST, $post_id, $per_unit_table);

return;
}

}
new Nono_ProductAdmin();
53 changes: 50 additions & 3 deletions inc/class.printing_product.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,16 +4,63 @@
*/ */


class NONO_Product_Variable extends WC_Product_Variable { class NONO_Product_Variable extends WC_Product_Variable {
var $min_order_qty;


public function __construct( $product ) {
parent::__construct( $product );

$prices = get_post_meta($product->ID, NonoPrintPricing::$pricing_table_key, true);
if( !is_array($prices) ) return;

ksort($prices, SORT_NUMERIC);
foreach($prices as $min => $details){
$this->min_order_qty = $min;
$this->price = $this->special_price = $this->regular_price = $details['price'];
break;
}

add_filter('woocommerce_quantity_input_args', array($this, 'set_min_quantities'), 10, 2);
}

function set_min_quantities($args, $product){
kickout('set_min_quantities_variable', $args, $product);
$args['input_value'] = $args['min_value'] = $this->min_order_qty;

return $args;
}
} }


class NONO_Product_Variation extends WC_Product_Variation { class NONO_Product_Variation extends WC_Product_Variation {
var $min_order_qty;


function __construct(){ function __construct($variation, $args = array() ){
parent::__construct(); parent::__construct($variation, $args);


$this->price = $this->special_price = $this->regular_price = (float) 1.00; $this->prices = get_post_meta($args['parent_id'], NonoPrintPricing::$pricing_table_key, true);
ksort($this->prices, SORT_NUMERIC);
foreach($this->prices as $min => $details){
$this->min_order_qty = $min;
$this->price = $this->special_price = $this->regular_price = $details['price'];
break;
}
add_filter('woocommerce_quantity_input_args', array($this, 'set_min_quantities'), 10, 2);


add_filter('woocommerce_available_variation', array($this, 'set_min_qty_attributes'), 10, 3);
} }


function set_min_quantities($args, $product){
kickout('set_min_quantities_variation', $args, $product);
$args['input_value'] = $args['min_value'] = $this->min_order_qty;

return $args;
}

function set_min_qty_attributes($attributes, $object, $variation){

$attributes['min_qty'] = $this->min_order_qty;
$attributes['price_html'] = $this->get_price_html();
$attributes['price_breaks'] = $this->prices;

return $attributes;
}
} }
21 changes: 5 additions & 16 deletions inc/class.shortcodes.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,32 +31,21 @@ function pricetable($atts){
extract( shortcode_atts(array('id' => 0, 'price_levels' => "50,100,250,500,1000"), $atts) ); extract( shortcode_atts(array('id' => 0, 'price_levels' => "50,100,250,500,1000"), $atts) );


if( 0 == $id ) { if( 0 == $id ) {
$id = $product->id; $the_product = $product;
} else {
$the_product = get_product($id);
} }
$rows = explode(',', $price_levels); $rows = explode(',', $price_levels);


$prices = get_post_meta($id, NonoPrintPricing::$pricing_table_key, true);
ksort($prices, SORT_NUMERIC);

$output = '<table class="price-table">'; $output = '<table class="price-table">';
$output .= sprintf('<thead><th>%s</th><th>%s</th></thead>', __('Qty', 'nono-per-unit'), __('Price', 'nono-per-unit')); $output .= sprintf('<thead><th>%s</th><th>%s</th></thead>', __('Qty', 'nono-per-unit'), __('Price', 'nono-per-unit'));
foreach( $rows as $row ){ foreach( $rows as $qty ){
$row_text = '<tr><td class="price-table-qty">%d</td><td class="price-table-amount">%s</td></tr>'; $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) $qty, number_format(NonoPrintPricing::determine_price($the_product, $qty), 2, ',', '.') );
} }
$output .= '</table>'; $output .= '</table>';


return $output; return $output;
} }

function determine_price($qty, $prices){
$pricing = 0;
foreach($prices as $min => $details ){
if( $qty >= $min ) {
$pricing = (($min * $details['price']) + ( ($qty - $min) * $details['price'] ));
}
}
return $pricing;
}
} }
new NonoPrintPricingShortcodes; new NonoPrintPricingShortcodes;
4 changes: 4 additions & 0 deletions js/accounting.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions js/nono-per-unit.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,51 @@
jQuery(function($){
$('form .quantity .qty').on('change', function(){
qty = $(this).val();

var all_set = true;
var any_set = false;
var current_settings = {};
var all_variations = $variation_form.data( 'product_variations' );

$('.variations select').each( function() {

if ( $(this).val().length == 0 ) {
all_set = false;
} else {
any_set = true;
}

// Encode entities
value = $(this).val();

// Add to settings array
current_settings[ $(this).attr('name') ] = value;

});

var matching_variations = $.fn.wc_variation_form.find_matching_variations( all_variations, current_settings );

if ( all_set ) {

var variation = matching_variations.pop();

if ( variation ) {

the_break = price_index(qty, Object.keys(variation.price_breaks));
console.log(variation.price_breaks[the_break], the_break, variation, matching_variations);
console.log(accounting.formatMoney(4999.99, "€", 2, ".", ","));

}
}

//$variation_form.find('.single_variation').html( variation.price_html + variation.availability_html );
});
});
var price_index = function(qty, break_points){

index = break_points[0];
for(i=0; i<break_points.length; i++){
if( qty >= break_points[i]) index = break_points[i];
}
return index;
};
22 changes: 20 additions & 2 deletions js/product-editor.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ jQuery(function($){
row = $(this).closest('tr'); row = $(this).closest('tr');
new_row = $(row).clone(); new_row = $(row).clone();
$(new_row).find('input').val(''); $(new_row).find('input').val('');
$(row).after(new_row);
nextrow = $(row).next();
new_sales_row = $(nextrow).clone();
$(new_sales_row).find('input').val('');

$(nextrow).after(new_sales_row).after(new_row);

return false; return false;
}); });


$('#nono-pricing-table-edit').delegate('.delete-row', 'click', function(e){ $('#nono-pricing-table-edit').delegate('.delete-row', 'click', function(e){
row = $(this).closest('tr').remove(); row = $(this).closest('tr');
nextrow = $(row).next();

$(nextrow).remove();
$(row).remove();

return false; return false;
}); });

$('#enable-sales-pricing').change(function(){
$('.on-sale', '#nono_price_panel').toggle();
});
$('#enable-per-unit-pricing').change(function(){
$('.enable-per-unit-pricing', '#nono_price_panel').toggle();
});
}); });
Loading

0 comments on commit 8533fc9

Please sign in to comment.