Magento Sales Order tables - magento

Hello I am trying to get all customer who has bought this certain product. Although I am new to Magento So need your help. This is what I did so far .
SELECT o.customer_id,i.product_id ,i.NAME
FROM sales_flat_order_item i INNER JOIN sales_flat_order o
ON o.entity_id = i.order_id
WHERE o.customer_id IS NOT NULL
AND product_id = '8' OR product_id = '14' OR product_id = '15' OR product_id = '16' ;
I need to get only the customers who's payment has been approved for these products(Where can I check that) .
How this will work if the payments were made by recurring payments ?
Please provide some way.

Related

Customer details based on booking count

Write a query to display user id and user name where the number of seats booked by the user in the individual booking is greater than 1. Display records in ascending order by user name.
I have tried this query and I m getting errors. Please help me!!
select u.user_id,u.name
from users u join bookingdetails bd
on u.name=bd.name
join tickets t on u.user_id=t.user_id
group by u.name
having count(t.no_seats) > 1
order by u.name;
select distinct u.user_id,u.name from users u
join tickets t on u.user_id = t.user_id
where
u.user_id in( select user_id from tickets where no_seats>1)
order by u.name;
If I understand your assignment it looks like you should do
WITH cteBookings AS (SELECT bd.USER_ID, SUM(t.NO_SEATS) AS TOTAL_SEATS_BOOKED
FROM BOOKINGDETAILS bd
INNER JOIN PAYMENTS p
ON p.BD_ID = bd.BD_ID
INNER JOIN TICKETS t
ON t.TICKET_ID = p.TICKET_ID
GROUP BY bd.USER_ID)
SELECT DISTINCT b.USER_ID, u.USER_NAME
FROM USERS u
INNER JOIN cteBookings b
ON b.USER_ID = u.USER_ID
WHERE b.TOTAL_SEATS_BOOKED > 1
ORDER BY u.USER_NAME ASC
Best of luck.
According to the question, it asks for the individual bookings > 1
So, the solution is very simple i.e, for each ticket_id, the no of seats should be greater than 1.
SELECT DISTINCT(user_id),name
FROM Tickets inner join Users
USING (user_id)
WHERE no_seats>1
ORDER BY name;
It will simpler if you try it without joins.
A simple example using a subquery:
select user_id, name from users
where user_id = any(select distinct user_id from tickets where no_seats > 1)
order by name asc;

Magento querying customers by eav option value

