• Jouke

    (@joukeschotanus)


    Hello, I have a license that can be sold multiple times. When a customer places and order, the plugin gives the status of the license key to “delivered” and sends it. I want only for this particular key the status to always stay on “active” and that the plugin sends this particular key with every new order for this particular product. However, I can’t seem to find how to install that. Is that even possible?

    I tried adding the following code in the functions.php file from my child theme but without success:

    add_action(‘woocommerce_order_status_processing’, ‘reset_license_after_order’);
    function reset_license_after_order($order_id) {
    // Haal de bestelling op
    $order = wc_get_order($order_id);

    // Log de status van de bestelling
    error_log("Order status is now: " . $order->get_status()); // Dit logt de huidige status van de bestelling

    // Controleer of de bestelling het product heeft dat de licentie gebruikt
    $target_product_id = XXXXX; // Product ID waarvoor we de licentie willen resetten
    $target_license_id = XXXXX; // Licentie ID die we willen resetten

    // Loop door de items in de bestelling en zoek naar het specifieke product
    $license_updated = false;
    foreach ($order->get_items() as $item_id => $item) {
    $product_id = $item->get_product_id();
    if ($product_id == $target_product_id) {
    // Licentie reset nodig
    global $wpdb;

    // Gebruik de volledige tabelnaam zonder de prefix
    $table_name = 'tabel-name'; // Volledige tabelnaam zonder de prefix

    // Update de licentie status naar 'actief' (3) en verwijder de koppeling met klant en bestelling
    $result = $wpdb->update(
    $table_name,
    array(
    'status' => 3, // Actief
    'user_id' => NULL, // Koppel klant los
    'order_id' => NULL, // Koppel bestelling los
    ),
    array('id' => $target_license_id), // Gebruik hier de kolomnaam 'id' in plaats van 'license_id'
    array('%d', '%d', '%d'),
    array('%d')
    );

    // Log de updates
    if ($result !== false) {
    error_log("License status updated for id: $target_license_id to 'actief'.");
    error_log("License user_id set to NULL for id: $target_license_id.");
    error_log("License order_id set to NULL for id: $target_license_id.");
    $license_updated = true;
    } else {
    error_log("Failed to update license with id: $target_license_id");
    }

    // Tweede methode via directe SQL
    $sql = $wpdb->prepare(
    "UPDATE $table_name SET status = 3, user_id = NULL, order_id = NULL WHERE id = %d", // Gebruik de kolomnaam 'id'
    $target_license_id
    );

    // Voer de query uit
    $wpdb->query($sql);

    // Log de directe SQL-update
    error_log("Direct SQL update executed for id: $target_license_id");
    }
    }

    // Als de licentie succesvol is bijgewerkt, log het resultaat
    if ($license_updated) {
    error_log("Licentie met id $target_license_id is opnieuw beschikbaar voor verkoop.");
    }

    }

Viewing 1 replies (of 1 total)
  • Plugin Support mobi5533

    (@mobi5533)

    Hi?@joukeschotanus

    Thank You For Contacting WpExperts Support Team!

    Our technical team would need more details on the issue you are facing. Kindly create a ticket on our official website with the same summary so we can assist you efficiently.

    Thank You

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.