Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created December 23, 2021 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorduchenko/f210366c6f2c4b8a00b1adeb1a522b5f to your computer and use it in GitHub Desktop.
Save ihorduchenko/f210366c6f2c4b8a00b1adeb1a522b5f to your computer and use it in GitHub Desktop.
Woocommerce estimated shipping date, based on stock and shipping method
<table class="shop_table mb-4">
<tfoot>
<!-- Skipped top part -->
<!-- Stock part -->
<?php $stock_values = array(); ?>
<?php $max_stock_period = 0; ?>
<?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { ?>
<?php $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); ?>
<?php $stock_status = $_product->get_stock_status(); ?>
<?php array_push($stock_values, $stock_status); ?>
<?php } ?>
<?php if ( in_array( 'onbackorder', $stock_values ) ) { ?>
<?php $max_stock_period = 14; ?>
<?php } elseif ( in_array( 'instock', $stock_values ) ) { ?>
<?php $max_stock_period = 3; ?>
<?php } ?>
<!-- END Stock part -->
<!-- Delivery method part -->
<?php
$current_shipping_method_arr = WC()->session->get( 'chosen_shipping_methods' );
$current_shipping_method = $current_shipping_method_arr[0]; ?>
<?php
$shipping_days = 0;
switch ($current_shipping_method) {
case 'flat_rate:2':
$shipping_days = 30;
break;
case 'flat_rate:16':
$shipping_days = 14;
break;
case 'flat_rate:7':
$shipping_days = 10;
break;
case 'nova_poshta_shipping:20':
$shipping_days = 3;
break;
default:
$shipping_days = 0;
} ?>
<!-- END Delivery method part -->
<?php $date = date('d.m.y'); ?>
<?php $estimated_days = $max_stock_period + $shipping_days; ?>
<?php $estimateddate = date( 'd.m.y', strtotime(" + $estimated_days days") ); ?>
<tr>
<th>
<?php _e( 'Estimated shipping date', 'woocommerce' ); ?>:
</th>
<td>
<strong>
<?php echo $estimateddate; ?>
</strong>
</td>
</tr>
<tr class="order-total">
<th><?php _e( 'Total', 'woocommerce' ); ?></th>
<td><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
<?php do_action( 'woocommerce_review_order_after_order_total' ); ?>
</tfoot>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment