woo.php 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
<?php 

add_filter( 'woocommerce_account_menu_items', 'misha_rename_downloads' );

function misha_rename_downloads( $menu_links ){

	$menu_links[ 'dashboard' ] = 'My Account';
    $menu_links[ 'edit-address' ] = 'Address';

	return $menu_links;
}



function wooc_extra_register_fields() {?>
    <p class="form-row form-row-first">
    <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
    </p>
    <p class="form-row form-row-last">
    <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
    </p>
    <div class="clear"></div>
    <?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );


function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
           $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
    }
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
           $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
    }
       return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );

/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );



// ----- validate password match on the registration page
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
	global $woocommerce;
	extract( $_POST );
	if ( strcmp( $password, $password2 ) !== 0 ) {
		return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
	}
	return $reg_errors;
}
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);

// ----- add a confirm password fields match on the registration page
function wc_register_form_password_repeat() {
	?>
	<p class="form-row form-row-wide">
		<label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
		<input type="password" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
	</p>
	<?php
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );

// ----- Validate confirm password field match to the checkout page
function lit_woocommerce_confirm_password_validation( $posted ) {
    $checkout = WC()->checkout;
    if ( ! is_user_logged_in() && ( $checkout->must_create_account || ! empty( $posted['createaccount'] ) ) ) {
        if ( strcmp( $posted['account_password'], $posted['account_confirm_password'] ) !== 0 ) {
            wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' ); 
        }
    }
}
add_action( 'woocommerce_after_checkout_validation', 'lit_woocommerce_confirm_password_validation', 10, 2 );

// ----- Add a confirm password field to the checkout page
function lit_woocommerce_confirm_password_checkout( $checkout ) {
    if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {

        $fields = $checkout->get_checkout_fields();

        $fields['account']['account_confirm_password'] = array(
            'type'              => 'password',
            'label'             => __( 'Confirm password', 'woocommerce' ),
            'required'          => true,
            'placeholder'       => _x( 'Confirm Password', 'placeholder', 'woocommerce' )
        );

        $checkout->__set( 'checkout_fields', $fields );
    }
}
add_action( 'woocommerce_checkout_init', 'lit_woocommerce_confirm_password_checkout', 10, 1 );

add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );

add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
function check_product_added_to_cart( $passed, $product_id, $quantity) {
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
        // if products are already in cart:
        if( $cart_item['data']->get_id() == $product_id ) {
            // Set the verification variable to "not passed" (false)
            $passed = false;
            return $passed;
        }
    }
    return $passed;
}


add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
function wc_remove_all_quantity_fields( $return, $product ) 
{
    return( true );
}

add_filter( 'woocommerce_cart_item_thumbnail', 'my_remove_cart_product_image', 10 );
function my_remove_cart_product_image() {
    return __return_false();
}


add_action('acf/save_post', 'create_and_attach_woocommerce_product', 1);
add_action('wp_after_insert_post', 'create_and_attach_woocommerce_product', 1);


function create_and_attach_woocommerce_product( $course_id ) {
    $post  = get_post($course_id);
    if ( !wp_is_post_autosave( $post ) && $post->post_status !== 'auto-draft' && !wp_is_post_revision( $course_id ) ) {
        if ( get_post_type($course_id) == 'sfwd-courses') {

            if(!get_post_meta( $course_id, '_woocommerce_product', true )){  
                $product_data = array(
                    'post_title'  => get_the_title( $course_id ),
                    'post_type'   => 'product',
                    'post_status' => 'publish'
                    );  
                $product_id = wp_insert_post( $product_data );
                update_post_meta( $course_id, '_woocommerce_product', $product_id );
                // Set the product type
                wp_set_object_terms( $product_id, 'course', 'product_type' );
                wp_set_object_terms( $product_id, 'uncategorized', 'product_cat' );
                // Set the related_course
                $related_course = array($course_id);
                update_post_meta( $product_id, '_related_course', $related_course);
            }else{
                $product_id = get_post_meta( $course_id, '_woocommerce_product', true );
                $product_data = array(
                    'ID' => $product_id,
                    'post_title' => get_the_title( $course_id )
                );
                $product = wp_update_post( $product_data );
            }
            learndash_update_setting($course_id, 'sfwd-courses_course_price_type', 'closed' );
            learndash_update_setting($course_id, 'sfwd-courses_certificate', 426 );    
            if($product_id !=""){  
                $courses_custom_button_url = learndash_update_setting($course_id, 'sfwd-courses_custom_button_url', get_site_url().'/cart/?add-to-cart='.$product_id );
            }
            $price = get_post_meta($course_id, 'program_info_cost_&_dates_0_cost',true ); 
            if($price !=""){  
                learndash_update_setting($course_id, 'sfwd-courses_course_price', $price );
            }
            $staff_price = get_post_meta($course_id, 'program_info_cost_&_dates_0_cost_staff',true );
            if($staff_price !=""){  
                $sp = array('staff' =>array('regular_price' => $staff_price));
                update_post_meta( $product_id, '_regular_price',  $price );
                update_post_meta( $product_id, '_price',  $price );
                update_post_meta( $product_id, '_role_based_price',$sp );
            }    
        }
    }
}



