woo.php 10.2 KB
<?php 

add_filter( 'woocommerce_account_menu_items', 'misha_rename_downloads' );

function misha_rename_downloads( $menu_links ){
	
	// $menu_links[ 'TAB ID HERE' ] = 'NEW TAB NAME HERE';
	$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);


function create_and_attach_woocommerce_product( $course_id ) {
    if ( get_post_type($course_id) == 'sfwd-courses') {

        error_log('create_and_attach_woocommerce_product');
        // Set the product data
        $product_data = array(
        'post_title'  => get_the_title( $course_id ),
        'post_type'   => 'product',
        'post_status' => 'publish'
        );

        // Insert the product
        $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);
        // Set the prices
        update_post_meta( $product_id, '_regular_price',  $price );
        update_post_meta( $product_id, '_price',  $price );
        update_post_meta( $product_id, '_role_based_price',$sp );
              
    }

}


 add_action( 'wp_after_insert_post', 'my_wp_after_insert_post', 10, 4 );

function my_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
   if ( 'publish' !== $post->post_status || ( $post_before && 'publish' === $post_before->post_status ) || wp_is_post_revision( $post_id )) {
        if ( get_post_type($post_id) == 'sfwd-courses') {
            error_log('woocommerce_learndash_product');
            learndash_update_setting($post_id, 'sfwd-courses_course_price_type', 'closed' );
            $product_id = get_post_meta($post_id, '_woocommerce_product',true);
            learndash_update_setting($post_id, 'sfwd-courses_certificate', 426 );    
            if($product_id !=""){  
                $courses_custom_button_url = learndash_update_setting($post_id, 'sfwd-courses_custom_button_url', get_site_url().'/cart/?add-to-cart='.$product_id );
            }
            $price = get_post_meta($post_id, 'program_info_cost_&_dates_0_cost',true ); 
            if($price !=""){  
            learndash_update_setting($post_id, 'sfwd-courses_course_price',   $price  );
            }
            $staff_price = get_post_meta($post_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' );