Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active May 22, 2019 03:21
Show Gist options
  • Save wpmudev-sls/9582a9d7b7a52718b62b1e42d9f5de0f to your computer and use it in GitHub Desktop.
Save wpmudev-sls/9582a9d7b7a52718b62b1e42d9f5de0f to your computer and use it in GitHub Desktop.
[MarketPress] - Custom Order Numbers
<?php
/**
* Plugin Name: [MarketPress] - Custom Order Numbers
* Description: [MarketPress] - Custom Order Numbers - 1121058994277124
* Plugin URI: https://premium.wpmudev.org/
* Author: WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_mp_custom_order_number_func', 100 );
function wpmudev_mp_custom_order_number_func() {
if ( defined( 'MP_VERSION' ) && class_exists( 'Marketpress' ) ) {
class WPMUDEV_MP_Custom_Order_Number{
private $init_order_number = 0;
private $order_number_len = 1;
private $current_order_number = 0;
public function __construct(){
add_action( 'admin_init', array( $this, 'custom_order_on_admin_order' ) );
$this->current_order_number = get_option( 'wpmudev_mp_current_order_number' );
// add option reset order number
if( isset( $_GET['wpmudev-mp-reset-order-number'] ) ){
$this->current_order_number = false;
}
if( false === $this->current_order_number ){
add_action( 'init', array( $this, 'update_meta_order_number') );
}
add_action( 'save_post_mp_order', array( $this, 'add_meta_order_number' ), 10, 3 );
// pdf invoice
// add_filter( 'mp_pdf_invoice_button_params', array( $this, 'pdf_invoice_button_params'), 10, 3 );
add_filter( 'mp_pdf_invoice_params', array( $this, 'change_invoice_id_on_pdf_invoice' ) );
// replace order number in Order Status
add_filter( 'mp_order/header', array( $this, 'change_order_number_in_render_text'), 10, 2 );
// custom order in Email
// for customer
add_filter( 'mp_order/notification_subject', array( $this, 'change_order_number_in_render_text'), 10, 2);
add_filter( 'mp_order/notification_body', array( $this, 'change_order_number_in_render_text'), 10, 2);
// for admin
add_filter( 'mp_order_notification_admin_subject', array( $this, 'change_order_number_in_render_text'), 10, 2);
add_filter( 'mp_order_notification_admin_msg', array( $this, 'change_order_number_in_render_text'), 10, 2 );
// setting options
add_action( 'wpmudev_metabox/before_settings_metabox/mp-settings-general-advanced-settings', array( $this, 'add_start_order_number_option') );
add_action( 'wpmudev_metabox/before_save_fields/mp-settings-general-advanced-settings', array( $this, 'add_start_order_number_option') );
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
$this->init_order_number = mp_get_setting('init_order_number', '00000');
$this->order_number_len = strlen( $this->init_order_number );
$this->init_order_number = (int) $this->init_order_number;
}
public function update_meta_order_number()
{
global $wpdb;
$orders = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_status <> %s ORDER BY ID ASC", 'mp_order', 'trash'));
if( ! empty( $orders ) ){
$this->current_order_number = 0;
foreach( $orders as $order ){
$this->current_order_number ++;
update_post_meta( $order->ID, 'mp_order_number', $this->current_order_number );
}
update_option( 'wpmudev_mp_current_order_number', $this->current_order_number, false );
}
}
public function add_meta_order_number( $post_id, $post, $update )
{
if( $update ) return;
$this->current_order_number += 1;
if( add_post_meta( $post_id, 'mp_order_number', $this->current_order_number, true ) ){
update_option( 'wpmudev_mp_current_order_number', $this->current_order_number, false );
}else{
$this->current_order_number -= 1;
}
}
public function custom_order_on_admin_order()
{
global $pagenow;
if( $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'mp_order' ){
add_filter( 'the_title', array( $this, 'custom_order_via_title'), 10, 2 );
}
}
public function change_order_number_in_render_text( $text, $order ){
$text = str_replace($order->get_id(), $this->get_custom_order( $order->ID ), $text);
return $text;
}
public function get_custom_order($id)
{
if( ! is_numeric( $id ) ){
$order = new MP_Order( $id );
$id = $order->ID;
}
$order_number = get_post_meta( $id, 'mp_order_number', true );
if( false !== $order_number ){
$id = $order_number;
}
$order_number = $this->init_order_number + $id;
if( $len = strlen( $order_number ) < $this->order_number_len ){
$order_number = sprintf("%0{$this->order_number_len}d", $order_number );
}
return $order_number;
}
public function custom_order_via_title( $order_number, $id )
{
if( get_post_type( $id ) === 'mp_order' ){
$order_number = $this->get_custom_order( $id );
}
return $order_number;
}
public function pdf_invoice_button_params( $button_params, $type, $order )
{
if( isset( $button_params['order_id'] ) ){
$button_params['order_id'] = $order->ID;
}
return $button_params;
}
public function change_invoice_id_on_pdf_invoice($data){
if( isset( $data['{{order_id}}'] ) ){
$custom_order_id = $this->get_custom_order( $data['{{order_id}}'] );
$data['{{order_id}}'] = $custom_order_id;
}
return $data;
}
public function add_start_order_number_option($metabox)
{
$metabox->add_field('text',array(
'name' => 'init_order_number',
'label' => array( 'text' => __( 'Initial Order Number', 'mp' ) ),
'desc' => __( 'Enter your initial order number', 'mp' ),
'default_value' => '00000',
'style' => 'width:150px;'
));
}
}
$run = new WPMUDEV_MP_Custom_Order_Number();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment