View Categories

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, curiero_get_delivered_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',
            'args' => [1, $order_id],
        ],
        'oblio' => [
            'option' => 'enable_automatic_oblio',
            'function' => '_wp_oblio_generate_invoice',
            'meta_key' => 'oblio_invoice_link',
            'args' => [$order_id],
        ],
        'fgo' => [
            'option' => 'enable_automatic_fgo',
            'function' => [WC_FGO_Premium_Order::class, 'buildFactura'],
            'meta_key' => '_fgo_invoice_link',
            'args' => [$order_id],
        ],
    ];

    foreach ($invoice_systems as $system => $config) {
        if (
            get_option($config['option']) === '1'
            && is_callable($config['function'])
            && empty($order->get_meta($config['meta_key'], true))
        ) {
            if (
                $system === 'smartbill'
                && (new ReflectionFunction('smartbill_create_document'))->getNumberOfParameters() < 3
            ) {
                $config['args'] = [$order_id];
            }

            call_user_func_array($config['function'], $config['args']);

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