jacekwilczynski

Dostosowania WooCommerce wFirma

Jul 26th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.  
  3. // Add payment method information to each invoice
  4. add_filter('woocommerce_wfirma_invoice_data', function (array $invoice, WC_Order $order) {
  5.     if (preg_match('/tpay/i', $order->get_payment_method_title())) {
  6.         if (isset($invoice['description']) && is_string($invoice['description'])) {
  7.             $invoice['description'] .= ', ';
  8.         } else {
  9.             $invoice['description'] = '';
  10.         }
  11.         $invoice['description'] .= __('Zapłacono przez') . ': tpay.com';
  12.     }
  13.  
  14.     return $invoice;
  15. }, 10, 2);
  16.  
  17. // Format invoice number for order export
  18. $formatInvoiceNumberForOrderExport = function ($output) {
  19.     if (isset($output['_woo_wfirma_faktura'])) {
  20.         try {
  21.             $invoiceData   = unserialize($output['_woo_wfirma_faktura']);
  22.             $invoiceNumber = $invoiceData['numer'];
  23.             assert(is_string($invoiceNumber));
  24.             $output['_woo_wfirma_faktura'] = $invoiceNumber;
  25.         } catch (Throwable $e) {
  26.             $output['_woo_wfirma_faktura'] = '';
  27.         }
  28.     }
  29.  
  30.     return $output;
  31. };
  32. foreach (['xls', 'csv', 'xml', 'json', 'tsv', 'pdf'] as $format) {
  33.     add_filter("woe_{$format}_output_filter", $formatInvoiceNumberForOrderExport);
  34. }
  35.  
  36. // Send invoice to admin, too
  37. add_filter('woocommerce_email_recipient_woo_wfirma_faktura', function (?string $recipient) {
  38.     return $recipient ? $recipient . ', ' . get_bloginfo('admin_email') : $recipient;
  39. }, 10, 1);
  40.  
  41. // Force issuing invoices while keeping sending them to the customer optional
  42. add_action("woocommerce_order_status_changed", function (int $orderId) {
  43.     $getWfirmaIntegration = function (): ?Woocommerce_wFirma {
  44.         try {
  45.             $wFirmaIntegration = array_find(
  46.                 wc()->integrations->get_integrations(),
  47.                 function (WC_Integration $integration) {
  48.                     return $integration instanceof Woocommerce_wFirma;
  49.                 }
  50.             );
  51.  
  52.             return $wFirmaIntegration;
  53.         } catch (Throwable $e) {
  54.             return null;
  55.         }
  56.     };
  57.  
  58.     try {
  59.         $buyerWantsInvoice = get_post_meta($orderId, '_billing_faktura', true);
  60.         $wFirma            = $getWfirmaIntegration();
  61.         if ((float)wc_get_order($orderId)->get_total() > 0) {
  62.             $wFirma->wystawianie_faktur = Woocommerce_Faktura_Integration::GENERATE_ALWAYS;
  63.             $wFirma->wysylka_faktur     = $buyerWantsInvoice ? 'yes' : 'no';
  64.         } else {
  65.             $wFirma->wystawianie_faktur = Woocommerce_Faktura_Integration::GENERATE_MANUALLY;
  66.         }
  67.     } catch (Throwable $e) {
  68.         error_log($e);
  69.     }
  70.  
  71. }, 0, 1);
Advertisement
Add Comment
Please, Sign In to add comment