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

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 only products in built-in WordPress search results:

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

To allow only one item from the product to be bought in a single order, when low stock threshold:

add_filter( 'woocommerce_is_sold_individually', 'condless_wc_remove_quantity_fields', 10, 2 );
function condless_wc_remove_quantity_fields( $return, $product ) {
	if ( $product->get_stock_quantity() <= $product->get_low_stock_amount() )
  		return true;
}

To set up minimum payment for purchase with shipping, write in the “Cost” field (replace $min_order with your value):

$min_order - [fee percent="100" max_fee="$min_order"]

Popup creator. For example notifying the customer in product page about discount ineligibility since purchase not enough items from it, add the Auto Open trigger and the product id page and the custom conditions (update the product id and the quantity for discount):

add_filter( 'pum_get_conditions', 'condless_pum_cart_product_quantity_conditions' );
function condless_pum_cart_product_quantity_conditions( $conditions ) {
        return array_merge( $conditions, array(
                'password_page_unlocked' => array(
                        'group'    => __( 'Products' ),
                        'name'     => __( 'Products: Cart Quantity' ),
                        'callback' => 'cart_product_quantity',
                ),
        ) );
}
function cart_product_quantity() {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( $cart_item['product_id'] == 772 && $cart_item['quantity'] < 3 ) {
                                return true;
                }
        }
        return false;
}

Conditional shipping methods.

Booking and Appointments.

Wholesale.

Products personalization.

Recover abandoned carts.

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.

Request for quote payment option.

Advanced pricing.

Products filter.

Crowdfunding.

Bulk edit for variable products.

Multi vendor.

Selling digital media access (video courses, images, music, content), create virtual product for each media file with direct link to it / to the page on which it is embedded (use Vimeo Plus for heavy videos), restrict the media files / relevant pages that only those who purchased the compatible product on the WooCommerce may view it, enable content protection, limit connection to only one device, and activate 2FA.

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.