get all users shopping cart - magento

I'm trying to migrate all user's carts to another system.
Is there a way to obtain every item in the shopping cart for every user?
What I need is only the product and customer IDs

I think when you say shopping cart items you mean order items right??
I'll do this with sql because you need to migrate the info, if you need as collection to use on magento let me know.
SELECT o.customer_id , p.name
FROM sales_flat_quote as o
LEFT JOIN sales_flat_quote_item as i ON i.quote_id = o.entity_id
LEFT JOIN catalog_product_flat_1 as p ON i.product_id = p.entity_id
$list = Mage::getModel('sales/quote')->getCollection();
foreach($list as $li){
$itens = $li->getItemsCollection(); //here we have the itens;
$li->getCustomer()->getName; // here we have the customer name.
foreach($itens as $i){
$i->getName();//itens name;
}
}

You should take a look at quote tables: sales_flat_quote, sales_flat_quote_item and sales_flat_quote_item_option

Related

how to get the count of customers and display them against product?

I am able to get the count of customers against the product but for me it is difficult to display like how i have mentioned in the above image.
You could use a left join and count function:
select p.product,
count(c.name) as cust_count
from product p
left join customer c
on p.product = c.product
group by p.product;
This is assuming you need zero count for the products that don't have any customers.
If that's not the case, you can use simple aggregation:
select product,
count(*) as cust_count
from customer
group by product;

Set Include in Navigation Menu to Yes in database

I want to set Include in Navigation Menu for all categories to Yes.
Can anybody tell me in which table this value is?
Magento use an eav model to save values in the database.
You have to search an attribute called "include_in_menu" in the "eav_attribute" table.
This attribute have an "attribute_id" which will be retrieve in the other tables.
On my installation, this attribute has attribute_id = 67 which is stored as an INTEGER (int)
On magento all the attributes have a type which you can find on the eav_attribute table.
You want to update your categories, we have to update the table where the integer attribute "include_in_menu" of the categories is saved.
As you can see in your database, you have a lot of tables for the categories :
catalog_category_entity, catalog_category_entity_datetime, catalog_category_entity_decimal, catalog_category_entity_int...
You have to select all your categories from the first table : select entity_id from catalog_category_entity
After you have to select the attribute_id in the table related to the good attribute type. Here is "catalog_category_entity_int" for all the INTEGER attribute...
select value from catalog_category_entity_int where attribute_id = 67 and store_id = ...
Be careful if you have multiple stores...All the stores are on the same table.
And now you just have to update value to "1" on the selected rows.
Sorry for my English, i m french...
Best regards
Cédric
This answer worked on Magento EE 2.2.2.
Based on Cedric's answer I used the following query to remove all of the categories from the menu via Sequel Pro.
You can remove all of your categories from your menu by running:
UPDATE catalog_category_entity_int SET value='0' WHERE attribute_id='67'
You can add all of your categories to your menu by running:
UPDATE catalog_category_entity_int SET value='1' WHERE attribute_id='67'
Then flush your cache (php bin/magento cache:flush) and you should see your changes on the frontend.
Again, be careful of what 'store_id' you're affecting.
You can use following query to update all categories:
update catalog_category_entity_int cci
inner join eav_attribute a on a.attribute_id = cci.attribute_id
set cci.value = 1
where a.attribute_code = 'include_in_menu';

Group join in linq but only where childs is present

I have this simple query and I want to have a list of categories and for each category the number of products inside.
from category in categories
join product in products
on category.Id equals product.Category_Id into productsPerCategories
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};
But if there is no products, I don't want the category to appear. With my current construct, there will be categories with no products inside. How can I achieve that?
Just add a condition
where productsPerCategories.Any()
or
where productsPerCategories.Count() > 0
So your query would be:
from category in categories
join product in products on category.Id equals product.Category_Id into productsPerCategories
where productsPerCategories.Any()
select new Categories
{
Category = category,
products = productsPerCategories.Count()
};

Magento The Amount of times a product has been ordered

I want to show the products that have been purchased the most
so far i am using
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addAttributeToFilter('visibility', $visibility)
->setOrder('ordered_qty', 'desc');
BUT I want to count the amount of time the product has been purchased, disregarding the qty purchased. EG
a customer buys 20 X PRODUCT A (Count this as 1)
a customer buys 1 X PRODUCT B (Count this as 1)
This is because some products are bought in huge quantities so bestsellers data doesn't really reflect our most popular products.
Is this data available?
Cheers
If you have the access of MySQL database then you can easily get the list by query
SELECT sku, count(*)as qty FROM sales_flat_order_item
GROUP BY sku
ORDER BY qty desc
or you can also use Magento Resource Model to get the same
$query = Mage::getResourceModel('sales/order_item_collection');
$query->getSelect()->reset(Zend_Db_Select::COLUMNS)
->columns(array('sku','COUNT(*)'))
->group(array('sku'));

How can I use a compound condition in a join in Linq?

Let's say I have a Customer table which has a PrimaryContactId field and a SecondaryContactId field. Both of these are foreign keys that reference the Contact table. For any given customer, either one or two contacts may be stored. In other words, PrimaryContactId can never be NULL, but SecondaryContactId can be NULL.
If I drop my Customer and Contact tables onto the "Linq to SQL Classes" design surface, the class builder will spot the two FK relationships from the Customer table to the Contact table, and so the generated Customer class will have a Contact field and a Contact1 field (which I can rename to PrimaryContact and SecondaryContact to avoid confusion).
Now suppose that I want to get details of all the contacts for a given set of customers.
If there was always exactly one contact then I could write something like:
from customer in customers
join contact in contacts on customer.PrimaryContactId equals contact.id
select ...
...which would be translated into something like:
SELECT ...
FROM Customer
INNER JOIN Contact
ON Customer.FirstSalesPersonId = Contact.id
But, because I want to join on both the contact fields, I want the SQL to look something like:
SELECT ...
FROM Customer
INNER JOIN Contact
ON Customer.FirstSalesPersonId = Contact.id OR Customer.SecondSalesPersonId = Contact.id
How can I write a Linq expression to do that?
It's rarely correct to use join in LINQ to SQL.
Since you want contacts, why not start your selection there? Presuming the association between Customer and Contact is two-way, you should be able to write something like:
IEnumerable<Guid> customerIds = // ...
var q = from contact in Context.Contacts
where customerIds.Contains(contact.Customer.Id)
select contact;
Use anonymous classes. EG
new { A.Foo, B.Bar } equals new { Foo = B.Baz, Bar = C.Ork }

Resources