I'm new to wordress and php.
I need to display all posts from june 2009 to june 2010.
How can I do that by creating a custom loop?
query_posts() just allow to show posts from a specific week or month. However, you can show posts between two dates, adding a few lines of code. You need to paste this code wherever in your theme you'd like to display.
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2009-06-01' AND post_date <= '2010-06-30'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
while (have_posts()) :
the_post();
the_content();
endwhile;
?>
Source: http://bit.ly/i5zXP0
Using WP Query
Time Parameters
year (int) - 4 digit year (e.g. 2011).
monthnum (int) - Month number (from 1 to 12).
w (int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependent on the "start_of_week" option.
day (int) - Day of the month (from 1 to 31).
hour (int) - Hour (from 0 to 23).
minute (int) - Minute (from 0 to 60).
second (int) - Second (0 to 60).
m (int) - YearMonth (For e.g.: 201307).
Returns posts for just the current date:
$today = getdate();
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );
Returns posts for just the current week:
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );
Return posts for March 1 to March 15, 2010:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Return posts 30 to 60 days old
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
For more reference click here http://codex.wordpress.org/Class_Reference/WP_Query
Related
I have this code to display field order total amount on the order edit page:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
$method = get_post_meta( $order->id, '_payment_method', true );
if($method != 'pec_gateway_check')
return;
echo date('Y/m/d', strtotime("+40 days")) . " تاریخ چک صیادی";
}
I want add below code to show total amount in this page:
How to do that?
global $woocommerce;
$carttotal = $woocommerce->cart->total;
$total35 = $carttotal*0.35;
$total65 = $carttotal*0.65;
echo number_format($total35) . " ریال پرداخت شده";
echo number_format($total65) . " ریال مبلغ چک پرداختی";
From your question, it sounds like you just need to add the code you've written to the function you already have. So something like this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
global $woocommerce;
$method = get_post_meta( $order->id, '_payment_method', true );
if($method != 'pec_gateway_check')
return;
echo date('Y/m/d', strtotime("+40 days")) . " تاریخ چک صیادی";
$carttotal = $woocommerce->cart->total;
$total35 = $carttotal*0.35;
$total65 = $carttotal*0.65;
echo number_format($total35) . " ریال پرداخت شده";
echo number_format($total65) . " ریال مبلغ چک پرداختی";
}
Hi how to see last order of specific product in woocommerce?
EXEMPLE:
PRODUCT NAME
PRICE 10 $ [ADD CART]
LAST ORDER
DATE QUT. PRICE
10.10.2021 2 9$
10.12.2021 6 3$
10.01.2022 39 5$
EDIT: I would like to place it on the product page and as a second viewer user sees his latest product orders.
Place the following function in your functions.php file
function latest_order_by_product_id() {
global $wpdb;
// Select Product ID
$product_id = 14;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}
Call latest_order_by_product_id() where you need it. Result - https://prnt.sc/6AhgTPXFerBg
Example with shortcode. How to use [lobpid product_id="35"]
add_shortcode('lobpid','latest_order_by_product_id' );
function latest_order_by_product_id($attr) {
global $wpdb;
$args = shortcode_atts( array(
'product_id' => '',
), $attr );
// Select Product ID
$product_id = $args['product_id'];
//Skip the rest if we dont have product id defined
if(empty($product_id)): echo 'there is no product id defined'; return; endif;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
//Skip the rest if we dont have orders
if(empty($orders)): echo 'no orders found'; return; endif;
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}
I have this code to display the price.
function edit_selected_variation_price( $data, $product, $variation ) {
$price = $variation->price;
$price_incl_tax = $price + round($price * ( 23 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<div><span class="price">';
$display_price .= '<span>Cena netto</span><span class="amount">' . $price .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</div><div>';
$display_price .= '<span>Cena brutto</span><span class="amount">' . $price_incl_tax .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</span></div>';
$data['price_html'] = $display_price;
return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);
How to get the maximum price displayed without choosing any variants?
I do not fully understand your question, you want the max price from what prices (incl tax and ...)?
However, when you have different prices in an array (or as loose variables), consider using the PHP max() function:
https://www.php.net/manual/en/function.max.php
That will return the highest value (Hope that's what you're after)
I create a custom badge/bubble "New!" based on time published:
add_action( 'woocommerce_before_shop_loop_item', function() {
#add_action( 'woocommerce_single_product_image_html', function() {
$postdate = get_the_time( 'Y-m-d' ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}, 20 );
But i want to add conditional to show if "on sale" is not set.
How should look like?
thanks!
WC_Product::is_on_sale() – Returns whether or not the product is on sale.
function my_callback_function() {
global $product;
$postdate = get_the_date( 'Y-m-d', $product->get_id() ); // Post date
$postdatestamp = strtotime( $postdate ); // Timestamped post date
$newness = 10; // Newness in days
$onsale = $product->is_on_sale();
if ( ( time() - ( 60 * 60 * 24 * $newness ) ) < $postdatestamp && !$onsale ) {
echo '<div class="woo-entry-new-badge"><div class="woo-cust-new-badge">' . esc_html__( 'New!', 'woocommerce' ) . '</div></div>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'my_callback_function', 10, 0 );
This question already has answers here:
Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4
(1 answer)
Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"
(8 answers)
Closed 3 years ago.
Hello I was using woocommerce_add_order_item_meta to add some additional information for order item. After woocommerce update I got error on ordar pay page. I researched that why it happens. I found this article. and I understood why I'm getting error. I changed action hook name but still get errors. What should I do ?
function addBizDays($item_id, $cart_item ,$start, $add){
$start =current_time( 'mysql' );
$add = get_post_meta( $cart_item[ 'product_id' ], 'dp_kargolanma_suresi', true );
$d = new DateTime($start );
$t = $d->getTimestamp();
// loop for X days
for($i=0; $i<$add; $i++){
// add 1 day to timestamp
$addDay = 86400;
// get what day it is next day
$nextDay = date('w', ($t+$addDay));
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
$teslim_tarihi = $d->format( 'd-m-Y' ). "\n";
$desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight());
$kargosinifi = $cart_item['data']->get_shipping_class();
wc_update_order_item_meta( $item_id, 'En Gec Kargolanma Tarihi', $teslim_tarihi );
wc_update_order_item_meta( $item_id, '_desi_kg', $desiveyakg );
wc_update_order_item_meta( $item_id, '_alici_onay_durumu', 'Onay Bekliyor' );
if($cart_item['quantity'] >= 100)
{
wc_update_order_item_meta( $item_id, '_kargo_sinifi', 'ucretsiz-kargo' );
}
else{
wc_update_order_item_meta( $item_id, '_kargo_sinifi', $kargosinifi );
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'addBizDays', 10, 5 );
You need to change more than just the hook… The hooked function arguments are wrong in your code and the way to updated data has changed too. Try the following:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_business_days', 10, 4 );
function add_business_days( $order_item, $cart_item_key, $cart_item, $order ){
$now = current_time( 'mysql' );
$add = $cart_item['data']->get_meta( 'dp_kargolanma_suresi' );
$d = new DateTime( $now );
$t = $d->getTimestamp();
$oneDay = 86400;
$nextDay = date('w', ($t + $oneDay));
// loop for X days
for( $i = 0; $i < $add; $i++ ) {
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t + $oneDay;
}
$d->setTimestamp($t);
$teslim_tarihi = $d->format( 'd-m-Y' ). "\n";
$desiveyakg = ( $cart_item['quantity'] * $cart_item['data']->get_weight() );
$kargosinifi = $cart_item['data']->get_shipping_class();
$order_item->update_meta_data( 'En Gec Kargolanma Tarihi', $teslim_tarihi );
$order_item->update_meta_data( '_desi_kg', $desiveyakg );
$order_item->update_meta_data( '_alici_onay_durumu', 'Onay Bekliyor' );
if($cart_item['quantity'] >= 100) {
$order_item->update_meta_data( '_kargo_sinifi', 'ucretsiz-kargo' );
} else {
$order_item->update_meta_data( '_kargo_sinifi', $kargosinifi );
}
}
It should work…