Pridanie widgetu TAG na stránku objednávky

Pridanie widgetu TAG OBJEDNÁVKY na bočný panel na stránke objednávky /order page


// Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
if ( ! function_exists( 'mv_add_meta_boxes' ) )
{
    function mv_add_meta_boxes()
    {
        add_meta_box( 'mv_other_fields', __('TAG OBJEDNÁVKY','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' );
    }
}


// Adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'mv_add_other_fields_for_packaging' ) )
{
    function mv_add_other_fields_for_packaging()
    {
        global $post;

        $meta_field_data = get_post_meta( $post->ID, '_moj_order_tag', true ) ? get_post_meta( $post->ID, '_moj_order_tag', true ) : '';
		$meta_field_data2 = get_post_meta( $post->ID, '_moj_order_tag2', true ) ? get_post_meta( $post->ID, '_moj_order_tag2', true ) : '';

		echo '
            <form class="misc-pub-section my-options">
            <input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">
                <select id="my_order_tag" name="my_order_tag_field_name">
                <option value="" selected disabled hidden>' . $meta_field_data . '</option>
                <option style="background-color:pink" value="Čaká na odpoveď 1">Čaká na odpoveď 1</option>
                <option style="background-color:#82CAAF" value="Čaká na odpoveď 2">Čaká na odpoveď 2</option>
                <option style="background-color:#FECF6A" value="Osobný odber">Osobný odber</option>
                </select>
                <button type="" class="button save_order button-primary">Odoslať</button>
			</form>';

		echo ' 
            <p style="border-bottom:solid 1px #eee;padding-bottom:13px;"> Poznámka:
                <input type="text" style="width:250px;" name="my_order_tag_field_name2" placeholder="' . $meta_field_data2 . '" value="' . $meta_field_data2 . '">
            </p>';
    }
}

// Save the data of the Meta field
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{

    function mv_save_wc_order_other_fields( $post_id ) {

        // We need to verify this with the proper authorization (security stuff).

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) ) {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) ) {
            return $post_id;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }
        // --- Its safe for us to save the data ! --- //

        // Sanitize user input  and update the meta field in the database.
        update_post_meta( $post_id, '_moj_order_tag', $_POST[ 'my_order_tag_field_name' ] );
		update_post_meta( $post_id, '_moj_order_tag2', $_POST[ 'my_order_tag_field_name2' ] );
    }
}

Pridanie stĺpca v zozname objednávok ktorý bude zobrazovať tagy

/// stlpec v objednavkach

// With this filter we can register the "header" part
add_filter( 'manage_edit-shop_order_columns', 'add_tag_order_columns_to_orders_table', 11 );
// add columns shipping_and_payment
function add_tag_order_columns_to_orders_table($columns) {
	$columns['TAG'] = 'TAG';
    return $columns;
}

function get_tag_order_columns_values($column) {
    global $post;
    $order_facory = new WC_Order_Factory();
    $order = $order_facory->get_order( $post->ID );

    if ( $column == 'TAG' ) {    
    echo   get_post_meta( $post->ID, '_moj_order_tag', true ) ?  '<span style="background-color:pink; padding:10px">' . get_post_meta( $post->ID, '_moj_order_tag', true ) . '<span>' : '';;
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'get_tag_order_columns_values', 2 );

Filtrovanie objednávok podľa tagu

//// FILTROVANIE

add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter(){
    global $pagenow, $post_type;

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
        $domain    = 'woocommerce';
        $languages = array( __('Osobný odber', $domain), __('Čaká na odpoveď 2', $domain) );
        $current   = isset($_GET['filter_tag_action'])? $_GET['filter_tag_action'] : '';

        echo '<select name="filter_tag_action">
        <option value="">' . __('Filtruj TAG ', $domain) . '</option>';

        foreach ( $languages as $value ) {
            printf( '<option value="%s"%s>%s</option>', $value, 
                $value === $current ? '" selected="selected"' : '', $value );
        }
        echo '</select>';
    }
}

add_action( 'pre_get_posts', 'process_admin_shop_order_language_filter' );
function process_admin_shop_order_language_filter( $query ) {
    global $pagenow;

    if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['filter_tag_action'] ) 
        && $_GET['filter_tag_action'] != '' && $_GET['post_type'] == 'shop_order' ) {

        $meta_query = $query->get( 'meta_query' ); // Get the current "meta query"
        $meta_query = array( // Add to "meta query"
			array(
            'key' => '_moj_order_tag',
            'value'    => esc_attr( $_GET['filter_tag_action']) ,  //esc_attr( $_GET['filter_tag_action'])
			)
        );
		
        $query->set( 'meta_query', $meta_query ); // Set the new "meta query"
        $query->set( 'posts_per_page', 10 ); // Set "posts per page"
        $query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
    }
}

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená.