Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. // Call datepicker functionality in your custom text field
  2. <?php
  3. add_action('woocommerce_after_order_notes', 'ncydesign_end_datepicker_field', 10, 1);
  4. function ncydesign_end_datepicker_field( $checkout ) {
  5.  
  6. date_default_timezone_set('America/Los_Angeles');
  7. $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
  8.  
  9. echo '<div id="my_custom_checkout_field">
  10. <h3>'.__('Delivery Date').'</h3>';
  11. $today = date("Y-m-d");
  12. echo '<div id="start_date_checkout_field"><input type="date" min="'.$today.'" onchange="changeamount(this.value)" name="calendarstart">';
  13. // YOUR SCRIPT HERE BELOW
  14. echo '
  15. <script>
  16. jQuery(function($){
  17. $("#datepickerend").datepicker();
  18. });
  19. </script>';
  20.  
  21. /* woocommerce_form_field( 'delivery_date_end', array(
  22. 'type' => 'text',
  23. 'class' => array('my-field-class form-row-wide'),
  24. 'id' => 'datepickerend',
  25. 'required' => false,
  26. 'label' => __('Delivery Date'),
  27. 'placeholder' => __('Select Date'),
  28. 'options' => $mydateoptions
  29. ),
  30. $checkout->get_value( 'cylinder_collet_date' ));*/
  31.  
  32. echo '</div>';
  33. }
  34. /**
  35. * Process the checkout
  36. **/
  37. add_action('woocommerce_checkout_process', 'my_custom_checkout_field_end_process');
  38.  
  39. function my_custom_checkout_field_end_process() {
  40. global $woocommerce;
  41.  
  42. // Check if set, if its not set add an error.
  43. if (!$_POST['delivery_date_end'])
  44. wc_add_notice( '<strong>Delivery Date</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
  45. }
  46. /**
  47. * Update the order meta with custom fields values
  48. * */
  49. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_end_order_meta');
  50.  
  51. function my_custom_checkout_field_update_end_order_meta($order_id) {
  52.  
  53. if (!empty($_POST['delivery_date_end'])) {
  54. update_post_meta($order_id, 'End Date', sanitize_text_field($_POST['delivery_date_end']));
  55. }
  56. }
  57. add_action( 'wp_enqueue_scripts', 'enabling_start_date_picker' );
  58. function enabling_start_date_picker() {
  59.  
  60. // Only on front-end and checkout page
  61. if( is_admin() || ! is_checkout() ) return;
  62.  
  63. // Load the datepicker jQuery-ui plugin script
  64. wp_enqueue_script( 'jquery-ui-datepicker' );
  65. }
  66.  
  67. // Call datepicker functionality in your custom text field
  68. add_action('woocommerce_after_order_notes', 'ncydesign_start_datepicker_field', 10, 1);
  69. function ncydesign_start_datepicker_field( $checkout ) {
  70.  
  71. date_default_timezone_set('America/Los_Angeles');
  72. $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
  73.  
  74. echo '<div id="start_date_checkout_field">
  75. <h3>'.__('start Date').'</h3>';
  76.  
  77.  
  78. global $woocommerce;
  79.  
  80. //print_r($_COOKIE);
  81. $amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
  82. // YOUR SCRIPT HERE BELOW
  83. //print_r($woocommerce->cart);
  84. echo $amount;
  85. ?>
  86. <input type="date" onchange="changeamount(this.value)" name="calendarend">
  87. <script>
  88. jQuery(function($){
  89. $("#datepickerstart").datepicker();
  90. });
  91.  
  92. function changeamount($thiss){
  93. var amouns = '<?php echo $amount ?>';
  94. var s = jQuery( "input[name=calendarstart]" ).val();
  95.  
  96. var e = jQuery( "input[name=calendarend]" ).val();
  97.  
  98. if(s){
  99. if(e){
  100.  
  101.  
  102.  
  103.  
  104. jQuery.ajax({
  105. url: "<?php echo admin_url('admin-ajax.php') ?>",
  106. type: "POST",
  107. data: "action=cats&d1="+s+"&d2="+e+"&amount="+amouns, // можно также передать в виде массива или объекта
  108. success: function( data ) {
  109. //alert(data);
  110. //woocommerce-Price-currencySymbol
  111. var cur = jQuery( ".woocommerce-Price-currencySymbol:eq(1)" ).text();
  112. //alert(data);
  113. jQuery( ".woocommerce-Price-amount:eq(1)" ).text(cur+data);
  114. jQuery( ".woocommerce-Price-amount:eq(2)" ).text(cur+data);
  115. }
  116. });
  117. }
  118. }
  119. }
  120.  
  121. </script>
  122. <?php
  123.  
  124.  
  125.  
  126.  
  127. add_action('woocommerce_checkout_process', 'my_custom_checkout_start_field_process');
  128.  
  129. function my_custom_checkout_start_field_process() {
  130. global $woocommerce;
  131.  
  132. // Check if set, if its not set add an error.
  133. if (!$_POST['delivery_date_start'])
  134. wc_add_notice( '<strong>Delivery Date</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
  135. }
  136. /**
  137. * Update the order meta with custom fields values
  138. * */
  139. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
  140.  
  141. function my_custom_checkout_field_update_order_meta($order_id) {
  142.  
  143. if (!empty($_POST['delivery_date_start'])) {
  144. update_post_meta($order_id, 'start Date', sanitize_text_field($_POST['delivery_date_start']));
  145. }
  146. }
  147. add_action( 'wp_ajax_cats', 'sets_days_amount' );
  148. add_action( 'wp_ajax_nopriv_cats', 'sets_days_amount' );
  149. function sets_days_amount(){
  150. // Первая дата
  151. $d1 = $_POST['d1'];
  152.  
  153. // Вторая дата
  154. $d2 = $_POST['d2'];
  155.  
  156. $amoun = $_POST['amount'];
  157.  
  158. // timestamp первой даты
  159. $d1_ts = strtotime($d1);
  160.  
  161. // timestamp второй даты
  162. $d2_ts = strtotime($d2);
  163.  
  164. // Количество секунд
  165. // Функция abs нужна, чтобы не проверять какая из двух дат больше
  166. $seconds = abs($d1_ts - $d2_ts);
  167.  
  168. // Количество дней нужно округлить в меньшую сторону,
  169. // чтобы узнать точное количество прошедших дней
  170. // 86400 - количество секунд в 1 дней (60 * 60 * 24)
  171. $days = floor($seconds / 86400);
  172.  
  173. //return $days;//3
  174.  
  175. $daysCoupon = 2;
  176. $coupon = 10;
  177.  
  178. $coupon = $coupon/100;
  179. $datetimeFrom = DateTime::createFromFormat( 'Y-m-d', $d1 );
  180. $datetimeTo = DateTime::createFromFormat( 'Y-m-d', $d2 );
  181.  
  182. $days = $datetimeFrom->diff( $datetimeTo )->format( '%a' ); # 366 days
  183.  
  184.  
  185. if((int)$days >= 8 AND (int)$days <= 14){
  186.  
  187. $coupon = 5;
  188.  
  189. $coupon = $coupon/100;
  190. $amoun = $amoun*$days;
  191. $skid = $amoun*$coupon;
  192.  
  193. $am =$amoun-$skid;
  194. global $woocommerce;
  195.  
  196. add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
  197. add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
  198. function custom_cart_total() {
  199.  
  200. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  201. return;
  202. global $am;
  203. WC()->cart->total = $am;
  204. }
  205. echo $am;
  206. die();
  207. }elseif($days > 14){
  208. $coupon = 10;
  209. $coupon = $coupon/100;
  210. $amoun = $amoun*$days;
  211. $skid = $amoun*$coupon;
  212.  
  213. $am =$amoun-$skid;
  214. global $woocommerce;
  215.  
  216. add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
  217. add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
  218. function custom_cart_total() {
  219.  
  220. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  221. return;
  222. global $am;
  223. WC()->cart->total = $am;
  224. //var_dump( WC()->cart->total);
  225. }
  226. echo $am;
  227. die();
  228. }else{
  229. echo (int)$days*$amoun;
  230. die();
  231. }
  232.  
  233.  
  234.  
  235. //print_r( $_POST);
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement