Categories
WordPress

WooCommerce

And Back to the Future.

WooCommerce is an open source E-commerce software which comes as a plugin for WordPress, it is the most popular way of creating E-commerce websites. From many clearing options, order management, shipping types to inventory settings, the plugin covers all the requirements that may arise in creating E-commerce websites.

To edit the mobile menu button text in Storefront theme:

add_filter( 'storefront_menu_toggle_text', 'condless_storefront_menu_toggle_text' );
function condless_storefront_menu_toggle_text( $text ) {
	$text = __( 'My menu' );
	return $text;
}

To edit the Update cart button text in mobile:

add_filter( 'gettext', 'condless_change_update_cart_text', 20, 3 );
function condless_change_update_cart_text( $translated, $text, $domain ) {
if ( wp_is_mobile() && is_cart() && $translated == 'Update cart' ) {
$translated = 'Text for mobile';
}
return $translated;
}

Assign Free Shipping to a single product: Create a Shipping Class “Free Shipping”, assign the new Shipping Class to your Product and set up a 0 Flat Rate based on Shipping Class.

Auto change the order status from “On hold” to “Processing”, for example, to allow a customer who pay in bank transfer (which take some time) to view the digital product download link on the “Order Received” page (must refresh the page):

add_action( 'woocommerce_thankyou', 'condless_woocommerce_auto_processing_orders' );
function condless_woocommerce_auto_processing_orders( $order_id ) {
    if ( ! $order_id )
        return;
    $order = wc_get_order( $order_id );
    if( $order->has_status( 'on-hold' ) ) {
        $order->update_status( 'processing' );
    }
}

Reordering checkout fields:

add_filter( 'woocommerce_checkout_fields', 'condless_reordering_checkout_fields' );
function condless_reordering_checkout_fields( $fields ) {
    $billing_order = array(
        'billing_first_name',
        'billing_last_name',
        'billing_email',
        'billing_phone',
        'billing_company',
        'billing_address_1',
        'billing_address_2',
        'billing_postcode',
        'billing_city',
        'billing_state',
        'billing_country'
    );
    $count = 0;
    $priority = 10;
    foreach($billing_order as $field_name){
        $count++;
        $fields['billing'][$field_name]['priority'] = $count * $priority;
    }
    $fields['billing']['billing_email']['class'] = array('form-row-first');
    $fields['billing']['billing_phone']['class'] = array('form-row-last');
    $fields['billing']['billing_postcode']['class'] = array('form-row-first');
    $fields['billing']['billing_city']['class'] = array('form-row-last');
    return $fields;
}

Hiding some variations of product:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'condless_hide_dropdown_variation_args', );
function condless_hide_dropdown_variation_args( $args ) {
    $args['show_option_none'] = false;
    foreach( $args['options'] as $key => $option ){
        if( $option === "2,5 kg" ) {
            unset($args['options'][$key]);
        }
    }
    return $args;
}

Add fields validation in the checkout page. for example, to allow ASCII chars only (as well as disallow non-English letters):

add_action( 'woocommerce_after_checkout_validation', 'condless_ascii_validate_checkout', 10, 2 );
function condless_ascii_validate_checkout( $fields, $errors ){
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_first_name' ] ) )
$errors->add( 'validation', '<strong>Billing First name</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_last_name' ] ) )
$errors->add( 'validation', '<strong>Billing Last name</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_company' ] ) )
$errors->add( 'validation', '<strong>Billing Company</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_address_1' ] ) )
$errors->add( 'validation', '<strong>Billing Address</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_address_2' ] ) )
$errors->add( 'validation', '<strong>Billing Address details</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'billing_city' ] ) )
$errors->add( 'validation', '<strong>Billing City</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_first_name' ] ) )
$errors->add( 'validation', '<strong>Shipping First name</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_last_name' ] ) )
$errors->add( 'validation', '<strong>Shipping Last name</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_company' ] ) )
$errors->add( 'validation', '<strong>Shipping Company</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_address_1' ] ) )
$errors->add( 'validation', '<strong>Shipping Address</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_address_2' ] ) )
$errors->add( 'validation', '<strong>Shipping Address details</strong> contains non-English letters.' );
if ( preg_match( '/[^[:ascii:]]/', $fields[ 'shipping_city' ] ) )
$errors->add( 'validation', '<strong>Shipping City</strong> contains non-English letters.' );
}

Manipulate the product price display, for example, to display the discount on sale products:

add_filter( 'woocommerce_get_price_html', 'condless_simple_product_price_format', 10, 2 );
function condless_simple_product_price_format( $price, $product ) {
   if ( $product->is_on_sale() && $product->is_type('simple') ) {
      $price = sprintf( __( '<div class="was-now-save"><div class="was">WAS %1$s</div><div class="now">NOW %2$s</div><div class="save">SAVE %3$s</div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() )  );      
   }
   return $price;
}

Display the TAX/VAT amount and the product price with/without it:

add_filter( 'woocommerce_get_price_suffix', 'condless_price_vat_suffix', 10, 4 );
function condless_price_vat_suffix( $html, $instance, $price, $qty ) {
if ( is_product() ) {
        $html .=  ' + ' . wc_price(wc_get_price_including_tax($instance) - wc_get_price_excluding_tax($instance)) . ' VAT =  ' . wc_price(wc_get_price_including_tax($instance)); }
return $html;
}

Remove the product added/removed messages:

add_filter( 'wc_add_to_cart_message_html', '__return_null' );
add_filter( 'woocommerce_cart_item_removed_notice_type', '__return_null' );

Display product only in built-in WordPress search:

function condless_search_wc( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', 'product' );
}
return $query;
}
add_filter('pre_get_posts','condless_search_wc');

Booking and Appointments.

Wholesale.

Products personalization.

Recover abandoned carts.

Free shipping settings.

Apply coupons automatically.

Shipment tracking.

Payment methods for payment apps which don’t have API.

Fees and discounts by payment method.

Payment method by product.

Add payment option of “request for quote“.

Advanced pricing.

Products filter.

Crowdfunding.

Bulk edit for variable products.

Multi vendor.

Selling courses. If the video files are not on the site create a post for each course with the embedded video, create a virtual product for each video course with a link to the video file (if the video files on the site) / post (if the video is embedded), define in each video file (if the files The video on the site) / post (if the video is embedded) that only those who purchased the compatible product on the WooCommerce may view it.

As you probably have noticed- Jigowatt ltd, the developers of Jigoshop on which WooCommerce code is based, was named after the pronunciation mistake of the word Gigawatt in the movie Back to the Future.