• Resolved iqqsam182

    (@iqqsam182)


    Hi, I’m creating a custom checkout form and have come up with the following code, however this does not properly manage stock if two try and checkout the stock drops below 0 and becomes minus, how can I check if item is in stock and return a response that its out of stock? especially when two try and checkout simultaneously. Thanks, if there’s anything else that needs to be improved please tell me what should be done. (address filtering maybe?)
    function checkout{
    $address = $_POST[‘address’];
    if (!isset($address)) {
    return;
    }
    global $woocommerce;
    $address = array(
    address fields here
    );
    $coupon = WC()->cart->get_applied_coupons();
    $order = wc_create_order();
    $cart = WC()->cart;
    $order->set_currency( get_woocommerce_currency());
    $chosen_shipping_methods = WC()->session->get( ‘chosen_shipping_methods’ );
    $cart_hash = md5( json_encode( wc_clean( $cart->get_cart_for_session() ) ) . $cart->total );
    $order->set_cart_hash( $cart_hash );
    $order->set_total( $cart->total );
    $order->set_shipping_total( $cart->shipping_total );
    $order->set_discount_total( $cart->get_cart_discount_total() );
    WC()->checkout->create_order_line_items( $order, $cart );
    WC()->checkout->create_order_shipping_lines( $order, $chosen_shipping_methods );
    WC()->checkout->create_order_coupon_lines( $order, $cart );
    $cart->empty_cart();
    $order->set_address($address, ‘billing’);
    $order->calculate_totals();
    $order->save();
    $order->payment_complete();
    return;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Managing stock progamatically’ is closed to new replies.