Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Call datepicker functionality in your custom text field
- add_action('woocommerce_after_order_notes', 'ncydesign_end_datepicker_field', 10, 1);
- function ncydesign_end_datepicker_field( $checkout ) {
- date_default_timezone_set('America/Los_Angeles');
- $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
- echo '<div id="my_custom_checkout_field">
- <h3>'.__('Delivery Date').'</h3>';
- $today = date("Y-m-d");
- echo '<div id="start_date_checkout_field"><input type="date" min="'.$today.'" onchange="changeamount(this.value)" name="calendarstart">';
- // YOUR SCRIPT HERE BELOW
- echo '
- <script>
- jQuery(function($){
- $("#datepickerend").datepicker();
- });
- </script>';
- /* woocommerce_form_field( 'delivery_date_end', array(
- 'type' => 'text',
- 'class' => array('my-field-class form-row-wide'),
- 'id' => 'datepickerend',
- 'required' => false,
- 'label' => __('Delivery Date'),
- 'placeholder' => __('Select Date'),
- 'options' => $mydateoptions
- ),
- $checkout->get_value( 'cylinder_collet_date' ));*/
- echo '</div>';
- }
- /**
- * Process the checkout
- **/
- add_action('woocommerce_checkout_process', 'my_custom_checkout_field_end_process');
- function my_custom_checkout_field_end_process() {
- global $woocommerce;
- // Check if set, if its not set add an error.
- if (!$_POST['delivery_date_end'])
- wc_add_notice( '<strong>Delivery Date</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
- }
- /**
- * Update the order meta with custom fields values
- * */
- add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_end_order_meta');
- function my_custom_checkout_field_update_end_order_meta($order_id) {
- if (!empty($_POST['delivery_date_end'])) {
- update_post_meta($order_id, 'End Date', sanitize_text_field($_POST['delivery_date_end']));
- }
- }
- add_action( 'wp_enqueue_scripts', 'enabling_start_date_picker' );
- function enabling_start_date_picker() {
- // Only on front-end and checkout page
- if( is_admin() || ! is_checkout() ) return;
- // Load the datepicker jQuery-ui plugin script
- wp_enqueue_script( 'jquery-ui-datepicker' );
- }
- // Call datepicker functionality in your custom text field
- add_action('woocommerce_after_order_notes', 'ncydesign_start_datepicker_field', 10, 1);
- function ncydesign_start_datepicker_field( $checkout ) {
- date_default_timezone_set('America/Los_Angeles');
- $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));
- echo '<div id="start_date_checkout_field">
- <h3>'.__('start Date').'</h3>';
- global $woocommerce;
- //print_r($_COOKIE);
- $amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
- // YOUR SCRIPT HERE BELOW
- //print_r($woocommerce->cart);
- echo $amount;
- ?>
- <input type="date" onchange="changeamount(this.value)" name="calendarend">
- <script>
- jQuery(function($){
- $("#datepickerstart").datepicker();
- });
- function changeamount($thiss){
- var amouns = '<?php echo $amount ?>';
- var s = jQuery( "input[name=calendarstart]" ).val();
- var e = jQuery( "input[name=calendarend]" ).val();
- if(s){
- if(e){
- jQuery.ajax({
- url: "<?php echo admin_url('admin-ajax.php') ?>",
- type: "POST",
- data: "action=cats&d1="+s+"&d2="+e+"&amount="+amouns, // можно также передать в виде массива или объекта
- success: function( data ) {
- //alert(data);
- //woocommerce-Price-currencySymbol
- var cur = jQuery( ".woocommerce-Price-currencySymbol:eq(1)" ).text();
- //alert(data);
- jQuery( ".woocommerce-Price-amount:eq(1)" ).text(cur+data);
- jQuery( ".woocommerce-Price-amount:eq(2)" ).text(cur+data);
- }
- });
- }
- }
- }
- </script>
- <?php
- add_action('woocommerce_checkout_process', 'my_custom_checkout_start_field_process');
- function my_custom_checkout_start_field_process() {
- global $woocommerce;
- // Check if set, if its not set add an error.
- if (!$_POST['delivery_date_start'])
- wc_add_notice( '<strong>Delivery Date</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
- }
- /**
- * Update the order meta with custom fields values
- * */
- add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
- function my_custom_checkout_field_update_order_meta($order_id) {
- if (!empty($_POST['delivery_date_start'])) {
- update_post_meta($order_id, 'start Date', sanitize_text_field($_POST['delivery_date_start']));
- }
- }
- add_action( 'wp_ajax_cats', 'sets_days_amount' );
- add_action( 'wp_ajax_nopriv_cats', 'sets_days_amount' );
- function sets_days_amount(){
- // Первая дата
- $d1 = $_POST['d1'];
- // Вторая дата
- $d2 = $_POST['d2'];
- $amoun = $_POST['amount'];
- // timestamp первой даты
- $d1_ts = strtotime($d1);
- // timestamp второй даты
- $d2_ts = strtotime($d2);
- // Количество секунд
- // Функция abs нужна, чтобы не проверять какая из двух дат больше
- $seconds = abs($d1_ts - $d2_ts);
- // Количество дней нужно округлить в меньшую сторону,
- // чтобы узнать точное количество прошедших дней
- // 86400 - количество секунд в 1 дней (60 * 60 * 24)
- $days = floor($seconds / 86400);
- //return $days;//3
- $daysCoupon = 2;
- $coupon = 10;
- $coupon = $coupon/100;
- $datetimeFrom = DateTime::createFromFormat( 'Y-m-d', $d1 );
- $datetimeTo = DateTime::createFromFormat( 'Y-m-d', $d2 );
- $days = $datetimeFrom->diff( $datetimeTo )->format( '%a' ); # 366 days
- if((int)$days >= 8 AND (int)$days <= 14){
- $coupon = 5;
- $coupon = $coupon/100;
- $amoun = $amoun*$days;
- $skid = $amoun*$coupon;
- $am =$amoun-$skid;
- global $woocommerce;
- add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
- add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
- function custom_cart_total() {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- global $am;
- WC()->cart->total = $am;
- }
- echo $am;
- die();
- }elseif($days > 14){
- $coupon = 10;
- $coupon = $coupon/100;
- $amoun = $amoun*$days;
- $skid = $amoun*$coupon;
- $am =$amoun-$skid;
- global $woocommerce;
- add_action( 'woocommerce_review_order_before_order_total', 'custom_cart_total' );
- add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' );
- function custom_cart_total() {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- global $am;
- WC()->cart->total = $am;
- //var_dump( WC()->cart->total);
- }
- echo $am;
- die();
- }else{
- echo (int)$days*$amoun;
- die();
- }
- //print_r( $_POST);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement