Advertisement
Guest User

Untitled

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