Restrict Shipping To Specific States in WooCommerce

This code enables you to exclude states from the drop down menu on the checkout page for any country in WooCommerce. You can :

  • Exclude shipping to specific states in America
  • Restrict shipping to only 1 or more states in Australia
  • Remove states from the billing and shipping drop down menus for any other country

In this example, all U.S states are removed except California and Pennsylvania.

Remove U.S States from WC Shipping

In this example, only 3 states from Australia can be selected.

Remove Australian States from WooCommerce Shipping drop down menu

Installation

Copy & paste the PHP code to the end of your child themes functions file or custom functionality plugin.

2 code snippets are included for the United Stated and Australia. For use with other countries, swapout the country and state codes in the PHP code.

Categories
Free Tutorials Genesis Tutorials

Style First Nav Menu Item Using CSS

You can use CSS to style the 1st nav menu item in any theme using :

  • first-child
  • nth-child(1)
  • first-of-type

All 3 CSS rules enable you to style the 1st nav menu link for the primary navigation in Genesis powered child themes.

The first rule uses the first-child selector class like this :

.nav-primary .genesis-nav-menu > li:first-child > a {
    color: red;
}

You could also use nth-child like this :

.nav-primary .genesis-nav-menu > li:nth-child(1) > a {
    color: red;
}

Or use first-of-type like this :

.nav-primary .genesis-nav-menu > li:first-of-type > a {
    color: red;
}

All 3 pseudo-classes enable you to use CSS to target the first element among a group of child elements. In this case, the 1st element of a list of nav menu items.

In WordPress, you could also add a custom class to the 1st menu item and use that class as your selector in a CSS rule in your child themes style sheet.

Related Solutions

Only Allow 1 Coupon Code On Cart & Checkout WooCommerce

This PHP code enables you to programmatically remove the coupon code field once the first coupon code is entered & applied. Customers cannot enter another coupon as the coupon box is removed at the same time as the 1st coupon is applied as seen in the demo video.

Note : You can use the WooCommerce Marketing > Coupons > Coupon Data > Usage Restriction > Individual use only setting to do the same thing however this code enables you to integrate usage with WooCommerce extensions and other conditions using PHP.

Installation

There’s only 1 step :

Copy & paste the PHP code to the end of your child themes functions file or custom functionality plugin.

Categories
Genesis Tutorials

Genesis One Page Block Theme

This child theme for Genesis extends the Genesis Sample theme as a 1 page theme using Gutenberg blocks. The one page includes 3 sections after the main hero section which can be linked to from the menu and scrolled to on menu item click. Footer widgets are also included.

Genesis One Page Theme

Demo Video

Shows you how to install and setup the one page theme for Genesis and custom nav menu links for the scroll points.

Installation

Install the theme like any other Genesis child theme and click the setup link as seen in the demo video. You can then add custom links to the primary ( Header ) nav menu as seen in the following screenshot.

You may also need to swap out the image url on line 21 in config > import > content > one-page.php.

Download Theme

Categories
Genesis Tutorials

Reposition Image Before Title When Mobile Responsive

This coded solution for the Authority Pro child theme by StudioPress, enables you to move the home page image before the title and text on mobile screens rather than the default which shows the image after the content.

CSS image before title text mobiles

The code enables you to change the position of any element when in different columns in any container using CSS.

Demo Video

Shows the home page image in the first section moved to display before the intro text on mobiles.

Tested using the Authority Pro child theme by StudioPress however will work in any theme by changing the selector classes to match for the container and columns 1 and 2.

Installation

There’s only 1 step :

Copy and paste the CSS to the end of your Authority Pro child themes style.css file and clear caching.

Download Folder

Categories
Genesis Tutorials WordPress Tutorials

Post Tags By Current Category

This code enables you to display post tags by the current category when viewed on each category archive page. It displays a tag cloud of post tags assigned to posts within the current category archive.

The code loops through all posts assigned to each category and displays tags assigned to each post on the category archives as seen in the demo video.

Demo Video

Shows tags for posts on the current category archive page.

Tested using the Genesis Sample child theme by StudioPress & the Twenty Twenty default theme for WordPress.

Parameters

You can display your tags using the flat or list format and use any of the following parameters to customize the output.

WP Tag Cloud Parameters

Installation – Genesis

There’s only 1 step.

Upload the file named category.php to your child theme folder.

Installation – Other Themes

There’s 2 steps relating to usage of the code from the folder named tag-cloud:

Step 1 : Copy & paste the PHP code from functions.php to the end of your child themes functions file.

Step 2 : Paste the following template tag in your category.php template in your child theme. Included is a category.php template for the Twenty Twenty WordPress theme.

tag_cloud_by_current_category();

And here’s what you get on the Twenty Twenty category page :

Download Folder

Related Tutorials

Conditionally Show Hide Custom WooCommerce Checkout Fields Based on Cart Weight

