Membership 2: Show total sales

Please add a feature that will calculate sales sorted by time in Billing section.

  • Ivan

    Hi TINT. !

    You could try adding the following snippet in a MU plugin ( more info about MU plugins is here )

    <?php
    //Add Show Total Sales link
    add_filter( 'ms_view_billing_list', 'wpmu_add_total_sales_link', 10, 2 );
    function wpmu_add_total_sales_link( $html, $obj ) {
    	$button= array(
    		'type' => MS_Helper_Html::TYPE_HTML_LINK,
    		'url' => MS_Controller_Plugin::get_admin_url(
    			'billing',
    			array( 'show' => 'total_sales' )
    		),
    		'value' => __( 'Show Total Sales', 'membership2' ),
    		'class' => 'button',
    	);
    	ob_start();
    	MS_Helper_Html::html_element( $button );
    	$total_sales_link = ob_get_clean();
    	$html = preg_replace( '/show=logs" target=[^<]*<\/a>/', '$0' . $total_sales_link, $html );
    
    	return $html;
    }
    
    //Calculate Total Sales
    add_action( 'wp_ajax_wpmu_membership_get_total_sales', 'wpmu_membership_get_total_sales' );
    function wpmu_membership_get_total_sales() {
    	$start_date = filter_input( INPUT_POST, 'start_date' );
    	$end_date = filter_input( INPUT_POST, 'end_date' );
    	$total = 0;
    
    	$args = array ( 'posts_per_page' => -1, 'offset' => 0, 'state' => 'ok', );
    	if ( $end_date && check_date( $end_date ) ) {
    		$args['date_query'][0]['before'] = $end_date;
    		$restricts = true;
    	}
    	if ( $start_date && check_date( $start_date ) ) {
    		$args['date_query'][0]['after'] = $start_date;
    		$restricts = true;
    	}
    	if ( !empty( $restricts ) ) {
    		$args['date_query'][0]['inclusive'] = true;
    	}
    	$items = MS_Model_Transactionlog::get_items( $args );
    	foreach ( $items as $item ) {
    		$total += $item->amount;
    	}
    	wp_send_json_success( $total );
    }
    
    //Main functionality for Total Sales page
    $show = filter_input( INPUT_GET, 'show', FILTER_SANITIZE_STRING );
    if ( 'total_sales' === $show ) {
    	add_action( 'plugins_loaded', 'wpmu_init_membership_total_sales' );
    	add_action( 'admin_print_scripts-membership-2_page_membership2-billing', 'wpmu_membership_total_sales_enqueue_scripts' );
    	add_action( 'admin_print_styles-membership-2_page_membership2-billing', 'wpmu_membership_total_sales_enqueue_styles' );
    	function wpmu_membership_total_sales_enqueue_scripts() {
    		wp_enqueue_script( 'jquery-ui-datepicker' );
    		wp_enqueue_script( 'jquery-validate' );
    		$script = <<<EOF
    (function ($) {
    $(function () {
    	$('#calculate').on('click',function(){
    		var start_date = $('#start_date').val();
    		var end_date = $('#end_date').val();
    		$.post( ajaxurl, { action: 'wpmu_membership_get_total_sales',start_date: start_date, end_date: end_date }, function(data) {
    			$('#total').val(data.data);
    		});
    		return false;
    	});
    });
    })(jQuery);
    EOF;
    		wp_add_inline_script('jquery-validate', $script );
    	}
    	function wpmu_membership_total_sales_enqueue_styles() {
    		lib3()->ui->add( 'jquery-ui' );
    	}
    }
    
    function wpmu_init_membership_total_sales() {
    	/**
    	 * Render Invoice add/edit view.
    	 *
    	 * Extends MS_View for rendering methods and magic methods.
    	 *
    	 * @since  1.0.0
    	 *
    	 * @package Membership2
    	 * @subpackage View
    	 */
    	class MS_View_Billing_Total extends MS_View {
    
    		/**
    		 * Calculate Total Sales.
    		 *
    		 * @since  1.0.0
    		 *
    		 * @return string
    		 */
    		public function to_html() {
    			$this->check_simulation();
    
    			$fields 	= $this->prepare_fields();
    			$form_url 	= '#';
    
    			$title = __( 'Total Sales', 'membership2' );
    
    			ob_start();
    			// Render tabbed interface.
    			?>
    				<div class="ms-wrap ms-billing">
    					<?php
    					MS_Helper_Html::settings_header(
    						array(
    							'title' 			=> $title,
    							'title_icon_class' 	=> 'wpmui-fa wpmui-fa-pencil-square',
    						)
    					);
    					?>
    					<form action="<?php echo $form_url; ?>" method="post" class="ms-form">
    						<?php MS_Helper_Html::settings_box( $fields, '', '', 'static', 'ms-small-form' ); ?>
    					</form>
    					<div class="clear"></div>
    				</div>
    			<?php
    			$html = ob_get_clean();
    
    			return apply_filters( 'ms_view_billing_total_sales_to_html', $html, $this );
    		}
    
    		/**
    		 * Prepare html fields.
    		 *
    		 * @since  1.0.0
    		 *
    		 * @return array
    		 */
    		function prepare_fields() {
    			$currency 			= MS_Plugin::instance()->settings->currency;
    			$fields = array(
    				'start_date' 			=> array(
    					'id' 				=> 'start_date',
    					'title' 			=> __( 'Start date', 'membership2' ),
    					'type' 				=> MS_Helper_Html::INPUT_TYPE_DATEPICKER,
    				),
    				'end_date' 			=> array(
    					'id' 				=> 'end_date',
    					'title' 			=> __( 'End date', 'membership2' ),
    					'type' 				=> MS_Helper_Html::INPUT_TYPE_DATEPICKER,
    				),
    				'total' 		=> array(
    					'id' 				=> 'total',
    					'title' 			=> sprintf( __( 'Total (%s)', 'membership2' ), $currency ),
    					'type' 				=> MS_Helper_Html::INPUT_TYPE_NUMBER,
    					'read_only' 		=> true,
    				),
    				'separator' 		=> array(
    					'type' 				=> MS_Helper_Html::TYPE_HTML_SEPARATOR,
    				),
    				'submit' 			=> array(
    					'id' 				=> 'calculate',
    					'type' 				=> MS_Helper_Html::INPUT_TYPE_SUBMIT,
    					'value' 			=> __( 'Calculate', 'membership2' ),
    				),
    			);
    
    			return $fields;
    		}
    	}
    
    	function wpmu_membership_total_sales( $html, $obj ) {
    		$view = MS_Factory::create( 'MS_View_Billing_Total' );
    		remove_filter( 'ms_view_render', 'wpmu_membership_total_sales' );
    		$view->render();
    
    		return '';
    	}
    	add_filter( 'ms_view_render', 'wpmu_membership_total_sales', 10, 2 );
    }
    
    //help function for checking dates
    function check_date( $str ) {
        try {
            $dt = new DateTime( trim($str) );
        }
        catch( Exception $e ) {
            return false;
        }
        $month = $dt->format('m');
        $day = $dt->format('d');
        $year = $dt->format('Y');
        if( checkdate($month, $day, $year) ) {
            return true;
        }
        else {
            return false;
        }
    }

    Then you can see this link for Calculating Total Sales -
    https://i.imgur.com/voI1qnb.png

    Best,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.