Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 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. ?>
  85. <input type="date" onchange="changeamount(this.value)" name="calendarend">
  86. <script>
  87. jQuery(function($){
  88. $("#datepickerstart").datepicker();
  89. });
  90.  
  91. function changeamount($thiss){
  92. var amouns = '<?php echo $amount ?>';
  93. var s = jQuery( "input[name=calendarstart]" ).val();
  94.  
  95. var e = jQuery( "input[name=calendarend]" ).val();
  96.  
  97. if(s){
  98. if(e){
  99.  
  100.  
  101.  
  102.  
  103. jQuery.ajax({
  104. url: "<?php echo admin_url('admin-ajax.php') ?>",
  105. type: "POST",
  106. data: "action=cats&d1="+s+"&d2="+e+"&amount="+amouns, // можно также передать в виде массива или объекта
  107. success: function( data ) {
  108. //alert(data);
  109. //woocommerce-Price-currencySymbol
  110. var cur = jQuery( ".woocommerce-Price-currencySymbol:eq(1)" ).text();
  111. //alert(data);
  112. jQuery( ".woocommerce-Price-amount:eq(1)" ).text(cur+data);
  113. jQuery( ".woocommerce-Price-amount:eq(2)" ).text(cur+data);
  114. }
  115. });
  116. }
  117. }
  118. }
  119.  
  120. </script>
  121. <?php
  122.  
  123.  
  124.  
  125.  
  126. add_action('woocommerce_checkout_process', 'my_custom_checkout_start_field_process');
  127.  
  128. function my_custom_checkout_start_field_process() {
  129. global $woocommerce;
  130.  
  131. // Check if set, if its not set add an error.
  132. if (!$_POST['delivery_date_start'])
  133. wc_add_notice( '<strong>Delivery Date</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
  134. }
  135. /**
  136. * Update the order meta with custom fields values
  137. * */
  138. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
  139.  
  140. function my_custom_checkout_field_update_order_meta($order_id) {
  141.  
  142. if (!empty($_POST['delivery_date_start'])) {
  143. update_post_meta($order_id, 'start Date', sanitize_text_field($_POST['delivery_date_start']));
  144. }
  145. }
  146. add_action( 'wp_ajax_cats', 'sets_days_amount' );
  147. add_action( 'wp_ajax_nopriv_cats', 'sets_days_amount' );
  148. function sets_days_amount(){
  149. // Первая дата
  150. $d1 = $_POST['d1'];
  151.  
  152. // Вторая дата
  153. $d2 = $_POST['d2'];
  154.  
  155. $amoun = $_POST['amount'];
  156.  
  157. // timestamp первой даты
  158. $d1_ts = strtotime($d1);
  159.  
  160. // timestamp второй даты
  161. $d2_ts = strtotime($d2);
  162.  
  163. // Количество секунд
  164. // Функция abs нужна, чтобы не проверять какая из двух дат больше
  165. $seconds = abs($d1_ts - $d2_ts);
  166.  
  167. // Количество дней нужно округлить в меньшую сторону,
  168. // чтобы узнать точное количество прошедших дней
  169. // 86400 - количество секунд в 1 дней (60 * 60 * 24)
  170. $days = floor($seconds / 86400);
  171.  
  172. //return $days;//3
  173.  
  174. $daysCoupon = 2;
  175. $coupon = 10;
  176.  
  177. $coupon = $coupon/100;
  178. $datetimeFrom = DateTime::createFromFormat( 'Y-m-d', $d1 );
  179. $datetimeTo = DateTime::createFromFormat( 'Y-m-d', $d2 );
  180.  
  181. $days = $datetimeFrom->diff( $datetimeTo )->format( '%a' ); # 366 days
  182.  
  183.  
  184. if((int)$days >= 8 AND (int)$days <= 14){
  185.  
  186. $coupon = 5;
  187.  
  188. $coupon = $coupon/100;
  189. $amoun = $amoun*$days;
  190. $skid = $amoun*$coupon;
  191.  
  192. $am =$amoun-$skid;
  193. global $woocommerce;
  194.  
  195. add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
  196. add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
  197. function custom_cart_total() {
  198.  
  199. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  200. return;
  201. global $am;
  202. WC()->cart->total = $am;
  203. }
  204. echo $am;
  205. die();
  206. }elseif($days > 14){
  207. $coupon = 10;
  208. $coupon = $coupon/100;
  209. $amoun = $amoun*$days;
  210. $skid = $amoun*$coupon;
  211.  
  212. $am =$amoun-$skid;
  213. global $woocommerce;
  214.  
  215. add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
  216. add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
  217. function custom_cart_total() {
  218.  
  219. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  220. return;
  221. global $am;
  222. WC()->cart->total = $am;
  223. //var_dump( WC()->cart->total);
  224. }
  225. echo $am;
  226. die();
  227. }else{
  228. echo (int)$days*$amoun;
  229. die();
  230. }
  231.  
  232.  
  233.  
  234. //print_r( $_POST);
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement