Advertisement
Guest User

Untitled

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