Both code snippets in this download folder enable you to show a specific custom field based on the total weight of all products in the cart on the WooCommerce checkout page.

  • By default, the code is written to show a custom field named Up To 500 kg when the weight is 500 kg or less. Other fields will not display.
  • When the weight is over 500 kg or less than 1001 kg a field named Over 500kg & Less Than 1001kg displays and all other fields are hidden.
  • When the total weight in the cart is more than 1000 kg, a field named Over 1000 kg is shown and all other fields are hidden.

The custom field value is output on the WooCommerce > Orders > Edit Order screen like this under the shipping details :

Installation

Copy & paste the PHP code from the functions.php or functions 2.php file to the end of your child themes functions file or custom functionality plugin.

Categories
Free Tutorials WordPress Tutorials

Custom Button Link Per Product in WooCommerce

This PHP code enables you to add a custom button on your single product page with a custom link. You can add the link using WordPress custom fields or a custom field plugin like ACF.

  • Key used for WP custom fields is wp_button.
  • For ACF, use acf_button.

If ACF not active, WP custom fields will be used.

The code enables you to open the link in the same window or new window and add the nofollow attribute to the link on a per product basis.

Use this file to import the ACF settings.

Works in any WordPress theme running WooCommerce.

Set Out of Stock Threshold Per Product WooCommerce

This code enables you to set a threshold for each product so the product displays out of stock if below your threshold or in stock if above the threshold.

Example :

In the demo video, we set a threshold of 100 for the product named Less Than 100. If the inventory goes below 100, the product displays as Out of Stock. The product named 50 In Stock only displays in stock if there’s 50 of more in stock, otherwise it displays out of stock.

You can add your threshold on any Edit product screen > Product Data > Simple Product > Inventory > Out of Stock Threshold :

Installation

There’s only 1 step : Copy & paste the PHP code ( without the opening PHP tag ) to the end of your child themes functions file or custom functionality plugin. You can then set the threshold on any Edit product screen using the Inventory tab.

Note : The download file also contains PHP code ( commented out ) which enables you to show stock levels offset by the threshold.

Categories

Change The Text of The Add To Cart Button On Specific WooCommerce Product Page

Swap out the I.D for the product you want to target in the code and add to the end of your child themes functions.php file.

You could also write the code like this :

Categories

Automatically Add The Same Quantity of a Addon Product To Match The Quantity of Another Product

This code adds a product to the cart when another product is added and matches the quantity. Example :

  • If 1 product named A is added to cart, another (addon ) product named B will be added.
  • If 5 products named A are added to cart, 5 ( addon ) products named B will also be added.

The addon can be a free product or a paid product, it doesn’t matter.

Demo Video

Shows a product quantity of 5 demo products added to the cart which auto adds 5 addon products based on the product I.D added to the input field for the main product.

Tested using the Twenty Twenty default theme for WordPress however will work in any WordPress theme running WooCommerce.

Installation

Copy & paste the PHP code to the end of your child themes functions file or custom functionality plugin.

You can then grab the product I.D for your addon product to the input field on the edit product screen as seen in the demo video.

Categories
Free Tutorials WordPress Tutorials

Remove Single Product Image Per Product

Add this PHP code to the end of your child themes functions file to remove a product image for 1 specific product only. Swap out the post I.D to match your single products post i.d.

add_action( 'woocommerce_before_single_product_summary', 'single_product_remove_image', 1 );
function single_product_remove_image() {
if ( is_single( '193' ) ) {
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
}
}

To remove product images for more than 1 single product, add an array to the is_single conditional tag like this

add_action( 'woocommerce_before_single_product_summary', 'single_product_remove_images', 1 );
function single_product_remove_images() {
if ( is_single(array( '192','193' ) ) ) {
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
}
}

You can also use the is_product conditional tag in replace of is_single like this :

is_product('193')

And like this :

