Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.4 kB
3
Indexable
Never

add_action('b2bking_before_send_quote_cart', function() {
    // Check if WooCommerce is active
    if (function_exists('WC')) {
        // Get the current cart instance
        $cart = WC()->cart;
        // Ensure the cart isn't empty
        if (!$cart->is_empty()) {
            // Create a new order
            $order = wc_create_order();

            // Assuming the current user is logged in, retrieve user ID
            $user_id = get_current_user_id();

            // Loop through cart items and add them to the order
            foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
                $item_id = $order->add_product(
                    wc_get_product($cart_item['product_id']), 
                    $cart_item['quantity'], 
                    array(
                        'subtotal' => 0, // Set item subtotal (before discounts) to 0
                        'total' => 0, // Set item total (after discounts) to 0
                    )
                );

                // If you have product variations
                if (!empty($cart_item['variation_id'])) {
                    $order->update_product($item_id, wc_get_product($cart_item['variation_id']));
                }
            }

            // Load the user's billing and shipping details
            $billing_address = array(
                'first_name' => get_user_meta($user_id, 'billing_first_name', true),
                'last_name'  => get_user_meta($user_id, 'billing_last_name', true),
                'company'    => get_user_meta($user_id, 'billing_company', true),
                'email'      => get_user_meta($user_id, 'billing_email', true),
                'phone'      => get_user_meta($user_id, 'billing_phone', true),
                'address_1'  => get_user_meta($user_id, 'billing_address_1', true),
                'address_2'  => get_user_meta($user_id, 'billing_address_2', true),
                'city'       => get_user_meta($user_id, 'billing_city', true),
                'state'      => get_user_meta($user_id, 'billing_state', true),
                'postcode'   => get_user_meta($user_id, 'billing_postcode', true),
                'country'    => get_user_meta($user_id, 'billing_country', true),
            );

            $shipping_address = array(
                'first_name' => get_user_meta($user_id, 'shipping_first_name', true),
                'last_name'  => get_user_meta($user_id, 'shipping_last_name', true),
                'company'    => get_user_meta($user_id, 'shipping_company', true),
                'address_1'  => get_user_meta($user_id, 'shipping_address_1', true),
                'address_2'  => get_user_meta($user_id, 'shipping_address_2', true),
                'city'       => get_user_meta($user_id, 'shipping_city', true),
                'state'      => get_user_meta($user_id, 'shipping_state', true),
                'postcode'   => get_user_meta($user_id, 'shipping_postcode', true),
                'country'    => get_user_meta($user_id, 'shipping_country', true),
            );

            // Set order billing and shipping details
            $order->set_address($billing_address, 'billing');
            $order->set_address($shipping_address, 'shipping');

            $order->calculate_totals();
            $order->update_status('wc-pending', 'Order created programmatically by B2BKing before sending a quote cart', TRUE);

        }
    }
});
Leave a Comment