Add user filter in a function

Asked

Viewed 38 times

1

The function below issues tax notes automatically using This plugin (Electronic Invoice Woocommerce) when the order status is completed.

function issue_automatic_invoice( $order_id, $from, $to, $order ) {
    
    // Validate
    $option = apply_filters( 'nfe_issue_automatic', get_option('wc_settings_woocommercenfe_emissao_automatica') );
    if ( !$option ){
        return;
    }

    // Process
    if (
        $to == 'processing' && ($option == 1 || $option == 'yes') ||
        $to == 'completed' && $option == 2
    ){

        $nfes = get_post_meta( $order_id, 'nfe', true );

        if ( !empty($nfes) && is_array($nfes) ) {
            foreach ( $nfes as $nfe ) {
                if ( $nfe['status'] == 'aprovado' ) {
                    return;
                }
            }
        }

        $nf = new WooCommerceNFeIssue;
        $response = $nf->send( array( $order_id ) );

    }

    do_action( 'nfe_issued_automatic', $order_id, $to, $response );

}

How would it be possible to add a if not to issue if the originator of the request is admin?

I want the function to be performed only on customers' orders and not on mine.

2 answers

2


You can search the CPF registered in the request to be processed and execute the function only if the CPF is different from yours, for example:

function issue_automatic_invoice( $order_id, $from, $to, $order ) {
    $format = new WooCommerceNFeFormat;
    $checaCPF = $format->format_number( get_post_meta( $order->get_id(), '_billing_cpf', true ) );
    if ( $checaCPF != 12345678900){
    
        // Validate
        $option = apply_filters( 'nfe_issue_automatic', get_option('wc_settings_woocommercenfe_emissao_automatica') );
        if ( !$option ){
            return;
        }

        // Process
        if (
            $to == 'processing' && ($option == 1 || $option == 'yes') ||
            $to == 'completed' && $option == 2
        ){

            $nfes = get_post_meta( $order_id, 'nfe', true );

            if ( !empty($nfes) && is_array($nfes) ) {
                foreach ( $nfes as $nfe ) {
                    if ( $nfe['status'] == 'aprovado' ) {
                        return;
                    }
                }
            }

            $nf = new WooCommerceNFeIssue;
            $response = $nf->send( array( $order_id ) );

        }

        do_action( 'nfe_issued_automatic', $order_id, $to, $response );     }
}

1

You can do an if before calling the function:

if(cliente) {
    issue_automatic_invoice( $order_id, $from, $to, $order )
} else {
    die('admin')
}

hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.