Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Add payment method information to each invoice
- add_filter('woocommerce_wfirma_invoice_data', function (array $invoice, WC_Order $order) {
- if (preg_match('/tpay/i', $order->get_payment_method_title())) {
- if (isset($invoice['description']) && is_string($invoice['description'])) {
- $invoice['description'] .= ', ';
- } else {
- $invoice['description'] = '';
- }
- $invoice['description'] .= __('Zapłacono przez') . ': tpay.com';
- }
- return $invoice;
- }, 10, 2);
- // Format invoice number for order export
- $formatInvoiceNumberForOrderExport = function ($output) {
- if (isset($output['_woo_wfirma_faktura'])) {
- try {
- $invoiceData = unserialize($output['_woo_wfirma_faktura']);
- $invoiceNumber = $invoiceData['numer'];
- assert(is_string($invoiceNumber));
- $output['_woo_wfirma_faktura'] = $invoiceNumber;
- } catch (Throwable $e) {
- $output['_woo_wfirma_faktura'] = '';
- }
- }
- return $output;
- };
- foreach (['xls', 'csv', 'xml', 'json', 'tsv', 'pdf'] as $format) {
- add_filter("woe_{$format}_output_filter", $formatInvoiceNumberForOrderExport);
- }
- // Send invoice to admin, too
- add_filter('woocommerce_email_recipient_woo_wfirma_faktura', function (?string $recipient) {
- return $recipient ? $recipient . ', ' . get_bloginfo('admin_email') : $recipient;
- }, 10, 1);
- // Force issuing invoices while keeping sending them to the customer optional
- add_action("woocommerce_order_status_changed", function (int $orderId) {
- $getWfirmaIntegration = function (): ?Woocommerce_wFirma {
- try {
- $wFirmaIntegration = array_find(
- wc()->integrations->get_integrations(),
- function (WC_Integration $integration) {
- return $integration instanceof Woocommerce_wFirma;
- }
- );
- return $wFirmaIntegration;
- } catch (Throwable $e) {
- return null;
- }
- };
- try {
- $buyerWantsInvoice = get_post_meta($orderId, '_billing_faktura', true);
- $wFirma = $getWfirmaIntegration();
- if ((float)wc_get_order($orderId)->get_total() > 0) {
- $wFirma->wystawianie_faktur = Woocommerce_Faktura_Integration::GENERATE_ALWAYS;
- $wFirma->wysylka_faktur = $buyerWantsInvoice ? 'yes' : 'no';
- } else {
- $wFirma->wystawianie_faktur = Woocommerce_Faktura_Integration::GENERATE_MANUALLY;
- }
- } catch (Throwable $e) {
- error_log($e);
- }
- }, 0, 1);
Advertisement
Add Comment
Please, Sign In to add comment