// Redirect to All posts dashboard after post update/publish
function webroomtech_redirect_to_post_list() {
    global $pagenow;
    if (( $pagenow == 'post-new.php' ) && (get_post_type() == 'sfwd-courses')) { ?>
    <script>
    // On "publish / update / submit changes" button click, redirect to cpt listing
    jQuery(document).ready(function($) {
        // Getting the post type to work for post, pages, custom post types etc.
        let postType = document.querySelector('form.metabox-base-form input#post_type').value;
        var url = '/wp-admin/edit.php?post_type=' + postType;
            setTimeout(function() {
                $('.editor-post-publish-button__button').on('click', function() {
                    setTimeout(function() {
                        $.ajax({
                            success: function() {
                                window.location.href = url;
                            }
                        });
                    }, 2000);
                });
            }, 4000);
    });
    </script>
    <?php
    }
    }

    add_action( 'admin_print_footer_scripts', 'webroomtech_redirect_to_post_list' );



    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     unset($fields['billing']['billing_company']);
     unset($fields['billing']['billing_address_1']);
     unset($fields['billing']['billing_address_2']);
     unset($fields['billing']['billing_city']);
     unset($fields['billing']['billing_postcode']);     
     unset($fields['billing']['billing_country']);

     

     return $fields;
}

/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_after_checkout_billing_form', 'my_staff_checkout_field' );

function my_staff_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>' . __('St. Joseph\'s Health Centre Guelph (SJHCG) Staff') . '</h3>';

    woocommerce_form_field( 'are_you_staff', array(
        'type'          => 'select',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Are you a staff memeber of SJHCG:'),
        'options'     => array(
            'blank'   => __( 'Select a option' ),
            'y' => __('- Yes, I Am A SJHCG Staff Member'),
            'n' => __('- No, I Am Not A SJHCG Staff Member'),
        )
        ), $checkout->get_value( 'are_you_staff' ));  

    echo '<div id="my_staff_checkout_field"> ';   
    woocommerce_form_field( 'my_job_title', array(
        'type'          => 'text',
        'class'         => array('my-staff-class form-row-wide'),
        'label'         => __('SJHCG Job Title'),
        'placeholder'   => __('What is your official job title at SJHCG?'),
        'required'  => true,
        ), $checkout->get_value( 'my_job_title' ));

    woocommerce_form_field( 'my_leaders_name', array(
        'type'          => 'text',
        'class'         => array('my-staff-class form-row-wide'),
        'label'         => __('SJHCG Leader’s Name'),
        'placeholder'   => __('Who do you report to at SJHCG?'),
        'required'  => true,
        ), $checkout->get_value( 'my_leaders_name' ));
        
    woocommerce_form_field( 'is_your_registration', array(
        'type'          => 'select',
        'class'         => array('my-staff-class form-row-wide'),
        'label'         => __('Is your Registration For This Program:'),
        'required'  => true,
        'options'     => array(
            'blank'   => __( 'Select a option' ),
            'department_leader' => __('- Paid for by my SJHCG Department/Leader'),
            'myself' => __('- I Will Be Paying For This Program Myself'),
        )
        ), $checkout->get_value( 'is_your_registration' ));   
        
        echo '<tr class="coupon-form"><td colspan="2">';
        wc_get_template(
            'checkout/form-coupon.php',

        );
        echo '</td></tr>';
        

    echo "</div></div><script>
    jQuery(document).ready(function($) {
        $('#are_you_staff').change(function () {
            var are_you_staff =  $('#are_you_staff').val();
            if(are_you_staff == 'y'){
                $('#my_staff_checkout_field').show()

            }else{
                $('#my_staff_checkout_field').hide()

            }
        });
        $('#is_your_registration').change(function () {
            var are_you_staff =  $('#is_your_registration').val();
            if(are_you_staff == 'department_leader'){
                $('#coupon-form').show()

            }else{
                $('#coupon-form').hide()

            }
        });
    });
    </script>";
}
  

