Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add pricetable shortcode

Add new class for shortcodes
  • Loading branch information...
commit dde1966bf89eda54303e9af0d9b627a8808ed299 1 parent 07211db
@1bigidea authored
Showing with 78 additions and 1 deletion.
  1. +77 −0 inc/class.shortcodes.php
  2. +1 −1  loader.php
View
77 inc/class.shortcodes.php
@@ -0,0 +1,77 @@
+<?php
+
+class NonoPrintPricingShortcodes {
+ private static $_this;
+ var $plugin_slug = "NonoPrintPricingShortcodes";
+ var $plugin_name = "";
+ var $plugin_version = "1.0";
+
+ function __construct() {
+
+ add_shortcode('pricetable', array($this, 'pricetable'));
+
+ }
+ static function this(){
+ // enables external management of filters/actions
+ // http://hardcorewp.com/2012/enabling-action-and-filter-hook-removal-from-class-based-wordpress-plugins/
+ // enables external management of filters/actions
+ // http://hardcorewp.com/2012/enabling-action-and-filter-hook-removal-from-class-based-wordpress-plugins/
+ // http://7php.com/how-to-code-a-singleton-design-pattern-in-php-5/
+ if( !is_object(self::$_this) ) self::$_this = new NonoPrintPricingShortcodes();
+
+ return self::$_this;
+ }
+ /*
+ * Functions below actually do the work
+ */
+
+ function pricetable($atts){
+ global $woocommerce, $product;
+
+ extract( shortcode_atts(array('id' => 0, 'qty' => "50,100,250,500,1000"), $atts) );
+
+ if( 0 == $id ) {
+ $this_product = $product;
+ } else {
+ // get the product
+ }
+
+ $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);
+
+ $output = '<table class="price-table">';
+ 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 .= '</table>';
+
+ return $output;
+ }
+
+ 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;
+ }
+ }
+ return $pricing;
+ }
+}
+new NonoPrintPricingShortcodes;
View
2  loader.php
@@ -69,7 +69,7 @@ static function this(){
* Functions below actually do the work
*/
function init(){
-
+ include_once('inc/class.shortcodes.php');
}
function admin_init(){
Please sign in to comment.
Something went wrong with that request. Please try again.