I'm developing an order system in Laravel and I encountered a problem when trying to process the products of the order.
The page looks like this:
Each Product entry represents an item in a table called Products that has id,name and price as columns.
When I'm sending the form , each QTY input has as name the product's id(for Cabbage is 1 , for Water is 3, for Bread is 4 ).
Something like this(for cabbage):
<input style="width:70px;margin-bottom:9px;" class="qty form-control input-lg" required="required" name="1" type="number" value="">
One of the problems is that those aren't the single inputs on the page and I can't really identify them from the rest.
When the form is sent I would want to take those values , search in the db , get the name of each product based on id and make a simple list like:
Cabbage - 2 qty
Water - 1 qty
Bread - 3 qty
What I'm asking is what would be the best way to get the list above as the products will be dynamic(removed , added , modified , etc).
Initially I thought of something like:
//all inputs with name products[] and as value the product id
$items = Input::get('products');
foreach($items as $item){
$prodid = Product::find($item);
echo $prodid['name'];
}
But doing so I wouldn't know the qty value , I'm pretty much stuck.
I appreciate any ideas that would help me achieve what I explained.
Thank you!
You could sent the quantities also as an array, so name you qte inputs like quantities[], then get them by index in the controller :
$product_ids = Input::get('products'); //input name products[]
$quantities = Input::get('quantities'); //input name quantities[]
foreach($product_ids as $index=>$product_id){
$product = Product::find($product_id);
$quantity = $quantities[$index];
echo $product['name'].' : '.$quantity;
}
Related
I'm trying to get two arrays "Product IDs and Product Quantity" from URL parameters written like this: id[]=p1&qty[]=12&id[]=p2&qty[]=12.
My code is:
<?php
$id = $_GET["id"];
$qty = $_GET["qty"];
foreach ($id as $product) {
echo "<br>Product ID is: $product and Quantity is: $qty";
};
?>
The purpose of the code is to assign quantity to each ID in a single line of code.
I will appreciate your guidance for the exact example in my question as I've seen similar questions but with other examples and purposes.
Thank you very much in advance!
Trying to assign product quantity to each product ID.
Good day
I'd be grateful for some expertise with this issue of mine.
Assume we have the following variables in a hypothetical situation:
$uom = (either "PLT" or "CRT")
$prow = item ID in cart (starts from 1 and goes up depending on how many items)
$quantity = quantity of each item
$Name = item name
$quantityplt = new variable for PLT quantity
$quantitycrt = new variable for CRT quantity
This information is acquired for each product from a cart shopping system on the website. So once the user adds an item to the cart, that information is displayed for each item in the cart that they chose.
I have two text inputs which need to display the total_quantity for all the items: depending on whether or not the $uom is "CRT" or "PLT".
<input type="text" id="plt_sum" readonly/>
<input type="text" id="crt_sum" readonly/>
Now, I need to calculate the $quantity depending on $uom.
It must then display the value in either "#plt_sum" or "#crt_sum"...
I'm not confident in using loops so I don't really know how to:
<?php
foreach($prow){
if($uom=="PLT"){
$quantityplt value == (perform $quantity addition WHERE $uom is "PLT");
echo $quantityplt (in the #plt_sum text input);
}
elseif($uom=="CRT"){
$quantitycrt value == (perform $quantity addition WHERE $uom is "CRT");
echo $quantitycrt (in the #crt_sum text input);
}
}
?>
I do hope this piece of php makes any sense at all. I realise my syntax and everything else is most likely wrong. I'm still very new to it and learning things every day.
Thank you for reading and I appreciate any help.
I am developing a module to receive commissions from companies that are selling in my Magento store. It is already working, but need to add a new feature that I'm not getting.
I need to add an extra value in the commission, if the application of any company is greater than "X"
I did in a way that did not work:
foreach ($order->getAllVisibleItems() as $key => $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$price = $item->getPrice();
$valor_comissao = (int)$product->getAttributeText('comissao_vendedor')/100;
$comissao_valor2= $price-($price*$valor_comissao);
$qty = $item->getQtyOrdered();
$valor_pedido= ($price*$qty);
if($valor_pedido > 150) {
$comissao_valor= ($comissao_valor2*$qty-17);
} else {
$comissao_valor=($comissao_valor2*$qty-2);
}
$comissoes[] = array (
'Razao' => 'Produto '.$item->getName().' id '.$item->getProductId().' x '.$item->getQtyToInvoice().' sku: '. $item->getSku(),
'Login' => $product->getAttributeText('login'),
'ValorFixo' => $comissao_valor
);
}
then only if the product go from $ 150.00 the value is added and if you have two selections from costing $ 100.00, the value is not added. I need the value to be added if the entire order of the brand exceeds $ 150.00.
The logic that I'm trying to follow is as follows:
Taking the total value of the order
Filter by tags, ie total value of the order by brand
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I wonder if someone has gone through something similar and could help me.
Grateful.
Taking the total value of the order
You can retrieve the total of the order via:
$order->getGrandTotal()
Filter by tags, ie total value of the order by brand
I would recommend using Magento's resource model object and building a query on it.
This post shows how this can be done: https://stackoverflow.com/a/5139391/3784145
note
Mage::getModel('catalog/product')->getCollection()
can be used instead of
Mage::getResourceModel('sales/order_collection')
Explanation on the difference between Model and Resource:
https://magento.stackexchange.com/a/4579
If the product is a brand that exceeded "x" and she has not yet received the additional value, add shipping
I don't really know what you mean with: If the product is a brand that exceeded "x"
But I think this would require another collection query.
I have a url like this
http://example.com/index.php?&item_name_1=productnamex&quantity_1=1&amount_1=0&item_number_1=2&on0_2=thumb&option_index_2=1&item_name_2=productnamex&quantity_2=1&amount_2=0&item_number_2=2&on0_2=thumb&option_index_2=1&item_name_3=productnamexx&quantity_3=1&amount_3=0&item_number_3=3&on0_3=thumb&option_index_3=1.....
that sends to a page the values of a shopping card. I want to GET the values item_name_x, quantity_x, amount_x where x the number of each product, group and print them like
Product 1 name - Product 1 Quantity - Product 1 Amount
Product 2 name - Product 2 Quantity - Product 2 Amount
Product 3 name - Product 3 Quantity - Product 3 Amount......
I can't make any change to the page who produce the url.
Can anyone help me?
Thanks in advance for any help you are able to provide.
That's a really bad design, poor you have to deal with it. However, if you're sure that the query string will always be consistent (i.e.: each group will always be complete: name-quantity-amount) you can try this:
$i = 1;
while (isset($_GET['item_name_'.$i])) {
$name = $_GET['item_name_'.$i];
$qty = $_GET['quantity_'.$i];
$amt = $_GET['amount_'.$i];
... // do whatever you want with those 3
$i++;
}
what about this
<?php
$count=count($_GET);
for($i=1;$i=$count;$i++)
{?>
Product <?php $i;?> name - Product <?php echo $i?> Quantity - Product <?php echo $i?> Amount
<?php }?>
I am working on an eCommerce website.I have a product page where i have single form to add multiple product quantities at the same time using the button .
On my products page,within my single form i am sending individual product values to next page like these using the loop:
<?
$query_data=$this->db->get('products');
foreach($query_data->result() as $product)
{
?>
<input name="productname[]" value="<?=$product->Name?>" type="hidden" />
<input type = "hidden" id = "product_id" name = "product_id[]" value = "<?=$product->Code?>" />
<input name="num_pallets[]" id="num_pallets" value="<?=$pallet?>" type="hidden" />
<input type = "hidden" id = "num_packs" name = "num_packs[]" value = "<?=$packet?>" />
<input type="text" name="quantity[]" id="quantity" size="2" />
<input type = "hidden" id = "product_price" name = "product_price" value = "<?=$number?>" />
<?
}
?>
Where :
1) the "quantity" field is the quantity of the product a customer will order.
2) the "num_pallets" field is the pallet quantity of the product a product has in the database.
3) the "num_packs" field is the pack quantity of the product a product has in the database.
Now,i want to retrieve the corresponding values on shopping cart page so that i could perform calculations accordingly.
The other problem i have is that when i submit my form,it submits all the values INCLUDING THE products' values of those I dont enter quantity in the text boxes.
MY Question:
How can i loop through the products (I enter the "quantity" in text boxes) on my page so that it could give me the corresponding values of the ordered products ONLY i.e if i fill in the quantity text box of product(11760) then i want to process values of 11760 ,not other products.
How can i retrieve the corresponding values of 1 or more ordered product only ??Thanks
If I understand correctly, you want to check whether a quantity was filled in for any number of products, if so, process accordingly, right?
// controller method that receives posted form data
$quantity = $this->input->post('quantity');
foreach($this->input->post('product_id') as $key => $id){
if($quantity[$key]){
//there's a quantity for this product
}
}
Here is the solution.I have worked it out ,tested and found that in order to keep corresponding indexes together with the individual products,I will need to pass the product code in the form of multidimensional array like this:
<?
if (isset($_POST['quantity']) && is_array($_POST['quantity']))
{
$field_array = array();
foreach($_POST['quantity'] AS $itemID => $value)
{
if ($_POST['quantity'][$itemID]>0)
{
if (isset($_POST['product_id'][$itemID])&& (isset($_POST['product_price'][$itemID])) && (isset($_POST['counter'][$itemID]))&& (isset( $_POST['quantity'][$itemID])))
{
$productid=$_POST['product_id'][$itemID];
$this->load->model('m_shopping_cart');
$pricehead=($_POST['quantity'][$itemID])*($_POST['product_price'][$itemID]);
$this->m_shopping_cart->add_to_cart($_POST['product_id'][$itemID],$_POST['product_price'][$itemID],$_POST['quantity'][$itemID],$_SESSION['cart_id'],$pricehead,false,false,false);
}
}
}
}
?>