add_action( 'woocommerce_after_checkout_validation', 'staff_checkout_field', 9999, 2);
function staff_checkout_field( $fields, $errors ){
    // if any validation errors
    if ( $_POST['are_you_staff'] == 'y' ) { 
        if ( empty( $_POST['my_job_title'] ) ) {
            $errors->add( 'woocommerce_password_error', __( 'Please enter a Job Title' ) );
        } 
        if ( empty( $_POST['my_leaders_name'] ) ) {
            $errors->add( 'woocommerce_password_error', __( 'Please enter a Leader’s Name' ) );
        } 
        if ( empty( $_POST['is_your_registration'] ) ) {
            $errors->add( 'woocommerce_password_error', __( 'Please selcet an option' ) );
        } 
    }
} 

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['my_job_title'] ) ) {
        update_post_meta( $order_id, 'my_job_title', sanitize_text_field( $_POST['my_job_title'] ) );
    }
    if ( ! empty( $_POST['my_leaders_name'] ) ) {
        update_post_meta( $order_id, 'my_leaders_name', sanitize_text_field( $_POST['my_leaders_name'] ) );
    }
    if ( ! empty( $_POST['is_your_registration'] ) ) {
        update_post_meta( $order_id, 'is_your_registration', sanitize_text_field( $_POST['is_your_registration'] ) );
    }       
}


// Display a custom field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_custom_meta_data_in_backend_orders', 10, 1 );
function display_custom_meta_data_in_backend_orders( $order ){
    $domain = 'woocommerce';

    $my_job_title = $order->get_meta('my_job_title');
    if( ! empty( $my_job_title ) ){
        echo '<p><strong>'.__('SJHCG Job Title', $domain).': </strong> ' . $my_job_title . '</p>';
    }
    $my_leaders_name = $order->get_meta('my_leaders_name');
    if( ! empty( $my_leaders_name ) ){
        echo '<p><strong>'.__( 'SJHCG Leader’s Name', $domain).': </strong> ' . $my_leaders_name . '</p>';
    }
    $is_your_registration = $order->get_meta('is_your_registration');
    if( ! empty( $is_your_registration ) ){
        echo '<p><strong>'.__( 'Is your Registration For This Program', $domain).': </strong> ' . $is_your_registration . '</p>';
    }
}


    /*
     * Removing default "Coupon form" from the top of the checkout page
     */
    remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

    /*
     * Hooking "Coupon form" after order total in checkout page with custom function
     */
    //add_action( 'woocommerce_review_order_after_order_total', 'woocommerce_checkout_coupon_form_custom' );

    /*
     * Rendering html for "Coupon form" with custom function
     */
    function woocommerce_checkout_coupon_form_custom() {
        echo '<tr class="coupon-form"><td colspan="2">';
        wc_get_template(
            'checkout/form-coupon.php',
            array(
                'checkout' => WC()->checkout(),
            )
        );
        echo '</td></tr>';
    }



    function ajax_apply_coupon()
 {
     $coupon_code = null;
     if (!empty($_POST['coupon_code'])) {
         $coupon_code = sanitize_key($_POST['coupon_code']);
     }
     $coupon_id = wc_get_coupon_id_by_code($coupon_code);
     if (empty($coupon_id)) {
         wc_add_notice(__('Sorry, there has been an error.', 'woocommerce'), 'error');
         wp_send_json_error(['message' => __('Sorry, there has been an error.', 'woocommerce')], 400);
     }

     if (!WC()->cart->has_discount($coupon_code)) {
         WC()->cart->add_discount($coupon_code);
     }
     wp_send_json_success(['message' => __('Coupon code applied successfully.', 'woocommerce')], 200);
 }

 add_action('wp_ajax_ajax_apply_coupon', 'ajax_apply_coupon');
 add_action('wp_ajax_nopriv_ajax_apply_coupon', 'ajax_apply_coupon');