Пример #1
0
 function process_order_bulk_actions()
 {
     global $typenow;
     if ('shop_order' == $typenow) {
         $wp_list_table = _get_list_table('WP_Posts_List_Table');
         $action = $wp_list_table->current_action();
         if (!in_array($action, array('wc_nfe_emitir'))) {
             return false;
         }
         if (isset($_REQUEST['post'])) {
             $order_ids = array_map('absint', $_REQUEST['post']);
         }
         if (empty($order_ids)) {
             return false;
         }
         if ($action == 'wc_nfe_emitir') {
             WC_NFe()->emitirNFe($order_ids);
         }
     }
 }
Пример #2
0
 function emitirNFe($order_ids = array())
 {
     foreach ($order_ids as $order_id) {
         $data = self::order_data($order_id);
         $webmaniabr = new NFe(WC_NFe()->settings);
         $response = $webmaniabr->emissaoNotaFiscal($data);
         if (isset($response->error) || $response->status == 'reprovado') {
             $mensagem = 'Erro ao emitir a NF-e do Pedido #' . $order_id . ':';
             $mensagem .= '<ul style="padding-left:20px;">';
             $mensagem .= '<li>' . $response->error . '</li>';
             if (isset($response->log)) {
                 if ($response->log->xMotivo) {
                     $mensagem .= '<li>' . $response->log->xMotivo . '</li>';
                 } else {
                     foreach ($response->log as $erros) {
                         foreach ($erros as $erro) {
                             $mensagem .= '<li>' . $erro . '</li>';
                         }
                     }
                 }
             }
             $mensagem .= '</ul>';
             WC_NFe()->add_error($mensagem);
         } else {
             $nfe = get_post_meta($order_id, 'nfe', true);
             if (!$nfe) {
                 $nfe = array();
             }
             $nfe[] = array('status' => (string) $response->status, 'chave_acesso' => $response->chave, 'n_recibo' => (int) $response->recibo, 'n_nfe' => (int) $response->nfe, 'n_serie' => (int) $response->serie, 'url_xml' => (string) $response->xml, 'url_danfe' => (string) $response->danfe, 'data' => date_i18n('d/m/Y'));
             update_post_meta($order_id, 'nfe', $nfe);
             WC_NFe()->add_success('NF-e emitida com sucesso do Pedido #' . $order_id);
         }
     }
 }