Woocommerce email invoice, shipping method style by instance_id - wordpress

In woocommerce I have one shipping zone and under it, there is seven shipping methods. Shipping method ids are 4 (local pick up), 6, 7, 8, 9, 10, 10. Last six are delivery. So in first, local pick up, I want to show only shipping method and in others show shipping ,ethod and shipping address
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
$shipping = $order->get_formatted_shipping_address();
$current_shipping_method_id = '';
$current_shipping_method = '';
$type = '';
foreach($order->get_items('shipping') as $item){
$current_shipping_method_id = $item->get_instance_id().'<br>';
}
$shipping_packages = WC()->cart->get_shipping_packages();
foreach( array_keys( $shipping_packages ) as $key ) {
if( $shipping_for_package = WC()->session->get('shipping_for_package_'.$key) ) {
if( isset($shipping_for_package['rates']) ) {
foreach ( $shipping_for_package['rates'] as $rate_key => $rate ) {
$method_id = $rate->method_id;
$instance_id = $rate->instance_id;
if($instance_id == 4){ // Saņemt veikalā
$current_shipping_method = $order->get_shipping_method();
$type = 4;
}
if($instance_id == 6){ // Piegāde darba dienās 9.00-17.00 (nesaskaņots, pēc servisa
vēlmēm)
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 6;
}
if($instance_id == 7){ // Piegāde darba dienās 9.00-17.00 (Saskaņots pēc klienta vēlmēm)
$current_shipping_method = $order->get_shipping_method().'('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 7;
}
if($instance_id == 8){ // Bezmaksas piegāde
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 8;
}
if($instance_id== 9){ // Omniva pakomāts
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 9;
}
if($instance_id == 10){ // Omniva kurjers
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 10;
}
if($instance_id == 11){ // DPD pakomāts
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 11;
}
}
}
}
}
?>
<div class="p2">
<table class="table2">
<tr>
<th>Pasūtītājs:</td>
<td><?php echo $order->get_billing_first_name().' '.$order->get_billing_last_name(); ?>
</td>
</tr>
<tr>
<th>Kontakti:</th>
<td>
<a href="mailto:<?php echo $order->get_billing_email(); ?>"><?php echo esc_html(
$order->get_billing_email() ); ?></a>,
<?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
</td>
</tr>
<tr>
<th>Preces saņemšana</th>
<td><?php echo $order->get_shipping_method(); ?></td>
</tr>
<?php
if($type == 6 || $type == 7 || $type == 8 || $type == 9 || $type == 10 || $type == 11){
?>
<tr>
<th>Saņemšanas vieta:</th>
<td><?php echo $order->get_shipping_address_1().' '.$order->get_shipping_address_2(); ?>
</td>
</tr>
<?php
}
?>
</table>
<?php echo $type; ?>
</div>
After new order, which is selected as local pick up, instance id 4, in email shows that current instance id is 4, but $type 11 why? I don't understand!

Ok, I did it different, but still don't understand why it's not working with type method
Now it's working:
<?php
$instance_ids = array(6, 7, 8, 9, 10, 11);
if(in_array($current_shipping_method_id, $instance_ids)){
?>
<tr>
<th>Saņemšanas vieta:</th>
<td><?php echo $order->get_shipping_address_1().' '.$order->get_shipping_address_2(); ?></td>
</tr>
<?php
}
?>

Related

Update Parent Product Quantity When Variation Quantity Changes

I am actually trying to update the stock quantity of parent products whenever the quantity of any of its variation increases or decreases.
What could be the error in following code.
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
}
}

See last order on specific product

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));
}

Variable width on posts using post count

