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 );

 

WordPress YouTube native responsive

The native YouTube url when you embed into wordpress content is not responsive so we need to add a code to this iframe or embed

so we need to add to functions.php this code

what this code do is to search for the embed code of youtube and add to it

in order to change it with css


add_filter('embed_oembed_html', 'wrap_embed_with_div', 10, 3);

function wrap_embed_with_div($html, $url, $attr) {
        return "<div class=\"responsive-container\">".$html."</div>";
}

and this css code to your css file or theme settings css


.responsive-container {
        position: relative;
        padding-bottom: 50.25%;
        padding-top: 30px;
        height: 0;
        overflow: hidden;
        margin-bottom: 1em;
}
.responsive-container iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
}


hope i helped someone in this world 🙂

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

fixing data-filter to support hebrew

a friend of mine bought this plugin ” Final Tiles WordPress Gallery”

https://codecanyon.net/item/final-tiles-wordpress-gallery/5189351

there is a data-filter problem to show hebrew names as it dosen’t convert it as english with the – instead of space

so to fix it we eed to go to final-tiles-gallery/lib file name “gallery-class.php”

on line 232: instead of



static public function slugify($text)
{
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
if(function_exists("iconv"))
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);

if (empty($text))
{
return 'n-a';
}

return $text;
}


replace it with



static public function slugify($text)
{
$text = trim($text, '-');
$text = strtolower($text);
$text = preg_replace('/[[:space:]]+/', '-', $text);

if (empty($text))
{
return 'n-a';
}

return $text;
}


and