Cum pot genera automat facturi doar pentru comenzile achitate prin ramburs (COD)?

1 min read

Aceast snippet de cod are rolul de a automatiza procesul de generare a facturilor doar pentru comenzile care sunt achitate prin ramburs (COD – Cash On Delivery).

Codul de mai jos trebuie adăugat în fișierul functions.php al temei child:

function curiero_autogenerate_invoice(int $order_id, string $awb_status): void
{
    if (!in_array($awb_status, DELIVERED_AWB_STATUSES)) {
        return;
    }

    $order = curiero_get_order($order_id);

    // Do not generate invoice if payment method is not COD
    if ($order->get_payment_method() !== ‘cod’) {
        return;
    }

    $invoice_systems = [
        ‘smartbill’ => [
            ‘option’ => ‘enable_automatic_smartbill’,
            ‘function’ => ‘smartbill_create_document’,
            ‘meta_key’ => ‘smartbill_private_link’,
        ],
        ‘oblio’ => [
            ‘option’ => ‘enable_automatic_oblio’,
            ‘function’ => ‘_wp_oblio_generate_invoice’,
            ‘meta_key’ => ‘oblio_invoice_link’,
        ],
        ‘fgo’ => [
            ‘option’ => ‘enable_automatic_fgo’,
            ‘function’ => [WC_FGO_Premium_Order::class, ‘buildFactura’],
            ‘meta_key’ => ‘_fgo_invoice_link’,
        ],
    ];

    foreach ($invoice_systems as $system => $config) {
        if (get_option($config[‘option’]) === ‘1’) {
            if (
                (is_callable($config[‘function’]))
                && empty($order->get_meta($config[‘meta_key’], true))
            ) {
                call_user_func($config[‘function’], $order_id);
            }

            return;  // Exit after processing the active invoice system
        }
    }
}