Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon.nova-pro-v1:0· AmazonHugging Face LMArena Human Preference 140K

function clearcartandaddproductredirectwithcheckout() { global $wpdb; // if user will…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-mini-2025-04-14’s answer.
  1. User

    function clear_cart_and_add_product_redirect_with_checkout() { global wpdb;//ifuserwillbuythecoursefromdetailspage(Live,Consultancy)thanremovefromWishlistandaddtocartif((isset(wpdb; // if user will buy the course from details page (Live, Consultancy) than remove from Wishlist and add to cart if ((isset(_GET['add_product']) && GET[′addproduct′]!==′′)∣∣(isset(_GET['add_product'] !== '') || (isset(_GET['add-to-cart']) && $_GET['add-to-cart'] !== '')) {

        $product_id = isset($_GET['add_product']) ? intval($_GET['add_product']) : $_GET['add-to-cart'];
        $courseType = get_post_meta($product_id, 'os_course_type', true);
        $user_id = get_current_user_id();
        if ($user_id !== '' && get_post_type($product_id) == 'lp_course' && ($courseType === 'live' || $courseType === 'consultancy')) {
            $user_wishlist = get_user_meta($user_id, '_lpr_wish_list', true);
            $wishlists = $user_wishlist ? $user_wishlist : array();
    
            if (!empty($wishlists) && in_array($product_id, $wishlists)) {
                $pos = array_search($product_id, $wishlists);
                unset($wishlists[$pos]);
            }
    
            if (!empty($wishlists) && count($wishlists) > 0) {
                update_user_meta($user_id, '_lpr_wish_list', $wishlists);
            } else {
                delete_user_meta($user_id, '_lpr_wish_list');
            }
        }
    }
    
    if (isset($_GET['clear_cart']) && $_GET['clear_cart'] == '1' && isset($_GET['add_product'])) {
        //WC()->cart->empty_cart();
        $product_id = intval($_GET['add_product']);
        // WC()->cart->add_to_cart($product_id);
        if (get_post_type($product_id) == 'lp_course') {
            $wc_product_id = get_post_meta($product_id, '_associated_product_id', true);
            if (isset($_GET['batch'])) {
                $batch_type = get_batch_type_by_id($_GET['batch']);
                $cart_item_data = array(
                    'batch_id' => $_GET['batch']
                );
    
                if (!$batch_type) {
                    $wc_product_id = $product_id;
                }
            }
    
            WC()->cart->add_to_cart($wc_product_id, 1, 0, array(), $cart_item_data);
        }
        if (isset($_GET['buynow']) && $_GET['buynow'] == 1) {
            $redirect_url = wc_get_checkout_url();
            wp_safe_redirect(esc_url_raw($redirect_url));
        } else {
            $batch_id = isset($_GET['batch']) ? sanitize_text_field($_GET['batch']) : '';
            $redirect_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
    
            if (!empty($batch_id)) {
                $redirect_url .= '&batch=' . $batch_id;
            }
            wp_safe_redirect(esc_url_raw($redirect_url));
        }
        exit;
    } else if (isset($_GET['add-to-cart']) && $_GET['add-to-cart'] !== '') {
        //WC()->cart->empty_cart();
        $product_id = intval($_GET['add_product']);
    
        //WC()->cart->add_to_cart($product_id, 1, 0, array());
        $redirect_url = wc_get_checkout_url();
        wp_safe_redirect(esc_url_raw($redirect_url));
        exit;
    } else {
        if (!isset($_GET['_locale'])) {
            $session_handler = WC()->session;
            // Ensure session is initialized
            if (isset($session_handler)) {
                $session_key = $session_handler->get_customer_id();
    
                $wpdb->delete(
                    $wpdb->prefix . 'woocommerce_sessions',
                    ['session_key' => $session_key],
                    ['%s']
                );
            }
        }
    }
    

    }

    in this code when order purchase its store in woocommerce order and lernapress order but in live course batch type purcchae its store only in woocommerce so i want when woocomerce order add after its store in learnpress order create this with hook

  2. Amazon Nova Assistant

    To ensure that when a WooCommerce order is created, it also creates a corresponding LearnPress order, you can use the woocommerce_new_order hook. This hook allows you to perform actions after a new WooCommerce order is created. Here's how you can modify your function to achieve this:

    Hook into the woocommerce_new_order action. Check if the order contains a LearnPress course. Create a corresponding LearnPress order.

    Here's the updated code:

    function clear_cart_and_add_product_redirect_with_checkout()
    {
        global $wpdb;
        // if user will buy the course from details page (Live, Consultancy) than remove from Wishlist and add to cart
        if ((isset($_GET['add_product']) && $_GET['add_product'] !== '') || (isset($_GET['add-to-cart']) && $_GET['add-to-cart'] !== '')) {
    
            $product_id = isset($_GET['add_product']) ? intval($_GET['add_product']) : $_GET['add-to-cart'];
            $courseType = get_post_meta($product_id, 'os_course_type', true);
            $user_id = get_current_user_id();
            if ($user_id !== '' && get_post_type($product_id) == 'lp_course' && ($courseType === 'live' || $courseType === 'consultancy')) {
                $user_wishlist = get_user_meta($user_id, '_lpr_wish_list', true);
                $wishlists = $user_wishlist ? $user_wishlist : array();
    
                if (!empty($wishlists) && in_array($product_id, $wishlists)) {
                    $pos = array_search($product_id, $wishlists);
                    unset($wishlists[$pos]);
                }
    
                if (!empty($wishlists) && count($wishlists) > 0) {
                    update_user_meta($user_id, '_lpr_wish_list', $wishlists);
                } else {
                    delete_user_meta($user_id, '_lpr_wish_list');
                }
            }
        }
    
        if (isset($_GET['clear_cart']) && $_GET['clear_cart'] == '1' && isset($_GET['add_product'])) {
            //WC()->cart->empty_cart();
            $product_id = intval($_GET['add_product']);
            // WC()->cart->add_to_cart($product_id);
            if (get_post_type($product_id) == 'lp_course') {
                $wc_product_id = get_post_meta($product_id, '_associated_product_id', true);
                if (isset($_GET['batch'])) {
                    $batch_type = get_batch_type_by_id($_GET['batch']);
                    $cart_item_data = array(
                        'batch_id' => $_GET['batch']
                    );
    
                    if (!$batch_type) {
                        $wc_product_id = $product_id;
                    }
                }
    
                WC()->cart->add_to_cart($wc_product_id, 1, 0, array(), $cart_item_data);
            }
            if (isset($_GET['buynow']) && $_GET['buynow'] == 1) {
                $redirect_url = wc_get_checkout_url();
                wp_safe_redirect(esc_url_raw($redirect_url));
            } else {
                $batch_id = isset($_GET['batch']) ? sanitize_text_field($_GET['batch']) : '';
                $redirect_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
    
                if (!empty($batch_id)) {
                    $redirect_url .= '&batch=' . $batch_id;
                }
                wp_safe_redirect(esc_url_raw($redirect_url));
            }
            exit;
        } else if (isset($_GET['add-to-cart']) && $_GET['add-to-cart'] !== '') {
            //WC()->cart->empty_cart();
            $product_id = intval($_GET['add_product']);
    
            //WC()->cart->add_to_cart($product_id, 1, 0, array());
            $redirect_url = wc_get_checkout_url();
            wp_safe_redirect(esc_url_raw($redirect_url));
            exit;
        } else {
            if (!isset($_GET['_locale'])) {
                $session_handler = WC()->session;
                // Ensure session is initialized
                if (isset($session_handler)) {
                    $session_key = $session_handler->get_customer_id();
    
                    $wpdb->delete(
                        $wpdb->prefix . 'woocommerce_sessions',
                        ['session_key' => $session_key],
                        ['%s']
                    );
                }
            }
        }
    }
    
    add_action('woocommerce_new_order', 'create_learnpress_order_from_woocommerce_order', 10, 1);
    
    function create_learnpress_order_from_woocommerce_order($order_id) {
        $order = wc_get_order($order_id);
        $items = $order->get_items();
    
        foreach ($items as $item) {
            $product_id = $item->get_product_id();
            if (get_post_type($product_id) == 'lp_course') {
                $user_id = $order->get_user_id();
                $course_id = $product_id;
    
                // Create LearnPress order
                $learnpress_order_id = learn_press_add_order([
                    'user_id' => $user_id,
                    'order_items' => [
                        [
                            'item_id' => $course_id,
                            'item_type' => 'course',
                            'quantity' => 1,
                            'price' => $item->get_total(),
                        ],
                    ],
                ]);
    
                // Optionally, you can update the WooCommerce order with the LearnPress order ID
                update_post_meta($order_id, '_learnpress_order_id', $learnpress_order_id);
            }
        }
    }
    

    Explanation: clear_cart_and_add_product_redirect_with_checkout function: This function handles the cart clearing, adding products, and redirections. create_learnpress_order_from_woocommerce_order function: This function is hooked to woocommerce_new_order. It checks if the WooCommerce order contains a LearnPress course and creates a corresponding LearnPress order.

    This approach ensures that whenever a WooCommerce order is created, a corresponding LearnPress order is also created if the order contains a LearnPress course.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.