I'm setting up a loop and with help from the $count I want to have different classes to the posts.
However, I want the $count to be dynamic, now I have to manually state the class depending on if the post count is equal to a number or not, as you can see in the code below.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$loop = new WP_query( $args );
$count = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$count++;
if( $count == 1) {
get_template_part( 'parts/case/item','six');
} elseif ( $count == 2) {
get_template_part( 'parts/case/item','three');
} elseif ( $count == 3) {
get_template_part( 'parts/case/item','three');
} elseif ( $count == 4) {
get_template_part( 'parts/case/item', 'six');
} elseif ( $count == 5) {
get_template_part( 'parts/case/item', 'six');
} elseif ( $count == 6) {
get_template_part( 'parts/case/item', 'three');
} else {
get_template_part( 'parts/case/item', 'three');
}
?>
<?php endwhile; wp_reset_postdata();?>
The number six and three are the classes of the posts.
What I want to execute.
Post 1 = class 6
Post 2 = class 3
Post 3 = class 3
Post 4 = class 6
Post 5 = class 6
Post 6 = class 3
Post 7 = class 3
Post 8 = class 6
Post 9 = class 6
And so forth.
Maybe, I'm looking at it the wrong way, the $count might not even be the way to go.
Appreciate all help.
Thank you.
You can try like this, without comparing each count individually.
if( $count == 1 || $count == 4 || $count == 5) {
get_template_part( 'parts/case/item','six');
} else {
get_template_part( 'parts/case/item', 'three');
}
Or you can even try like this
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$loop = new WP_query( $args );
$count = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$count++;
get_template_part( 'parts/case/item', 'somecontentpart');
?>
<?php endwhile; wp_reset_postdata();?>
then in template part's class add following code
class="<?php echo ( $count == 1 || $count == 4 || $count == 5) ? 'class6' : 'class3';?>"

Php Excel DateStamd Formatted date

I am reading an xlsx file and the fields are in DateStamd I want to convert them to a date data here I leave my progress
Excel A1 = 43160
Converte date = 01/03/2018
enter code hererequire '../PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require '../cnx/conection.php';
$objPHPExcel = PHPExcel_IOFactory::load('../../date.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$numRows = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
for ($i = 2; $i <= $numRows; $i++) {
$pm = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
echo $pm;
$sql = "INSERT INTO regfecha (reg_fecha) VALUES('$pm')";
$result = $mysqli->query($sql);
}
`
here I have another example and I do not know how to apply it
`
DATEVALUE
Converts a date in the form of text to a serial number.`
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();
// Add some data
$testDates = array( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
'17-03', '03-2012', '29 Feb 2011', '03-05-07',
'03-MAY-07', '03-13-07',
);
$testDateCount = count($testDates);
for($row = 1; $row <= $testDateCount; ++$row) {
$worksheet->setCellValue('A'.$row, $testDates[$row-1]);
$worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')');
$worksheet->setCellValue('C'.$row, '=B'.$row);
}
$worksheet->getStyle('C1:C'.$testDateCount)
->getNumberFormat()
->setFormatCode('yyyy-mmm-dd');
echo '<hr />';
// Test the formulae
?>
<p><strong>Warning: </strong>The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.</p>
<table border="1" cellspacing="0">
<tr>
<th>Date String</th>
<th>Formula</th>
<th>Excel DateStamp</th>
<th>Formatted DateStamp</th>
</tr>
<?php
for ($row = 1; $row <= $testDateCount; ++$row) {
echo '<tr>';
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
echo '</tr>';
}
?>

Break Up WordPress Archive List into Multiple Columns

I want to break up this archive list into multiple columns (three columns)...I don't want to use CSS3 for this. I can easily do this with wp_list_categories, but that method doesn't work with wp_get_archives.
Ideally, I'd like to implement a counter to add in tags at an approximate 1/3 count...then I can use CSS for the rest.
CUSTOM ARCHIVE FUNCTION:
function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_custom_archive' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_custom_archive', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
}
}
$output .= '</ul>';
if ( $echo )
echo $output;
else
return $output;
}
CURRENT HTML OUTPUT (shortened):
<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>
If I understand correctly, you would have to do something like this
$output = '<ul class="col1">';
if ( $arcresults ) {
$numarticles = count($arcresults);
$column1 = round($numarticles/3);
$column2 = round($column1*2);
$counter = 0;
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
if($counter == $column1) {
$output .= "</ul><ul class='col2'>";
} elseif($counter == $column2) {
$output .= "</ul><ul class='col3'>";
}
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
$counter ++;
}
$output .= '</ul>';
}
This way, when the counter gets to a third of the way, it will add another list, so at the end you will have 3 lists.
Hope this helps.

Resources