if ( is_product() AND get_the_ID() == 193 ) {

However, you should also check if WooCommerce is active like this using class_exists

if ( class_exists( 'woocommerce' ) AND is_product() AND get_the_ID() == 193 ) {
Categories
Genesis Tutorials

Image Vertically Centred Inline With Text

This code enables you to display a text title on the left and a image on the right which is vertically aligned center with your text. On Mobiles, the text displays before the image with the image centered below the text.

Video Demo

Shows the image vertically centered on desktops and underneath the text on mobiles.

Tested using the Breakthrough Pro child theme by StudioPress with a .webp image 2000 pixels in width.

Installation

There’s 3 steps for installation :

1. Here’s the HTML which goes in Customize > Breakthrough Pro Settings > Hero Section Headings > Hero Title field.

2. Upload your image to your WordPress media library and swap out the image link in the above HTML from step 1. In this example, i used a .webp image.

3. Copy & paste the CSS from the download folder to the end of your Breakthrough Pro themes style-front.css or style.css file and clear caching.

Download Folder

Categories
WordPress Tutorials

Open WC Mini Cart Links In New Window

This code modifies the default links in the WC mini cart so they open in a new window.

Demo Video

Shows the ‘View Cart’ link, once clicked, open in a new window.

Coming Soon.

Tested using the Twenty Twenty theme for WordPress however will work in any WordPress theme.

Installation

There’s only 1 step :

Step 1 : Upload the cart folder to your child themes woocommerce folder. If your theme doesn’t include a woocommerce folder, create one and add the cart folder inside your woocommerce folder.

Download Folder

Related Code

Categories
Genesis Tutorials WordPress Tutorials

Open WooCommerce View Cart Button Link In New Window

The code in this download folder, when clicked, opens the “View Cart” link on the WooCommerce single product page, in a new tab.

Demo Video

Shows the ‘View Cart’ link, once clicked, open in a new window.

Coming Soon.

Tested using the Twenty Twenty theme for WordPress however will work in any WordPress theme.

Installation

There’s 2 steps :

Step 1 : Copy & paste the PHP code ( without the opening PHP tag ) to the end of your child themes functions.php file.

Step 2 : Upload the file named view_cart_blank.js to your child theme folder.

You can swap out the selector classes in the jQuery to make the code work with any link or button in any WordPress theme.

Download Folder

Related Code

Categories
Free Tutorials WordPress Tutorials

How To Redesign The Single Product Page Template In WooCommerce

This is a beginners guide showing you 2 ways to modify the default design of your single product pages when using WooCommerce.

One : You can upload the following files from your wooocommerce plugin folder to a woocommerce folder in your child theme :

single-product.php
content-single-product.php
single-product-reviews.php

And also upload the single-product folder so you can modify the code in any of the template files to redesign different elements on your single product pages.

Your woocommerce folder inside your child theme would look like this :

It shows the 3 template files and 1 folder you can use to modify the default output in order to change the design of your single product page.

Two : You can use add_action, remove_action and add_filter hooks.

Action Hooks

Here’s an example using remove_action and add_action hooks in your child themes functions.php file to reposition the product title before the product image :

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_template_single_title' )

The code removes the title from its default position and then adds it back in a different hook position. View all single product page hooks here

Filter Hooks

Here’s an example of how to use a add_filter hook to add a custom image which replaces the main image on the single product page.

Categories
WordPress Tutorials

Add Multiple Authors To WooCommerce Products

This code enables you to add more than 1 author to WooCommerce products using the Co Authors Plus plugin.

Works in any WordPress theme. Tested using the Genesis sample child theme by StudioPress.

Installation

Requires the installation of the free Co Authors Plus plugin for WordPress.

  1. Copy & paste all the PHP code, ( excluding the opening PHP tag ), to the end of your child themes functions file or custom functionality plugin.

  2. Resave settings > permalinks.

  3. Copy & paste the CSS to the end of your style.css file and clear caching.

Download Folder

Related Solutions

Add Co Authors to Woocommerce Products

This code enables you to add 2 authors to WooCommerce products like this :

Works in any WordPress theme.

Installation

  1. Copy & paste all the PHP code, ( excluding the opening PHP tag ), to the end of your child themes functions file or custom functionality plugin.

  2. Resave settings > permalinks.

Note : If not using Genesis, don’t use the code for the genesis_product_author_shortcode function. There are 2 shortcode functions. 1 for genesis and 1 for non Genesis themes.

Adding 2nd Author

Use WordPress custom fields or any custom field plugin like ACF to add your 2nd author name. Custom field name is co_author.

Related Products

Add Author to Woocommerce Products

This code enables you to add authors to WooCommerce products. The code also creates a product authors archive for each author which is separate to the standard author archives for posts.

Works in any WordPress theme.

Installation

  1. Copy & paste all the PHP code, ( excluding the opening PHP tag ), to the end of your child themes functions file or custom functionality plugin.

  2. Resave settings > permalinks.

Note : If not using Genesis, don’t use the code for the genesis_product_author_shortcode function. There are 2 shortcode functions. 1 for genesis and 1 for non Genesis themes.

Custom Product Name For WooCommerce Receipts

This code enables you to change the default Name of any WooCommerce Product download so your custom product title is displayed on :

  • Order emails
  • Credit Card Statements
  • PayPal transactions and emails
  • WooCommerce My Account > Downloads > product name
  • WooCommerce My Account > Orders > product name

This way the name you use on your product title will be different to whats used once your order is complete.

Payment Gateways Email Notifications

This is how your custom product name will appear on payment gateway emails like PayPal:

You can test this using PayPal Sandbox https://developer.paypal.com/dashboard/notifications and viewing the Receipt for Your Payment to your test buyer under Sandbox email notifications.

Installation

Step 1 : Copy & paste the PHP code from the functions file to the end of your child themes functions file or custom functionality plugin.

Step 2 : Upload the folder named woocommerce to your child theme folder.

You can then add your custom product name on any single edit product screen in the Product data > General > Order Title field.