I'm creating an eCommerce website using HTML and PHP. I have a table named "products" in the database.
the minimum order values of the products are all saved in this table.
I have an admin panel to set the minimum order for that particular product. The quantity of the minimum order of each and every product was saved in the MySQL database. The minimum order value of that particular product is set by the admin according to the amount of availability of the product. There are no constraints for the admin to set the minimum order in the admin panel.
My question is how do I get the minimum order($row['moq']) quantity from the "product" table in the database so that the orders(cart_qty) from the users will be restricted to a certain amount that has been set from the Admin Panel using minimum attributes.
These are the codes that I used to require users to add the quantity that they wanted to order:
<div class="clearfix"></div>
<form action="store_items.php" method="post">
<div class="col-12 form-group">
<div class="row">
<div class="col-3">
<input type="number" name="cart_qty" step="0.01" value="1" class="form-control text-center" </div>
<div class="col-9">
<input class="btn btn-block addtocart" type="submit" value="Add to cart" name="addtocart" <?php if($row[ 'product_status']==0 ) echo 'disabled="disabled"'; ?> >
</div>
</div>
This is where I add the minimum order from the admin panel. moq is the entity of the minimum order in the product table.
<div class="col-md-2">
<div class="form-group">
<label>Minimum Order Quantity*</label>
<input type="number" step="0.01" class="form-control" placeholder="Enter minimum order quantity" name="moq" value="<?php echo $row['moq'];?>" required>
</div>
</div>
How do I get the $row['moq'] to restrict the number of orders in cart_qty. I know the method of using the minimum attributes but how do I get the values from of the moq in the product table to restrict the input value of cart_qty in the user page.
I'm not using any eCommerce platform (eg. WooCommerce) to build my website. Just a pure HTML, PHP, and JavaScript.
You can use the onChange event to restrict the value using Javascript.
<input type="number" onchange="restrictValue(this, 10, 30);">
Then in Javascript you do:
function restrictValue(inputElement, minimum, maximum)
{
let value = inputElement.value;
if (value < minimum) value = minimum;
if (value > maximum) value = maximum;
inputElement.value = value;
}
You could use the onKeyUp to constantly correct any value entered, but that could be quite disruptive and irritating.
Related
I am trying to make a e-commerce filter system with PHP and AJAX. I have watched some guy on YouTube and it worked, but my e-commerce website is in the clothing industry and his was with electronics... So he did not explain how to filter sizes which is stored with quantity (for example: S:2, M:3, L:4) with the filter systems. Now if I add a filter for a product, the product shows that size correctly. But if I try it the second time, it does not execute. I think because of the way my column is set up in MySQL. Please help.
Here is my code for the filter system:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="S" id="Maat_Voorraad">S
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="M" id="Maat_Voorraad">M
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="L" id="Maat_Voorraad">L
</label>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="XL" id="Maat_Voorraad">XL
</label>
</div>
</li>
</ul>
<script type="text/javascript">
$(document).ready(function(){
$(".product_check").click(function(){
$("#loader").show();
var action = 'data';
var type = get_filter_text('type');
var maat_voorraad = get_filter_text('Maat_Voorraad');
$.ajax({
url:'action.php',
method:'POST',
data:{action:action,type:type,maat_voorraad:maat_voorraad},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Gefilterde producten");
}
});
});
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
});
</script>
// action.php
if(isset($_POST['maat_voorraad'])){
$maat_voorraad = implode("','", $_POST['maat_voorraad']);
$sql .= "AND Maat_Voorraad LIKE '%$maat_voorraad%'";
}
// Maat_Voorraad refers to my table column in database. There the size and quantity is
stored per shirt like: S:2, M:4, L:3
// maat_voorraad means size_and_quantity and that variable sends only the size
See examplehere
I would really appreciate the help!
From the sample code you have shared, I can see you have an issue with your database design.
I would potentially have a table that stores shirt information like:
ID
Shirt Name
Shirt Price (Price of lowest SKU).
timestamps (When the item was added). You can add as many time stamps as are relevant to the product.
Another Table that stores shirt sizes:
id
name (e.g. "S")
Another Table (PIVOT Table) That holds information about a shirt, the sizes it has and associated price and availability:
shirt_id (Foreign Key references Shirts Table)
size_id (Foreign Key references Shirt Size Table)
unit_price (Price of this particular size of the said shirt)
discount_price (If there is a special offer on shirt)
A table to hold images of each shirt:
id
shirt_id (Foreign Key references Shirts Table)
url (Location of shirt image)
timestamps (When the image was added and when it was last updated).
Lastly, order table
id (order id)
shirt_id (Foreign Key references Shirts Table)
shirt_size_id (Foreign Key references Shirt Size Table)
quantity
total
date (Order created, Order fulfilled)
This is just a guide you can use to design your Database more efficiently. I omitted some columns as I figured you might already have them. If you want to implement multiple items within an order, just create a table for order items where you store individual items that have been ordered, then have a table that stores order total.
The link below will shed more light on what I've written above.
It's safe at this point to normalize your database to make querying easier. Refer to Normalization for more understanding.
how are you?
I would like to know how I can do to allow the user to edit(text) the quantity field in addition to using the + and - buttons (plus and minus). Briefly, how can i make the user enter the amount of items he wants to add to his cart. It is possible?
``` <div class="col-5 col-xs-5 col-md-2 border-bottom border-right" style=" text-align: center;"><br>
<button product_id="{{ $product->id }}" class="btn_minus rounded-circle btn btn-danger">-</button>
<span id="qtde_txt{{ $product->id }}">0</span>
<button product_id="{{ $product->id }}" class="btn_plus rounded-circle btn btn-success"><b>+</b></button>
</div>```
Many thanks!!
You can easily do this (for example) with JQuery. For the particular code that you have shown, you can use the following script to do this:
$(".btn_plus").on("click", function()
{
var span_element = $(this).prev(); //get the span element
var quantity = span_element.html(); //get the span value
quantity = parseInt(quantity) + 1; //increment it
span_element.html(quantity); //update the quantity in the span element
});
The same will go for the minus button, except that you will decrement your quantity. However, if you want the user to be able to edit the quantity just change the span into an input of type text:
<input type="text" id="qtde_txt{{ $product->id }}" value="0" />
But you will need to update the JQuery code I wrote above to map with the changes.
you need to use javascript for that, there are multiple examples for a counter / number input like that.
One example: Vanilla JS quantity increment and decrement buttons update only one input
I have a single.blade.php file that shows a congress details and has also a form so the user can select the quantity of tickets that he wants for each ticket type.
When user selects the quantities and click next the request goes to the RegistrationController that has the storeQuantity() method to stores the selected quantities
by the user in the array $selectedTypes and redirects the user to the registration.blade.php with the $selectedTypes array:
class RegistrationController extends Controller
{
public function storeQuantityandRedirect(Request $request, $id){
$typeQuantities = $request->get('types');
foreach($typeQuantities as $typeName => $quantity){
$type = TicketType::where('name', $typeName)->firstOrFail();
$price = $type->price;
$selectedTypes[$type->name]['quantity'] = $quantity;
$selectedTypes[$type->name]['price'] = $price;
}
return view('congresses.registration')->with('selectedTypes', $selectedTypes);
}
}
Now in the registration.blade.php file, the user needs to fill in the registration form.
If the user selected, for example, 2 tickets in the previous page in the registration page should appear two sections for the user introduce the
name and email for each ticket type. This is working fine with the code below:
#foreach($selectedRtypes as $k=>$selectedRtype)
<h6>Participant - 1 - Ticket Type - {{$k}}</h6> <!-- The variable {{$k}} shows the name of each ticket type (ex: full access, basic, etc). -->
<div class="form-group font-size-sm">
<label for="name" class="text-gray">Name</label>
<input type="text" class="form-control" id="name" value="">
</div>
<div class="form-group font-size-sm">
<label for="surname" class="text-gray">Surname</label>
<input type="text" class="form-control" name="surname" id="surname" value="">
</div>
#endforeach
Doubt:
Each ticket type can have custom questions.
My doubt is how to show besides the name and surname fields show also the custom fields of each specific ticket type.
For example, the congress organizer may have created for the ticket type "full access" the custom question "What is your email?". So in the foreach
when the ticket type is "full access" it should appear, besides the name and surname, the field "what is your email?".
Do you know how to do that?
I have this table content and relationships:
questions table:
id question congress_id
1 whats your email 1
ticket_type_questions table:
id ticket_type_id question_id
1 1 1 (the ticket type 1 has the question 1)
ticket types table
id name congress_id
1 full access 1
2 basic 1
Since you have the questions stored in a database table, you can pass them to your view from your controller. In your controller, you can add something like this:
$customQuestions = Question::where('congress_id', $cong_id);
Then modify your return to look something like this.
return response()->view('congress.registration', ['selectedTypes' => $selectedTypes, 'customQuestions' => $customQuestions], 200);
Then, on your view, right before the #endforeach, you can add a conditional to check if there are any custom questions.
// put this right before your #endforeach
#if(count($custQuestions) > 0)
// loop and display custom question fields.
#endif
Hope this helps.
I have a database named event-listing-db which contains a table wp_posts. Also i have a function for creating a form with method post with the following structure:
function ru_meta_callback() {
wp_nonce_field(basename(__FILE__), 'ru_jobs_nonce ');
?>
<form method="POST">
<div>
<div class="meta-row">
<div class="meta-th">
<label for="date_listed" class="ru-row-title">Event Date</label>
</div>
<div class="meta-td">
<input type="text" name="event_date" id="event_date" value=""/>
</div>
</div>
<div class="meta-row">
<div class="meta-th">
<label for="event_location" class="ru-row-title">Event Location</label>
</div>
<div class="meta-td">
<input type="text" name="event_location" id="event_location" value=""/>
</div>
</div>
<input type="submit" value="Add Event">
</div>
</form>
<?php
}
When I submit the form only the post_title column is filling. My question is: How can I create columns for event_date and event_location and fill'em when the data is submitted and save the new columns along with the original post?
You don't have to add a new column to the wp_posts table. But you can add a new meta for the post. If you want to add the field in the admin panel for the new field you want. You can use the add_meta_box to add a new meta box for post edit screen and then use the update_post_meta on the save_post hook. Hope that helps.
The meta fields can be used in the WP_Query in the meta_query index. But you can't use the custom field added to the wp_posts table to filter or search for the posts with the new meta added to the posts.
You have few options to have additional data to your post,
-> You can use hooks to add additional fields like here, WordPress - Add extra column to wp_posts, and post to it
-> You can make use of custom fields to add additional data to your post. And there is a good plugin too for this, https://wordpress.org/plugins/advanced-custom-fields/.
-> You can use the wp_postmeta table too to save your additional data for your post
I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
The output from the request on all three arrays is here: PasteBin
My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.
I don't think the HTML is very relevant, but here is a sample anyway:
<div class="well form-group ">
<div class="col-sm-2 col-md-2">
<label for="nails_staples">Nails & Staples:</label>
</div>
<div class="col-sm-4 col-md-4">
<select class="form-control product_id" name="product_code[10]">
<option selected="selected" value="">None</option>
<option value="8769">1 1/4 Coil Nails - box | $26.00</option>
<option value="6678">2" Hot Dipped Shake Nails | $135.00</option>
</select>
</div>
<div class="col-sm-1 col-md-1">
<label for="nails_req">Quantity:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control quantity" name="quantity[10]" type="text">
</div>
<div class="col-sm-1 col-md-1">
<label for="cost">Cost:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control cost" readonly="readonly" name="cost[]" type="text">
</div>
</div>
There are a few issues with this approach. Ideally you'd have separate calls to a controller for each entity you want to change, using AJAX to make each individual call as the user changes the entity. This solves the issue of attributes not being strongly tied to the entity ID (in your case, product_code).
If you're willing to make the assumption that all of the arrays match up, you should at least check that they are the same length.
Obviously I don't know all the specifics of your Model or exactly what you're looking for, but based on your paste bin, this should work.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
$count_ids = count($id);
if(count($qty) != $count_ids || count($cost) != $count_ids) throw new Exception("Bad Request Input Array lengths");
for($i = 0; $i < $count_ids; $i++){
if (empty($id[$i])) continue; // skip all the blank ones
$newProduct = new Product();
$newProduct->code = $id[$i];
$newProduct->qty = $qty[$i];
$newProduct->cost = $cost[$i];
$newProduct->save();
}