How to GET multiple values in php? - php

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 }?>

Related

Multiple inputs with names that represent a product id

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;
}

Calculate quantity based on different text values

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.

Get price of an order, by manufacturer or brand on MAGENTO

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.

How to get the grouped product price with a quantity of 1 shild product in magento

I have a groupe product which is made by 2 shild product (1 real product + 1 "virtual" product which represent the cost of the reparation ).
Therefore, the customer has 2 possibilities :
he can buy the real product to fix the problem by itself
or
he can buy the bundle and he will buy the reparation set and automatically the real product, and it's my factory who will repare his problem
For example, the product cost 30€, and the reparation set cost 20€.
On the front end, it will be set :
"Starting at: 20€"
However, I supposed that the customer cannot buy this bundle with only one product. Therefore the price is 50€, and I would like that on the frontend the price is set to 50€.
I try to modify on my theme the file which is located on :
.../Catalog/Block/Product/View/price.phtml
I would like insert the following variable $simulationPrice :
<span class="price" id="product-minimal-price-<?php echo $_id ?>
<?php echo $this->getIdSuffix() ?>">
<?php "there I would like the price 50€" echo $simulationPrice ?>
</span>
To do that, I would like to perform the operation :
<?php $_associatedProducts = $this->getAssociatedProducts(); ?>
<?php foreach ($_associatedProducts as $childProduct) { ?>
<?php $simulationPrice += $childProduct->getPrice() ?>
<?php } ?>
How can I do that ? I would like that the price = 1 real product * 1 quantity + 1 virtual product * 1 quantity.
If you have another method, it will be a pleasure to read your proposition.
I precise that I already modify the bundle and the customer can not modify the quantity which is set to 1 unit (1 real product + 1 virtual product), however he can modify the quantity in the basket...
If you have the solution about this code which doesn't work, it will be a pleasure.
Thank you for your help.
I got the solution with this code :
$simulationPrice=0;
$associatedProducts = $_product->getTypeInstance(true)->getAssociatedProducts($_product);
foreach ($associatedProducts as $childProduct) {
$quantityBackGround=$childProduct->getQty();
$simulationPrice += $childProduct->getPrice() * $childProduct->getQty();
}
In fact the big problem was that I past my code into the following condition :
<?php if (!$_product->isGrouped()): ?>
I didn't see the "!" in the condition....therefore it couldn't never work...

Php Array Count no work me

I have in txt file this data for products of shop :
product 1
product 1
product 2
product 2
product 2
product 1
product 1
product 3
product 3
product 3
product 1
product 1
product 3
product 1
Ok , the people add products to the card of shopping and these products insert in general txt
For recover the data i use the next script :
<?php
$file_data=file("products.txt");
for ($i=0;$i<sizeof($file_data);$i++)
{
/// Create array of groups
$global_products[]=$file_data[$i];
}
/// Now i want get the products and the number in each case for example of the product 1 i have 6 for the product 2 i have 3 and for the product 3 i have 4 , ok for get this i can use thi function
$prr=array_count_values($global_productos);
print_r (object2array($prr),true);
?>
If i use print_r i can get the products and number in each case but how i can get in other mode , for example for use with normal function of print or echo , i try get finally the data show as this :
Product 1 (6)
Product 2 (3)
Product 3 (4)
Thank´s Regards
use the code below
<?php
$file_data=file("output_rrr.txt");
for ($i=0;$i<sizeof($file_data);$i++)
{
/// Create array of groups
$global_products[]=$file_data[$i];
}
/// Now i want get the products and the number in each case for example of the product 1 i have 6 for the product 2 i have 3 and for the product 3 i have 4 , ok for get this i can use thi function
$prr=array_count_values($global_products);
foreach($prr as $key=>$value)
{
echo trim($key).": ".$value."<br/>";
}
?>

Categories