Adding “Accept Terms” Checkbox to WooCommerce Registration Form

a clientof mine requested this in order to protect her shop from the legal side so i found this code which will display a checkbox like this


add_action( 'woocommerce_register_form', 'razztech_add_registration_privacy_policy', 11 );

function razztech_add_registration_privacy_policy() {

woocommerce_form_field( 'privacy_policy_reg', array(
'type'          => 'checkbox',
'class'         => array('form-row privacy'),
'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required'      => true,
'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
));

}

// Show error if user does not tick

add_filter( 'woocommerce_registration_errors', 'razztech_validate_privacy_registration', 10, 3 );

function razztech_validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
$errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
}
}
return $errors;
}

Advertisements

change all orders to complete

i just got a shop with 40k orders that needed to be changed to completed

function auto_update_orders_status_from_processing_to_completed(){
// Get all current “processing” customer orders
$processing_orders = wc_get_orders( $args = array(
‘numberposts’ => -1,
‘post_status’ => ‘wc-pending’, //u can change it to wc-on-hold or whatever order status there is now

) );
if(!empty($processing_orders))
foreach($processing_orders as $order)
$order->update_status( ‘completed’ );
}
add_action( ‘init’, ‘auto_update_orders_status_from_processing_to_completed’ );

this code could kill the resources of the server so i finsd another way to do it using phpmyadmin

in the posts table just search and replace for wc-pending into wc-completed in the post_status

Woocommerce hide price range

Many clients requests to hide the price range in the product page when the product have variations in order to do this use this code

 

function wc_varb_price_range( $wcv_price, $product ) {

$prefix = sprintf(‘%s: ‘, __(‘From’, ‘wcvp_range’));

$wcv_reg_min_price = $product->get_variation_regular_price( ‘min’, true );
$wcv_min_sale_price = $product->get_variation_sale_price( ‘min’, true );
$wcv_max_price = $product->get_variation_price( ‘max’, true );
$wcv_min_price = $product->get_variation_price( ‘min’, true );

$wcv_price = ( $wcv_min_sale_price == $wcv_reg_min_price ) ?
wc_price( $wcv_reg_min_price ) :
‘<del>’ . wc_price( $wcv_reg_min_price ) . ‘</del>’ . ‘<ins>’ . wc_price( $wcv_min_sale_price ) . ‘</ins>’;

return ( $wcv_min_price == $wcv_max_price ) ?
$wcv_price :
sprintf(‘%s%s’, $prefix, $wcv_price);
}

add_filter( ‘woocommerce_variable_sale_price_html’, ‘wc_varb_price_range’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘wc_varb_price_range’, 10, 2 );

 

add new tab to my account in woocommerce

i wanted to add wishlist tab to the account page in woocommerce in the the theme enfold  i was using the yith plugin yith-woocommerce-wishlist  so i need to show the title with link to the content that will show the shortcode here what i did

first add to function.php in the child theme this code


/*
* add option to edit elements via css class
*/
// add_theme_support('avia_template_builder_custom_css');

/**
* Insert the new endpoint into the My Account menu.
*
* @param array $items
* @return array
*/
function my_custom_my_account_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );

// Insert your custom endpoint.
$items['my-custom-endpoint'] = __( 'המעודפים שלי', 'woocommerce' );

// Insert back the logout item.
$items['customer-logout'] = $logout;

return $items;
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );

/**
* Register new endpoint to use inside My Account page.
*
* @see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_custom_endpoints' );

/**
* Add new query var.
*
* @param array $vars
* @return array
*/
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';

return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

/**
* Endpoint HTML content.
*/
function my_custom_endpoint_content() {
echo do_shortcode('[yith_wcwl_wishlist]');
}

add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );

 

so with the function my_custom_endpoint_content i can display what i want in the my account tab content and i did used the shortcode that comes with the plugin

woocommerce show/hide flat rate based on product quantity

After long search for a good plugin couldn’t find anything so i created this snippets to help me in this subject

//Show/Hide FLATE RATE Shipping Based on Item Count for Domestic Shipment

add_filter('woocommerce_package_rates', 'show_hide_flat_rate_shipping', 10, 2);
function show_hide_flat_rate_shipping($available_shipping_methods, $package){
    $minimum_number_of_item = 10; //Give here minumum number of items to allow freeshipping
    

    $flat_rate  =   true;
    global $woocommerce;
    

    $item_count = 0;
    foreach (WC()->cart->cart_contents as $key => $item) {
        $item_count += $item['quantity'];
    }

    if( $item_count >= $minimum_number_of_item ){ //if products is greater or equal to the minumum number in my case 10 
        $flat_rate  =   false;
    }

    if($flat_rate){ //if flat_rate is true
     //do nothing show all shipping methods
    }
    else{ //if flat_rate is false
        foreach($available_shipping_methods as $shipping_method =>  $method){
            if( strpos( $shipping_method, 'flat_rate' ) !== false ) {
                unset($available_shipping_methods[$shipping_method]);
            }
        }
    }
	
    return $available_shipping_methods;
	
}

Make the WooCommerce Terms & Conditions checkbox checked by default

this code is working on woocommerce 3.x and it is test on
woocommerce 3.1.0 with WordPress 4.8

just go to function.php file on your theme and add this code


function razztech_wc_terms( $terms_is_checked ) { return true;}
add_filter( 'woocommerce_terms_is_checked', 'razztech_wc_terms', 10 );
add_filter( 'woocommerce_terms_is_checked_default', 'razztech_wc_terms', 10 );

and now it should check it by default