We created custom customer attributes for date of birth:
dob_month, dob_day
They are frontend input type of select, and backend type of int. We are trying to get from customers following fields: entity_id, dob, and store_id (if possible one dob field, otherwise, separated into dob_month and dob_day).
Here's were we are struggling, we would like to filter by these option values. Example: dob_month = 05 and dob_day = 08
Everything we have tried we are either having to filter by the value stored in customer_entity_int for example: 1125 = eav_option_value of 05 or we are only able to retrieve the value stored in customer_entity_int.
I figure there must be a way. Any help you can provide would be greatly appreciated.
Here's the closest I have gotten:
$collection = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('email')
->addAttributeToSelect('dob_month', 'left')
->addAttributeToSelect('dob_day', 'left')
->addExpressionAttributeToSelect('dob', "CONCAT({{dob_month}}, '-',{{dob_day}})", array(
'dob_month', 'dob_day'
))
->addAttributeToFilter('dob_month', array(
array('eq' => '1125')
))
/*->addAttributeToFilter('dob_day', array(
array('eq' => '08')
))*/
->setPageSize(20)
->setCurPage(1);
Note: The PageSize and CurPage is just for testing to limit the results.
In case anyone wants it, the best I got was to do it all via SQL:
SELECT e.entity_id,CONCAT(dob_month_eav.value, '-',dob_day.dob_day) as dob,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv
ON ( cv.attribute_id = (
SELECT attribute_id FROM eav_attribute
WHERE entity_type_id = e.entity_type_id
AND attribute_code = 'dob_month'
)
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_month_eav` ON dob_month_eav.option_id=cv.value
LEFT JOIN (SELECT e.entity_id,dob_day_eav.value as dob_day,e.store_id FROM customer_entity AS e
LEFT JOIN customer_entity_int AS cv
ON ( cv.attribute_id = (
SELECT attribute_id FROM eav_attribute
WHERE entity_type_id = e.entity_type_id
AND attribute_code = 'dob_day'
)
AND cv.entity_id = e.entity_id)
LEFT JOIN `eav_attribute_option_value` as `dob_day_eav` ON dob_day_eav.option_id=cv.value) as dob_day ON dob_day.entity_id=e.entity_id
WHERE dob_month_eav.store_id=0 AND dob_month_eav.value='05' AND dob_day='08'

Magento Upsell Products not visible if it's category and products category are different

Hi i'm having below categories
Camera
- DSLR
- Digital Camers
Accessories
- Lenses
- Memory cards
I've added a product from lenses category as a upsell to a product from Digital cameras. but it's not displaying in frontend.
It can be viewable only if i add dslr as one more category for lens product (Added as upsell to dslr product).
Is there any way to show upsell products from entirely different category?
Cross check below things:
The products you want to show are:
1. enabled
2. visible on frontend
3. in stock
4. should have been assigned in atleast 1 category.
5. must be visible in same website (in case of multi-websites).
then flush Magento cache and recheck frontend.
Try adding the product you want to display in upsell to the Default Category. This has been driving me crazy for the last hour - trial and error, worked for me!
I have the same issue and as workaround, I have added my product into the same category than my upsell products. In my case, my product is not visible (It is the target of google search) and I just change the visibility to search.
Magento executes the following query:
SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `e`.`name`, `e`.`short_description`, `e`.`price`, `e`.`special_price`, `e`.`special_from_date`, `e`.`special_to_date`, `e`.`small_image`, `e`.`thumbnail`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`url_key`, `e`.`required_options`, `e`.`image_label`, `e`.`small_image_label`, `e`.`thumbnail_label`, `e`.`msrp_enabled`, `e`.`msrp_display_actual_price_type`, `e`.`msrp`, `e`.`tax_class_id`, `e`.`price_type`, `e`.`weight_type`, `e`.`price_view`, `e`.`shipment_type`, `e`.`links_purchased_separately`, `e`.`links_exist`, `cat_index`.`position` AS `cat_index_position`, `links`.`link_id`, `link_attribute_position_int`.`value` AS `position` FROM `catalog_product_flat_1` AS `e`
INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = '1'
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.visibility IN(2, 4) AND cat_index.category_id = '2'
INNER JOIN `catalog_product_link` AS `links` ON links.linked_product_id = e.entity_id AND links.link_type_id = 4
LEFT JOIN `catalog_product_link_attribute_int` AS `link_attribute_position_int` ON link_attribute_position_int.link_id = links.link_id AND link_attribute_position_int.product_link_attribute_id = '4'
WHERE (e.entity_id NOT IN((SELECT `sales_flat_quote_item`.`product_id` FROM `sales_flat_quote_item` WHERE (quote_id = '')))) AND (links.product_id = 678) AND (e.entity_id != '678')
ORDER BY `position` ASC LIMIT 4
Here the category is '2' (cat_index.category_id = '2').
Otherwise you must modify the Magento code.

How to get product manufacturer name from manufacturer value id by mysql?

I know how to get the product manufacturer id, but I need the product manufacturer id & name using MySQL.
75 is the attribute id for getting product manufacturer id.
Anyone can help?
I want it in SQL format.
First of all I want to get this out of the way.
If you have the product as an object you can get the manufacturer id like this:
$product->getManufacturer();
and for the name:
$product->getAttributeText('manufacturer');
Now for the sql part...I don't know why you would need it but here goes.
Let's say the product id is 10.
Try this query.
SELECT
i.value as manufacturer_id,
v.value as manufacturer_name
FROM
catalog_product_entity_int i
LEFT JOIN
eav_attribute_option o
ON i.value = o.option_id
LEFT JOIN
eav_attribute_option_value v
ON o.option_id = v.option_id AND
v.store_id = 0
WHERE
i.entity_id = 10 AND
i.attribute_id = 75 AND
i.store_id = 0;
if you want the values for an other store view just replace the 0 in v.store_id = 0 and i.store_id = 0 with the value of your store id. 0 means default values.
Great code, thanks!
On my Magento 2.3.1 i have just changed attribute_id to '83' and added SKU. It works correctly.
SELECT
e.sku as SKU,
i.value as manufacturer_id,
v.value as manufacturer_name
FROM
catalog_product_entity_int i
LEFT JOIN
eav_attribute_option o
ON i.value = o.option_id
LEFT JOIN
eav_attribute_option_value v
ON o.option_id = v.option_id AND
v.store_id = 0
LEFT JOIN
catalog_product_entity e
ON i.entity_id = e.entity_id
WHERE
i.attribute_id = 83 AND
i.store_id = 0
ORDER BY `SKU` DESC

Tracking Unregistered Abandon carts in Magento

Does anyone know where I can start trying to get at cart information from unregistered users. I want to be able to look at abandon carts from people who are not logged in. This doesn't seem to be a default function of magento, but I have a feeling it's in the db somewhere.
all quotes are stored in sales_flat_quote table
I have checked this query and find one or two cases where some results are missing in the results but shows on the magento abandoned cart report. Here is what I tried.
SELECT entity_id, customer_firstname, customer_email, items_count, grand_total, created_at
FROM sales_flat_quote
WHERE entity_id NOT IN (SELECT quote_item_id AS quote_id FROM sales_flat_order_item)
AND items_count >0
AND customer_email IS NOT NULL
ORDER BY `sales_flat_quote`.`created_at` DESC
In case anyone is interested, this is the sql query that I came up with for getting abandon cart items directly out of the database. You'll need to change the date
SELECT quote_id, sku, qty, price, custom_price, row_total, created_at, product_id, store_id
FROM mage_sales_flat_quote_item
WHERE created_at > "2011-04-01"
AND quote_id NOT IN (SELECT quote_item_id AS quote_id FROM mage_sales_flat_order_item)
AND store_id IS NOT NULL
This is more like it.
SELECT `main_table`.*, GROUP_CONCAT(item.sku) AS `skus`, (main_table.base_subtotal_with_discount*main_table.base_to_global_rate) AS `subtotal`, `cust_email`.`email`, `cust_fname`.`value` AS `firstname`, `cust_lname`.`value` AS `lastname`, CONCAT_WS(' ', cust_fname.value, cust_lname.value) AS `customer_name` FROM `sales_flat_quote` AS `main_table`
INNER JOIN `sales_flat_quote_item` AS `item` ON item.quote_id = main_table.entity_id
INNER JOIN `customer_entity` AS `cust_email` ON cust_email.entity_id = main_table.customer_id
INNER JOIN `customer_entity_varchar` AS `cust_fname` ON cust_fname.entity_id = main_table.customer_id AND cust_fname.attribute_id = 5
INNER JOIN `customer_entity_varchar` AS `cust_lname` ON cust_lname.entity_id = main_table.customer_id AND cust_lname.attribute_id = 7 WHERE (items_count != '0') AND (main_table.is_active = '1') AND (main_table.created_at >= '2013-02-04 00:00:00' AND main_table.created_at <= '2013-02-04 24:00:00') AND (main_table.updated_at >= '2013-02-04 00:00:00' AND main_table.updated_at <= '2013-02-04 24:00:00') GROUP BY `item`.`quote_id` ORDER BY updated_at DESC

Resources