Show expected delivery date on Product page – Learn WordPress from a to z

Tutorials 0 lượt xem

What is the expected date of woocommerce products?

For example, Monday before 16:00 – this code is printed with a message just below the product details page after the add cart button, explaining that if you order before 16:00, the products will be shipped today .

Please note the function ” date(‘N’) ” and ” date_default_timezone_set() “, in PHP will return current day of week and current time of day so I can compare them with local time & present.

Also use the function ” date_default_timezone_set() ” in case you want to set a different time zone, it is very important.

add_action( 'woocommerce_after_add_to_cart_form', 'dispatch_info_single_product' );
    
function dispatch_info_single_product() {
   date_default_timezone_set( 'Europe/London' );  
    
   // if FRI/SAT/SUN delivery will be MON
   if ( date( 'N' ) >= 5 ) {
      $del_day = date( "l jS F", strtotime( "next monday" ) );
      $order_by = "Monday";
   } 
    
   // if MON/THU after 4PM delivery will be TOMORROW
   elseif ( date( 'H' ) >= 16 ) {
      $del_day = date( "l jS F", strtotime( "tomorrow" ) );
      $order_by = "tomorrow";
   } 
    
   // if MON/THU before 4PM delivery will be TODAY
   else {
      $del_day = date( "l jS F", strtotime( "today" ) );
      $order_by = "today";
   }
 
   $html = "<br><div class="woocommerce-message" style="clear:both">Order by 4PM {$order_by} for delivery on {$del_day}</div>";
    
   echo $html;
}<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span>
</div>

Epilogue

With just the above simple code, you have displayed the expected date of the woocommerce product.

If you find it interesting, you can follow the  wordpress tips section  to know more new knowledge.

Follow fanpage to receive the latest posts:  Group

Wish you have interesting and interesting knowledge about wordpress!

Bài viết liên quan