Building an eShop 'Latest Products' - asp.net

I was trying to do a 'Latest Products' page for an eShop project I'm working on. Basically, I want to show a number of products, say 10, from a table in my db that where added in my database in the last 30 days or less.
First I tried to use the GridView function in VB.NET, where it auto-populates the table but can't be limited and then I tried this SQL statement, which isn't working, giving me an error.
SELECT *
FROM Product
WHERE DateAdded > (SELECT DATEADD(d,-30,(SELCT MAX(DateAdded) FROM Product)) AS "Last 30 Products Added");
ORDER BY DateAdded DESC
My database has a table called 'Product' which has various columns:
ProductID
CategoryID
ProductModelNo
ProductImage
ProductName
UnitName
ProductActive
DateAdded
Any idea on how I can solve the problem ?

You are looking for the DATEDIFF function to compare the DateAdded with NOW().

Related

Get data from database into custom field

In our country we have to appear the discounted product previous sales price. (cheapest sales price from the last 30 days)
I found a plugin on github, which is saving the previous product prices, and save it into woocommerce_prices_history_products database table.
I created a custom field in the available product variation section. Is it possible to get data from that database table?
Other question. The plugin saves prices, but I need a filter to appear the cheapest price from the last 30 days.
https://github.com/pogla/Woocommerce-Save-Product-Price-History
Any idea how can i do this?
Yes, you can use $wpdb global object to get the value from desired table. You can do something like this:
$product_id = xxx; //PUT your product id here
$serialized_data = $wpdb->get_var( "SELECT data FROM woocommerce_prices_history_products WHERE product_id=$product_id" );
$price_array = unserialize($serialized_data);
With this array you can iterate, check that saved date is less than 30 days and retrieve the cheaper price:
foreach($price_array as $date => $price) {
//Check that $data is less than 30 days
//Check that $price['s_p'] is the cheapest one.
}

woocommerce update sale price

I currently have a form where the admin can bulk update sale prices of products within one screen.
When the form is submitted i simply use update_post_meta like this:
update_post_meta($id,'_sale_price', 'new sale price here');
This updates the _sale_price meta key. When i go into the admin to check this, the new sale price has been inserted. When i view the product on the front end, the item is not marked as on sale. I have to go back in and re-save the product.
My question is, does woocommerce add some other meta_key to mark the product as on sale? I have had a dig round in the database for all the custom fields inserted, but can only see _sale_price.
Any help would be greatly appreciated
Having a look at the Product abstract class in WooCommerce the following php code gets whether or not the product is on sale:
return ( $this->sale_price != $this->regular_price && $this->sale_price == $this->price );
Which seems to indicate that the _price has to be the same as the sale_price in order for it to be classed as on sale. So just
update_post_meta($id, '_price', 'new sale price here too');
as well as the code in your question and it should work.

pagerfanta - DoctrineORMAdapter Sorting

Although I know this is trivial I'm stuck trying to implement the pagerfanta Paginator using the DoctrineORMAdapter, I want to paginate all entities sorted by id in descending order, the final SQL I want is this:
SELECT id, name FROM User ORDER BY id DESC LIMIT 0, 5;
Suppose I had users from A to Z and I want to limit them by 5 each page, what DoctrineORMAdapter is paginating results in User E to A listed in the first page, while what I actually expect is to see User Z to user V in the first page, U to Q in the second page and so on. The DQL I'm passing to DoctrineORMAdapter is as follow:
SELECT u FROM My\FluffyBundle\Entity\User u ORDER BY u.id DESC
On execution this is the final DQL for the first page:
SELECT DISTINCT id0 FROM (SELECT u0_.id AS id0, u0_.name AS name1 FROM User u0_
ORDER BY u0_.id DESC) dctrn_result LIMIT 5 OFFSET 0
Please note that when using the ArrayAdapter instead of DoctrineORM's it works as expected, but it's not a good idea to rely on ArrayAdapter when you have thousands of complex Doctrine Entities, not even with extra lazy loading :D.
This is the only relevant code:
$queryBuilder = $repo->createQueryBuilder('u')->orderBy('u.id', 'DESC');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);
Thanks.
This will help you:
$adapter = new DoctrineORMAdapter($queryBuilder, false);
Had the same problem this morning.
By default Pagerfanta is treating your query as one with joins. Setting second argument to false makes it use simple query handling.
In Kunstmaan Bundle, in AdminListConfiguration class, you have to overide function that is creating Pagerfanta, if you want to sort simple entity.

How to query all related items in a many to many relationship using Pods API?

I have two Pods:
course
participant
The pod course has a PICK field to the pod participant. The field is a multiple relationship field. So, each course item has multiple participants.
I want to find all the course items where a certain participant is related.
So, I guess a SQL query such as the following would do what I want:
SELECT DISTINCT `t`.* FROM `wp_pods_course` AS `t` WHERE t.id IN
(SELECT DISTINCT r.item_id FROM wp_podsrel AS r WHERE r.related_item_id = '42')
42 is the id of a participant.
I am trying to figure out how to write such a SQL query using Pods API:
$pod = pods('course');
$participant_id = $participant->field('id');
$params['where'] = "t.id in (SELECT r.id FROM ??? WHERE ???)";
$pod->find($params);
Is this the correct way to write such a query?
You're overcomplicating things, Pods does all of the joins for you automatically with one of it's most powerful features, field traversal.
Try this:
$pod = pods( 'course' );
// I use ->id() because it's always the right ID field, no matter what pod type
$participant_id = $participant->id();
$params = array(
'where' => 'participants.id = ' . (int) $participant_id
);
$pod->find( $params );
Where particpants is your relationship field name, id is the field id on that related object.

matching fields in a database, ASP.net (VB)

In a table I have news posts with these fields:
Title
Content
OwnerID
And a users table
ID
Name
Surname
The OwnerID relates to the ID in the users table, how can I get the name of the user who's ID matches the OwnerID?
I'm writing a website in ASP.net (VB).
You would need to join the two tables together like this:
select users.Name
from news inner join users
on OwnerID = ID;
This query has no where clause to filter the results that are returned so this query would return all users who are associated with a news record. If you wanted to find users associated with a specific news record you would need to filter on the news title or content like this:
select users.Name
from news inner join users
on OwnerID = ID
where Title = 'Some Title';

Resources