• Hello,

    I have developed a WordPress plugin which adds WooCommerce products programmatically using this code:

    WC()->cart->empty_cart();

    foreach ($productos as $producto) {
    WC()->cart->add_to_cart($producto->post_id, $cantidad);
    }

    After I add products to cart I need to save the cart id, because after the user proceeded to checkout and did the payment, I need to save the order id in a custom database table. I need cart id because when the order is processed, I need to match it to a record in that custom database table.

    For example, if my custom database table, called wp_mycustom, has these records:

    ID Name cart_id order_id
    1 One Record —- —–
    2 Other Record 12345 <—- This is the record that is associated with certain purchase

    So, when the order is completed, I need to update the record whose ID is 2 with the corresponding order ID generated when completed.

    How can I accomplish that?

    Thanks
    Jaime

    • This topic was modified 2 years, 2 months ago by jstuardo.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Normalyl cats on WC don’t have an ID. Looking through the docs for the WC_Cart class, the closest thing that I can see is get_cart_hash() which will return a hash of the carts contents. That uses the cart session in the hash, so it might do what you want.

    Thread Starter jstuardo

    (@jstuardo)

    Hello… but if cart is stored per user session, I don’t think I can use it to be able to match the record when the order is complete (sometimes order is complete only when administrator changes status to complete, for example, when customer paid using bank transfer after he has abandoned browser session).

    The actual problem is this. My plugin allows the customer to do some choices before proceeding with the purchase. All those choices are stored in a custom database table I created together with the default WordPress tables.

    When the payment was made, I need to save the order id in that custom table in the same record of those choices, so that, in a later time, I will be able to know which choices were set for determined purchase.

    Thinking in other way to accomplish this. The table record of choices has en ID (the primary key). Is it possible to save that ID together with the order? If that is possible, that way I can do the match between the order and the choices.

    Thanks
    Jaime

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can I have cart id after calling WC()->cart->add_to_cart( ) method?’ is closed to new replies.