Get qty of products in an order - php

i would like to know how to get the unique quantity of products in a order
i have tried these (in /renderer/default.phtml):
$_order = $this->getOrder();
$qnt = round($_order->getData('total_qty_ordered'), 0);
but this returns the total number of products, and i need only total of different products.
thanks!

The problem is that an order may contain many products. Hence qty of a product is not assigned at order level but is is assigned at item level. You can get that as below:
$_order = $this->getOrder();
foreach ($_order->getAllItems() as $items){
$qty = $items->getQty();
}

You can get the number of different products by loading the visible items of an order (since Magento stores two order items for configurable products, one for the parent and one for the child, this method provides only one item in such cases) and count the number of array entries.
For example in this way:
$_order = $this->getOrder();
$qnt = count($_order->getAllVisibleItems());

Related

How can I change order values in a table when inserting a new row with a higher order value?

Using php and mysqli, I have a categories table that group products and control the order they are displayed in my catalog. The field "ord" is an int with the value that controls the order these product categories are displayed. When inserting a new category, I would like to reorder the remaining categories from the new category down and update the table then insert the new category without duplicating the "ord" value.
There are 11 categories. I want to insert my new category with it's "ord" value as 5. Clearly I need to add 1 to each "ord" values 6 - 11 to reorder my catalog. Not sure how to do this. I'm not good with arrays but I would think some sort of foreach array[$key][] + 1 with an UPDATE to the table would work. I'm not clear on how to create the array.
Don't really have any working ideas on this...
Category ord=1: Site Licensing
Category ord=2: PICC Insertion Training
Category ord=3: Ultrasound Training
New Category: ord 2: Membership Subscriptions
Category ord=1: Site Licensing
Category ord=2: Membership Subscriptions
Category ord=3: PICC Insertion Training
Category ord=4: Ultrasound Training
You can simply update the table before doing the INSERT. By updating the existing data first, you leave a space for your new entry.
$order = $_POST['order'];
$stmt = $mysqli->prepare("UPDATE `categories` set order = order + 1 where order >= ?"));
$stmt->bind_param("i", $order);
$stmt->execute();
/** Now perform INSERT statement */

Get total value of one column from a collection

I have an orders table which holds orders.
Each orders can have supplements.
The relation is accessed via
$order->supplements;
Theres a column on the supplements table called 'unit_price'.
How do I get the total price of all supplemented combined?
Try using the laravel sum query.
$total = $order->supplements->sum('unit_price');
Should give you the total price
Try using sum aggregates()
$totalPrice = $order->sum(function ($query) {
return $query->supplements->sum('unit_price');
});

Laravel avg review by product id

So I have 2 tables:
1. Products - id, title, description, price, category
2. Reviews with id, user_id, rating, product_id
I'm trying to develop a shop. So, in each product page I have a form where users can submit a review, giving 1-5 stars. On the index page, where all products are displayed with title, description, category, price, I want to display the average rating of each product. I already setup the relationships as follows:
Products -> hasMany(review)
Review -> belongsTo(user)
Review -> belongsTo(product)
User -> hasMany(review)
How do I interogate the database and display the average rating for each product on index? I found some solutions around here but I couldn't make it work.
P.S: Everyting that is inserted in the Review table is correct, I just need to display it for each product. Thank you in advance!
Without Eloquent, it will be something like this, can you make sure it outputs something?
$reviews = Product::find($id)->reviews;
$avg = 0;
foreach($reviews as $rev){
$avg += $rev->rating;
}
return round($avg/count($reviews));
With Eloquent, it will be like:
$product = Product::find($id);
$avg = $product->reviews()->avg('rating');
return $avg;

How to get the order item id in Magento 2 (not the order ID)?

I need the ORDER ITEM ID in order to consume the service RefundInvoice at Magento 2 to create a credit memo with a refund (http://devdocs.magento.com/guides/v2.1/mrg/ce/Sales/services.html).
Order item ID is I definitely something else than the order ID, since I tried that one.
So anyone an idea how I can retrieve the order item id?
//Try with below code.
$orderId = 1234; //put your order id.
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
$orderItems = $order->getAllItems();
foreach($orderItems as $item)
{
echo "Order Item ID :".$item->getItemId(); //
}
The item_id is actually the entity_id for sales_order_item. It is NULL until the record is saved to the database, upon when it is populated with the row ID. In order to access it and find its value, the salesOrderItem object must call ->save() first. If you are looking for the actual "item id" (the product), use product_id. If you need to record the record ID, then call $object->save(), and then you can read $object->getItemId()

Display the same item in several categories

I use the following code to get information from my MySql data base in order to display items :
$sql = "SELECT product,size1,size2,prdname,price
FROM multisize
WHERE online= 'online' AND category = 'backcovers' AND campaign = '".$brand."'
ORDER BY size1 DESC, size2 DESC ";
Some times I have the same item that needs to be displayed in two campaigns. (ex. iphone4, iphone5)
is there any way to tell my code that this item should be displayed in those two campaigns or should i just create duplicate items ?
You may use GROUP BY clause in your query.

Categories