Zen Cart Logo
Forums / All Other Contributions/Addons / Adding Quote Feature in Zen Cart

Adding Quote Feature in Zen Cart

Views: 980

Results 1 to 3 of 3
4 Apr 2025, 1:03 PM
#1
gozzandes avatar

gozzandes

Zen Follower

Join Date:
Jul 2021
Location:
Fukuoka Japan
Posts:
131
Plugin Contributions:
0

Adding Quote Feature in Zen Cart

[Note: remember to include site URL, ZC version, list of plugins, PHP version, etc ... read the Posting Tips shown above for information to include in your post here.
And, remove this comment before actually posting!]

I am trying to add a feature where customers can add products not registered in the product database to a quote. I created a quote table with the following columns: id, products_model, quantity, price, subtotal, status, created_at, customers_id, product_name, remark, and quote_number.

When accessing the URL like https://domain.jp/index.php?main_page=quotes&products_model=XZXX&quantity=1, the product model XZXX with quantity 1 gets added to the quote list, which shows the product number, quantity, unit price, subtotal, status, and a checkbox for purchasing.

I have written the code to add the selected item to the cart, but after checking the checkbox and clicking the purchase button, it gets registered in the customers_basket table but does not show up in the shopping cart immediately. After logging out and logging back in, the cart is updated with the product. Can anyone explain why the product does not appear in the shopping cart right after purchase?

// Update session after purchase process 
if (isset($_POST['submit_purchase'])) {
    foreach ($_POST['purchase'] as $id => $purchase) {
        if ($purchase == 'on') {
            // Retrieve product information from quotes table for selected items
            $product_query = $db->Execute("SELECT products_model, quantity FROM quotes WHERE id = " . (int)$id);
            $products_model = $product_query->fields['products_model'];
            $quantity = $product_query->fields['quantity'];
            
            // Get corresponding products_id using products_model
            $product_id_query = $db->Execute("SELECT products_id FROM products WHERE products_model = '" . zen_db_input($products_model) . "'");

            if ($product_id_query->RecordCount() > 0) {
                $products_id = $product_id_query->fields['products_id'];

                // Before adding to the cart, check if the product already exists in the cart
                $basket_check_query = $db->Execute("SELECT products_id FROM customers_basket WHERE customers_id = $customers_id AND products_id = $products_id");

                if ($basket_check_query->RecordCount() > 0) {
                   $cart->add_cart($products_id, $quantity, [], true);
                    // If the product already exists in the cart, update the quantity
                    $db->Execute("UPDATE customers_basket 
                                  SET customers_basket_quantity = customers_basket_quantity + $quantity 
                                  WHERE customers_id = $customers_id AND products_id = $products_id");
                } else {
                    // If the product does not exist in the cart, add it
                    $date_added = date('Ymd');                 
                    $db->Execute("INSERT INTO customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added)
                                  VALUES ($customers_id, '" . zen_db_input($products_id) . "', $quantity, '" . zen_db_input($date_added) . "')");
                }
            } else {
                // Error handling if product is not found (optional)
                echo "The specified product was not found.";
            }
        }
    }
    // Redirect to update changes
    zen_redirect(zen_href_link(FILENAME_SHOPPING_CART, '', 'SSL'));
}
4 Apr 2025, 3:01 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,080
Plugin Contributions:
56

Re: Adding Quote Feature in Zen Cart

Erm, possibly because while you've added the product to the customer's basket you've not added it to their cart?

5 Apr 2025, 12:19 AM
#3
gozzandes avatar

gozzandes

Zen Follower

Join Date:
Jul 2021
Location:
Fukuoka Japan
Posts:
131
Plugin Contributions:
0

Re: Adding Quote Feature in Zen Cart

lat9:

Erm, possibly because while you've added the product to the customer's basket you've not added it to their cart?

Thank you. I thought it was enough to insert into the customers_basket table, but it turns out that $_SESSION['cart'] was also necessary

  // Use products_model to get the corresponding products_id
$product_id_query = $db->Execute("SELECT products_id FROM products WHERE products_model = '" . zen_db_input($products_model) . "'");

if ($product_id_query->RecordCount() > 0) {
    $products_id = $product_id_query->fields['products_id'];

    // Check if the product is already in the cart before adding to customers_basket
    $basket_check_query = $db->Execute("SELECT products_id FROM customers_basket WHERE customers_id = $customers_id AND products_id = $products_id");

    if ($basket_check_query->RecordCount() > 0) {
       $cart->add_cart($products_id, $quantity, [], true);
        // If the product is already in the cart, update the quantity
        $db->Execute("UPDATE customers_basket 
                      SET customers_basket_quantity = customers_basket_quantity + $quantity 
                      WHERE customers_id = $customers_id AND products_id = $products_id");
    } else {
        // If the product is not in the cart, add a new product
        $date_added = date('Ymd');                 
        $db->Execute("INSERT INTO customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added)
                      VALUES ($customers_id, '" . zen_db_input($products_id) . "', $quantity, '" . zen_db_input($date_added) . "')");
    }
} else {
    // Error handling if the product is not found (optional)
    echo "The specified product was not found.";
}
// Redirect to reflect the changes
zen_redirect(zen_href_link(FILENAME_SHOPPING_CART, '', 'SSL'));