• Resolved pali47

    (@pali47)


    Hi,

    I would like to ask how I can change this part of the code so that it automatically adds the product to the end of the order after the order is created. Now this product is added to the beginning of my order.

    Thank you very much

    function add_item_to_existing_order($order_id) {
    $product_id = 14356;
    $quantity = 1;
    $order = wc_get_order($order_id);
    if (!$order) {
        //echo '<script>alert("No order")</script>';
        return;
    }
    
    $product = wc_get_product($product_id);
    if (!$product) {
        //echo '<script>alert("No product")</script>';
        return;
    }
    
    // Add the product to the order
    $order->add_product($product, $quantity);
    
    // Save the changes
    // $order->calculate_totals();
    $order->save();
    }
    
    add_action('woocommerce_new_order', 'add_item_to_existing_order');

Viewing 1 replies (of 1 total)
  • Plugin Support Feten L. a11n

    (@fetenlakhal)

    Hi there,

    Thanks for contacting us here at WooCommerce.com!

    It’s a development territory that may be beyond my area of expertise and assistance with code customization tasks such as this one is outside of the scope of support.

    Our friends at Codeable are able to help with that. Also, you can wait for an answer from another member;

    Thank you for your understanding

Viewing 1 replies (of 1 total)

The topic ‘Automatic add product when create order’ is closed to new replies.