Code Snippets

wc_clean – WooCommerce Core Function

December 4, 2021 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Trims and strips tags from a string ($var). wc_clean( $var )

Read More →

Disable the default stylesheet in WooCommerce

December 1, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Disable all stylesheets WooCommerce enqueues 3 […]

Read More →

Change the default state and country on the checkout in WooCommerce

November 25, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Change the default state […]

Read More →

Add a custom currency symbol in WooCommerce

November 4, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Custom currency and currency […]

Read More →

Retrieving a customer in WooCommerce

October 13, 2021 in Code Snippets by

$customer = new WC_Customer( $user_id ); $email = $customer->get_email(); $address = $customer->get_billing_address(); $name = $customer->get_first_name() . ' ' . $customer->get_last_name();

Read More →

Exclude products from a particular category on the shop page in WooCommerce

October 5, 2021 in Code Snippets by

As ‘clothing’ is an example in the snippet below, be sure to use a product category slug that exists in your WooCommerce store. Note that this will only work when you have your “Shop Page Display” option set to ‘Show Products’ under your Customizers’ WooCommerce > Product catalog settings. Add this code to your child […]

Read More →

WooCommerce payment gateway plugin base

September 30, 2021 in Code Snippets by

This code can be used as a base to create your own simple custom payment gateway for WooCommerce. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php […]

Read More →

Adding a Custom Special Field in WooCommerce

September 4, 2021 in Code Snippets by

Let’s add a new field to checkout, after the order notes, by hooking into the following. /** * Add the field to the checkout */ add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' ); function my_custom_checkout_field( $checkout ) { echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>'; woocommerce_form_field( 'my_field_name', array( 'type' => 'text', 'class' => array('my-field-class form-row-wide'), 'label' => __('Fill […]

Read More →

Make phone number not required in WooCommerce

July 7, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 ); […]

Read More →

Show product categories in WooCommerce breadcrumbs

June 5, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Show product categories in […]

Read More →

Declaring WooCommerce Support

May 9, 2021 in Code Snippets by

function mytheme_add_woocommerce_support() { add_theme_support( 'woocommerce' ); } add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );

Read More →

Hide loop read more buttons for out of stock items in WooCommerce

May 3, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Hide loop read more […]

Read More →

wc_get_weight – WooCommerce Core Function

May 2, 2021 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Takes a weight ($weight) weighed in WooCommerce’s weight unit and converts it to the target weight unit ($to_unit). wc_get_weight( 10, 'lbs' )

Read More →

Customize the WooCommerce breadcrumb

April 23, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Useful if you want to change […]

Read More →

Register the taxonomy for menus in WooCommerce

April 7, 2021 in Code Snippets by

When registering taxonomies for your custom attributes, WooCommerce calls the following hook: $show_in_nav_menus = apply_filters('woocommerce_attribute_show_in_nav_menus', false, $name); So, for example, if your attribute slug was ‘size’ you would do the following to register it for menus: add_filter('woocommerce_attribute_show_in_nav_menus', 'wc_reg_for_menus', 1, 2); function wc_reg_for_menus( $register, $name = '' ) { if ( $name == 'pa_size' ) $register […]

Read More →

Add a cookie compliance banner in Shopify

March 27, 2021 in Code Snippets by

Cookie HTML banner saved as snippet in snippets/cookie-banner.liquid : <style> #cookies-banner { display: none; justify-content: center; align-items: center; padding: 1em; position: fixed; bottom: 0px; width: 100%; background: #fff; border-top: 1px solid #dcdcdc; } </style> <div id="cookies-banner"> <span>This website uses cookies to ensure you get the best experience on our website.</span> <button style="margin-left: 1em;" onclick="handleDecline()">Decline</button> <button […]

Read More →

Check if both the billing and the shipping address contain a house number in WooCommerce

March 13, 2021 in Code Snippets by

Currently, when using the Checkout block from the WooCommerce Blocks plugin, there’s no check if the billing or shipping address field contains a house number. The following code snippets allow to check if these fields contain a number. To check if both the billing and the shipping address contain a house number, please use the […]

Read More →

Unhook the WooCommerce wrappers

March 11, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10); remove_action( 'woocommerce_after_main_content', […]

Read More →

Example products loop in WooCommerce

March 5, 2021 in Code Snippets by

<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12 ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul><!–/.products–>

Read More →

Display product filter in Shopify

February 19, 2021 in Code Snippets by

Display product filter: <form class="filter-form"> {%- for filter in collection.filters -%} <details class="filter-group"> <summary class="filter-group-summary"> <div> <span>{{ filter.label }}</span> {%- if filter.active_values.size > 0 -%} <span>({{ filter.active_values.size }})</span> {%- endif -%} </div> </summary> <div class="filter-group-display"> <div class="filter-group-display__header"> <span class="filter-group-display__header-selected">{{ filter.active_values.size }} selected</span> {%- if filter.active_values.size > 0 -%} <a href="{{ filter.url_to_remove }}" class="filter-group-display__header-reset">Reset</a> {%- endif […]

Read More →

Query whether WooCommerce is activated

February 17, 2021 in Code Snippets by

If you’re building a theme which supports but doesn’t require WooCommerce, you may want to wrap WooCommerce functionality (think cart links etc) inside a conditional query. That way, if WooCommerce isn’t activated, the functionality is simply ignored instead of producing fatal errors. Add this code to your child theme’s functions.php file or via a plugin […]

Read More →

Minimum Order Amount in WooCommerce

January 15, 2021 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Set a minimum order […]

Read More →

Allow shortcodes in product excerpts in WooCommerce

December 28, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Allow shortcodes in product […]

Read More →

Using the New Settings in WooCommerce

December 27, 2020 in Code Snippets by

You would now just use your newly created settings like you would any other WordPress / WooCommerce setting, through the get_option function and the defined ID of the setting. For example, to use the wcslider_auto_insert option from previous snippet, simply use the following code. get_option( 'wcslider_auto_insert' );

Read More →

Send coupons used in an order by email in WooCommerce

December 5, 2020 in Code Snippets by

This snippet sends by email the list of coupons used in an order. You can customize the $to variable and define your own email address, and the $message to have your own text. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as […]

Read More →

Change Limit on Number of Variations for Dynamic Variable Product Dropdowns in WooCommerce

October 5, 2020 in Code Snippets by

By default, if a Variable product has fewer than 30 variations, the dropdowns for selecting variations on the frontend will be dynamic. For example, with a T-shirt that has “Size” and “Color” attributes, after the customer selects a Size, the Color dropdown will be updated via AJAX to only display options that are available with […]

Read More →

JavaScript submission of the localization form in Shopify

September 25, 2020 in Code Snippets by

Because your country or language selector is a custom element and there’s no submit button included in the form, you need to submit the form through JavaScript. class LocalizationForm extends HTMLElement { constructor() { super(); this.elements = { input: this.querySelector('input[name="language_code"], input[name="country_code"]'), button: this.querySelector('button'), panel: this.querySelector('ul'), }; this.elements.button.addEventListener('click', this.openSelector.bind(this)); this.elements.button.addEventListener('focusout', this.closeSelector.bind(this)); this.addEventListener('keyup', this.onContainerKeyUp.bind(this)); this.querySelectorAll('a').forEach(item => item.addEventListener('click', […]

Read More →

Add your own stylesheet in WooCommerce

September 2, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Enqueue your own stylesheet like this: […]

Read More →

Display product collection in Shopify

August 28, 2020 in Code Snippets by

Here is how to display a product collection in a template. <ul> {% for product in collections.all.products %} <li><a href="{{ product.url}}">{{ product.title}}</a> {% endfor %} </ul> Save it as collection-product-list.liquid.

Read More →

How to Add Settings to a Section in WooCommerce

August 18, 2020 in Code Snippets by

You need to filter the output of woocommerce_get_sections_products (or similar). For example, let’s add the sample settings we discussed above to the new wcslider section we just created. /** * Add settings to the specific section we created before */ add_filter( 'woocommerce_get_settings_products', 'wcslider_all_settings', 10, 2 ); function wcslider_all_settings( $settings, $current_section ) { /** * Check […]

Read More →

Override loop template and show quantities next to add to cart buttons in WooCommerce

August 16, 2020 in Code Snippets by

To display quantity input fields for simple products within your shop archive pages, use the following code. /** * Override loop template and show quantities next to add to cart buttons */ add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 ); function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) { if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() […]

Read More →

Automatically add product to cart on visit in WooCommerce

August 9, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Automatically add product to […]

Read More →

Apply a coupon for minimum cart total in WooCommerce

July 31, 2020 in Code Snippets by

The code snippet below allows you to: Show a notice on the cart and checkout page, reminding customers that they get a discount if spending more than a minimum amount. Automatically apply a discount and show a notice that the discount was applied when the cart total is more than a minimum amount. Add this […]

Read More →

Add a new country to countries list in WooCommerce

July 30, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Add a new country […]

Read More →

List products in Shopify

July 20, 2020 in Code Snippets by

<ul> {% for product in collection-product-list limit: limit_count %} <li><a href="{{ product.url }}">{{ product.title }}</a> {% endfor %} </ul>

Read More →

Removing Tabs in WooCommerce

July 17, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Use the following snippet to remove […]

Read More →

Change number of products per row in WooCommerce

June 28, 2020 in Code Snippets by

Defines where ‘first’ and ‘last’ classes are applied in product archives. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely […]

Read More →

Add checkout validation styles to a custom theme in WooCommerce

June 15, 2020 in Code Snippets by

You need to add this CSS to your child theme’s style.css file or to your customizers’ “Additional CSS” section. Please don’t add custom code directly to your parent theme’s style.css file as this will be wiped entirely when you update the theme. .form-row.woocommerce-invalid .chzn-single, .form-row.woocommerce-invalid .chzn-drop, .form-row.woocommerce-invalid input.input-text, .form-row.woocommerce-invalid select { border:1px solid red; } […]

Read More →

Hide sub-category product count in WooCommerce product archives

June 10, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Hide category product count […]

Read More →

Using action hooks in WooCommerce

June 1, 2020 in Code Snippets by

To execute your own code, you hook in by using the action hook do_action(‘action_name’);. Here is where to place your code: add_action( 'action_name', 'your_function_name' ); function your_function_name() { // Your code }

Read More →

Remove product content based on category in WooCommerce

May 20, 2020 in Code Snippets by

The following code will remove product images on single product pages within the ‘Cookware’ category. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this […]

Read More →

Updating an existing coupon in WooCommerce

April 22, 2020 in Code Snippets by

$coupon = new WC_Coupon( $coupon_id ); $coupon->set_discount_type( 'percent' ); $coupon->set_amount( 25.00 ); $coupon->save();

Read More →

Display unit price in Shopify

March 20, 2020 in Code Snippets by

Display unit price for line item: {% if line_item.unit_price_measurement %} {{ line_item.unit_price | money }} {% if line_item.unit_price_measurement.reference_value != 1 %} {{ line_item.unit_price_measurement.reference_value }} {% endif %} {{ line_item.unit_price_measurement.reference_unit }} {% endif %} Display unit price for variant: {% if variant.unit_price_measurement %} {{ variant.unit_price | money }} {% if variant.unit_price_measurement.reference_value != 1 %} {{ variant.unit_price_measurement.reference_value […]

Read More →

Display product attribute archive links in WooCommerce

March 18, 2020 in Code Snippets by

Display WooCommerce product attribute archive links on the product page, right below the add to cart button. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file […]

Read More →

Check if the shipping address contains a house number in WooCommerce

March 12, 2020 in Code Snippets by

Currently, when using the Checkout block from the WooCommerce Blocks plugin, there’s no check if the billing or shipping address field contains a house number. The following code snippets allow to check if these fields contain a number. To check if the shipping address contains a house number, please use the following code snippet: add_action( […]

Read More →

Customizing checkout fields using actions and filters in WooCommerce

March 11, 2020 in Code Snippets by

Let us change the placeholder on the order_comments fields. We can change this by adding a function to our theme functions.php file: // Hook in add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); // Our hooked in function – $fields is passed via the filter! function custom_override_checkout_fields( $fields ) { $fields['order']['order_comments']['placeholder'] = 'My new placeholder'; return $fields; } […]

Read More →

Add a custom tab in WooCommerce

March 6, 2020 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Use the following snippet to add […]

Read More →

Change number of products displayed per page in WooCommerce

February 28, 2020 in Code Snippets by

Use the following code to change the number of WooCommerce products displayed per page. This requires you to have your shop page display, which can be found in customizer view under WooCommerce > Product catalog set to “Show products”. Add this code to your child theme’s functions.php file or via a plugin that allows custom […]

Read More →

Add a contact form in Shopify

February 22, 2020 in Code Snippets by

Add a contact form to your theme to allow customers to get in touch with the merchant. {% form 'contact' %} {{ form.errors | default_errors }} <div class="first-name"> <label for="first-name">First name</label> <input type="text" name="contact[first_name]" id="first-name" /> </div> <div class="last-name"> <label for="last-name">Last name</label> <input type="text" name="contact[last_name]" id="last-name" /> </div> <div class="phone"> <label for="phone">Phone</label> <input type="tel" name="contact[phone]" […]

Read More →

Creating a new simple product in WooCommerce

February 1, 2020 in Code Snippets by

$product = new WC_Product_Simple(); $product->set_name( 'My Product' ); $product->set_slug( 'myproduct' ); $product->set_description( 'A new simple product' ); $product->set_regular_price( '9.50' ); $product->save(); $product_id = $product->get_id();

Read More →

Change number of related products output in WooCommerce

January 11, 2020 in Code Snippets by

Please note that it may not work for all themes because of the way they are coded. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file […]

Read More →

Set a custom add to cart URL to redirect to in WooCommerce

December 23, 2019 in Code Snippets by

Read More →

Change number of upsells output in WooCommerce

December 19, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Change number of upsells […]

Read More →

Add reCAPTCHA in Shopify

December 2, 2019 in Code Snippets by

The necessary code for the reCAPTCHA functionality is included through the content_for_header object. This means that if a merchant has reCAPTCHA enabled, but the content_for_header object is not present, then the reCAPTCHA functionality won’t be present. The reCAPTCHA functionality is initialized based on the presence of customer, contact, and blog comment forms, and is triggered […]

Read More →

Hide trailing zeros on prices in WooCommerce

November 5, 2019 in Code Snippets by

The option to show trailing zeros on your prices has been removed in favor of a filter, as of WooCommerce 2.2x. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your […]

Read More →

Add a standard currency value surcharge to all transactions in WooCommerce

November 3, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Add a standard $ […]

Read More →

Change email subject lines in WooCommerce

July 29, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /* * goes in theme functions.php […]

Read More →

Notify Admin When New Account Is Created in WooCommerce

July 24, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Notify admin when a […]

Read More →

Check if WooCommerce is active

June 21, 2019 in Code Snippets by

Most WooCommerce plugins do not need to run unless WooCommerce is already active. You can wrap your plugin in a check to see if WooCommerce is installed: // Test to see if WooCommerce is active (including network activated). $plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php'; if ( in_array( $plugin_path, wp_get_active_and_valid_plugins() ) || in_array( $plugin_path, wp_get_active_network_plugins() […]

Read More →

Display Different Content for Different Categories in WooCommerce

June 20, 2019 in Code Snippets by

Display different content for different categories. if ( is_product_category() ) { if ( is_product_category( 'shirts' ) ) { echo 'Hi! Take a look at our sweet tshirts below.'; } elseif ( is_product_category( 'games' ) ) { echo 'Hi! Hungry for some gaming?'; } else { echo 'Hi! Check our our products below.'; } }

Read More →

Add menu in Shopify

June 20, 2019 in Code Snippets by

To add menu to your theme, you can use the linklists object to find the menu that you want to output. The default menu is the Main menu, which can be accessed with it’s handle main-menu. {% for link in linklists.main-menu.links %} <!– menu content –> {% endfor %} And here is how to display […]

Read More →

The Additional Information tab in WooCommerce

June 16, 2019 in Code Snippets by

Please note that the “Additional Information” tab will only show if the product has weight, dimensions or attributes (with “Visible on the product page” checked). If you try to apply a change to that tab and if the product does not have weight, dimensions or attribute, you will get an error message. In that case, […]

Read More →

Enable the product gallery in WooCommerce theme

June 12, 2019 in Code Snippets by

To enable the product gallery in your theme, you can declare support like this. add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-slider' );

Read More →

Change a currency symbol in WooCommerce

June 1, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Change a currency symbol […]

Read More →

Automatically Complete Orders in WooCommerce

April 8, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Auto Complete all WooCommerce […]

Read More →

wc_price – WooCommerce Core Function

March 28, 2019 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Formats a passed price with the correct number of decimals and currency symbol. wc_price( $price, $args = array() )

Read More →

Make WooCommerce price widget draggable on touch devices

March 2, 2019 in Code Snippets by

Download this script and save it to the JS folder in your theme directory. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will […]

Read More →

Add/Modify States in WooCommerce

February 13, 2019 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Don’t forget to add your own […]

Read More →

Add a message above the login / register form in WooCommerce

January 18, 2019 in Code Snippets by

Please note that for this code to work you’ll need to have the “Account creation” settings for customers checked, in “Accounts & Privacy”. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly […]

Read More →

Add product media in Shopify

January 16, 2019 in Code Snippets by

In Shopify merchants can add media to their products, like images, 3D models, videos, and YouTube or Vimeo videos. To display product media on the product page, and your product page content is hosted in a product.liquid section, then you might do the following: Create a snippet called media.liquid to host your media display. Render […]

Read More →

Changing image sizes via hooks in WooCommerce

November 24, 2018 in Code Snippets by

If you need more control over thumbnail sizes here is a hook available to you. add_filter( 'woocommerce_get_image_size_gallery_thumbnail', function( $size ) { return array( 'width' => 150, 'height' => 150, 'crop' => 0, ); } );

Read More →

Add a custom field (in an order) to the emails in WooCommerce

October 26, 2018 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Add a custom field […]

Read More →

How to Disable Gutenberg

October 25, 2018 in Blog, Code Snippets by

gutenberg-is-coming

For those wast majority that is still not ready to convert all sites and content to Gutenberg here is one snippet that you could add to your theme’s functions.php file that will disable Gutenberg for you.

Read More →

Prevent Data Leaks in WooCommerce

October 17, 2018 in Code Snippets by

Try to prevent direct access data leaks. Add this line of code after the opening PHP tag in each PHP file. if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly }

Read More →

Display product weight on archive pages in WooCommerce

October 10, 2018 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. WooCommerce 3.0+ /** * Show product […]

Read More →

Display product dimensions on archive pages in WooCommerce

October 4, 2018 in Code Snippets by

Display WooCommerce product dimensions on archive pages, below the title of the product. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be […]

Read More →

Add country selector in Shopify

September 4, 2018 in Code Snippets by

Add button and a popover containing each country option. {% if localization.available_countries.size > 1 %} <localization-form> {% form 'localization' %} <div class="disclosure"> <button type="button" class="disclosure__button" aria-expanded="false" aria-controls="CountryList"> {{ localization.country.name }} ({{ localization.country.currency.iso_code }} {{ localization.country.currency.symbol }}) <svg aria-hidden="true" focusable="false" role="presentation" class="icon icon-caret" viewBox="0 0 10 6"> <path fill-rule="evenodd" clip-rule="evenodd" d="M9.354.646a.5.5 0 00-.708 0L5 4.293 1.354.646a.5.5 […]

Read More →

Optional WooCommerce Theme Settings

August 3, 2018 in Code Snippets by

These are optional theme settings that you can set when declaring WooCommerce support. function mytheme_add_woocommerce_support() { add_theme_support( 'woocommerce', array( 'thumbnail_image_width' => 150, 'single_image_width' => 300, 'product_grid' => array( 'default_rows' => 3, 'min_rows' => 2, 'max_rows' => 8, 'default_columns' => 4, 'min_columns' => 2, 'max_columns' => 5, ), ) ); } add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );

Read More →

Display My Account link in a template file in WooCommerce

July 12, 2018 in Code Snippets by

Displays a link to the user account section. If the user is not logged in the link will display ‘Login / Register’ and take the use to the login / signup page. If the user is logged in the link will display ‘My account’ and take them directly to their account. <?php if ( is_user_logged_in() […]

Read More →

Show A Page Only To Logged-In Users – WordPress

July 5, 2018 in Code Snippets by

Below is a simple WordPress code that will show a page only to logged-in users. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this […]

Read More →

Hook in your functions to display the wrappers in your WooCommerce theme

June 27, 2018 in Code Snippets by

Hook in your own functions to display the wrappers your theme requires. Make sure that the markup matches that of your theme. If you’re unsure of which classes or IDs to use, take a look at your theme’s page.php for guidance. add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10); add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10); function my_theme_wrapper_start() { echo '<section id="main">'; } function […]

Read More →

Display price and discounts in Shopify

June 3, 2018 in Code Snippets by

Display the price and discounts: {% for item in cart.items %} <!– item info –> {% if item.original_price > item.final_price %} <s>{{ item.original_price | money }}</s> {% endif %} {{ item.final_price | money }}) {% if item.line_level_discount_allocations.size > 0 %} Discounts: <ul> {% for discount_allocation in item.line_level_discount_allocations %} <li> {{ discount_allocation.discount_application.title }}-{{ discount_allocation.amount | money […]

Read More →

Add security badge to your theme in Shopify

May 26, 2018 in Code Snippets by

<a href="https://www.shopify.com/security/pci-compliant?utm_medium=shop&utm_source=secure" title="This online store is secured by Shopify" target="_blank" rel="nofollow" > <img src="https://cdn.shopify.com/s/images/badges/shopify-secure-badge-white.svg" alt="Shopify secure badge" > </a>

Read More →

Hide other shipping methods when “Free Shipping” is available in WooCommerce

April 30, 2018 in Code Snippets by

Hides everything but free_shipping if it’s available and is compatible with Shipping Zones. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be […]

Read More →

Adjust the quantity input values in WooCommerce

March 1, 2018 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Set the starting value, maximum value, […]

Read More →

wc_get_page_id – WooCommerce Core Function

February 19, 2018 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Gets a WooCommerce page ID by name, e.g. thankyou wc_get_page_id( $page )

Read More →

Customize robots.txt in Shopify

January 24, 2018 in Code Snippets by

Shopify generates a default robots.txt file that works for most stores. However, if you want to make changes to the default file, then you can add the robots.txt.liquid template in order to make the following customizations: Add a new rule to an existing group Remove a rule from an existing group Add custom rules The […]

Read More →

Custom tracking code for the thank-you page in WooCommerce

January 8, 2018 in Code Snippets by

This snippet is for adding tracking for a separate piece of analytic software you may have installed. The order object contains all the order data you should need, including line items. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky […]

Read More →

Display Category Image on Category Archive in WooCommerce

January 6, 2018 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Display category image on […]

Read More →

Unhook/remove WooCommerce Emails

January 2, 2018 in Code Snippets by

This code allows you to unhook and remove the default WooCommerce emails. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten […]

Read More →

Change add to cart button text in WooCommerce

December 31, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. // Change add to cart text […]

Read More →

Display search results filter in Shopify

December 26, 2017 in Code Snippets by

Display search results filter: <form class="filter-form"> <input type="hidden" name="q" value="{{ search.terms }}"> {%- for filter in search.filters -%} <details class="filter-group"> <summary class="filter-group-summary"> <div> <span>{{ filter.label }}</span> {%- if filter.active_values.size > 0 -%} <span>({{ filter.active_values.size }})</span> {%- endif -%} </div> </summary> <div class="filter-group-display"> <div class="filter-group-display__header"> <span class="filter-group-display__header-selected">{{ filter.active_values.size }} selected</span> {%- if filter.active_values.size > 0 -%} […]

Read More →

Disable the product gallery in WooCommerce theme

December 11, 2017 in Code Snippets by

You can disable product gallery features with the code below. remove_theme_support( 'wc-product-gallery-zoom' ); remove_theme_support( 'wc-product-gallery-lightbox' ); remove_theme_support( 'wc-product-gallery-slider' );

Read More →

Show product recommendations in Shopify

December 6, 2017 in Code Snippets by

In this snippet, the section content builds the general display by looping through each product returned through the products attribute of the recommendations object. Add this code to sections/product-recommendations.liquid <div class="product-recommendations" data-url="{{ routes.product_recommendations_url }}?section_id={{ section.id }}&product_id={{ product.id }}&limit=4" > {%- if recommendations.performed and recommendations.products_count > 0 -%} <h2>You may also like</h2> <ul> {%- for product […]

Read More →

Allow HTML in term (category, tag) descriptions in WooCommerce

December 2, 2017 in Code Snippets by

By default, WordPress strips HTML from category descriptions. This code will prevent that from happening. The first part prevents HTML from being stripped from term descriptions. The second part prevents HTML being stripped out when using the term description function. Add this code to your child theme’s functions.php file or via a plugin that allows […]

Read More →

Add tag filtering in Shopify

November 11, 2017 in Code Snippets by

To implement a tag filtering feature, you can loop through all of the product tags in the collection using the all_tags attribute of the collection object. {% if collection.all_tags.size > 0 %} <ul class="tag-filters"> {% for tag in collection.all_tags %} {% if current_tags contains tag %} <li class="tag-filters__item active">{{ tag | link_to_remove_tag: tag }}</li> {% […]

Read More →

Add hreflang tags in Shopify

October 27, 2017 in Code Snippets by

An hreflang tag is a link element that identifies a localized URL of a website. You should add a unique hreflang tag for each language or region URL that exists, and they should be included in the , which is commonly found in theme.liquid. <head> <!– head element content –> <link rel="alternate" hreflang="en" href="your-store.myshopify.com" /> […]

Read More →

Add SEO meta in Shopify

October 23, 2017 in Code Snippets by

Add SEO metadata in your theme using HTML and Liquid. <head> <title> {{ page_title -}} {%- if current_tags %} &ndash; tagged "{{ current_tags | join: ', ' }}"{% endif -%} {%- if current_page != 1 %} &ndash; Page {{ current_page }}{% endif -%} {%- unless page_title contains shop.name %} &ndash; {{ shop.name }}{% endunless -%} […]

Read More →

wc_get_dimension – WooCommerce Core Function

October 16, 2017 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Takes a measurement (dim) measured in WooCommerce’s dimension unit and converts it to the target unit ($to_unit). wc_get_dimension( 10, 'lbs' )

Read More →

wc_mail – WooCommerce Core Function

October 10, 2017 in Code Snippets by

The following useful function is found in the includes/wc-core-functions.php and can be used by themes and plugins. Used to send an email using the WooCommerce email templates. wc_mail( $to, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" )

Read More →

Display single product collection in Shopify

October 8, 2017 in Code Snippets by

Here is how to display a single product collection in a template. <ul> {% for product in collection-product-list %} <li><a href="{{ product.url}}">{{ product.title}}</a> {% endfor %} </ul>

Read More →

Check if the billing address contains a house number in WooCommerce

September 29, 2017 in Code Snippets by

Currently, when using the Checkout block from the WooCommerce Blocks plugin, there’s no check if the billing or shipping address field contains a house number. The following code snippets allow to check if these fields contain a number. To check if the billing address contains a house number, please use the following code snippet: add_action( […]

Read More →

Add language selector in Shopify

September 17, 2017 in Code Snippets by

Language selector allow customers to manually choose their preferred language. Code below adds button and a popover containing each language option: {% if localization.available_languages.size > 1 %} <localization-form> {% form 'localization' %} <div class="disclosure"> <button type="button" class="disclosure__button" aria-expanded="false" aria-controls="LanguageList"> {{ localization.language.endonym_name | capitalize }} <svg aria-hidden="true" focusable="false" role="presentation" class="icon icon-caret" viewBox="0 0 10 6"> <path […]

Read More →

Add a surcharge based on the delivery country in WooCommerce

September 9, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Add a 1% surcharge […]

Read More →

Define theme image sizes in WooCommerce

September 7, 2017 in Code Snippets by

add_theme_support( 'woocommerce', array( 'thumbnail_image_width' => 200, 'gallery_thumbnail_image_width' => 100, 'single_image_width' => 500, ) );

Read More →

Add UK postal counties to WooCommerce

August 29, 2017 in Code Snippets by

This snippet adds UK postal counties to WooCommerce’s list of states. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely […]

Read More →

Change the placeholder image in WooCommerce

August 20, 2017 in Code Snippets by

The placeholder image can now be set at: WooCommerce > Settings > Products as of version 3.5x. WooCommerce 3.4x or earlier Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your […]

Read More →

Add a percentage based surcharge to all transactions in WooCommerce

July 13, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Add a 1% surcharge […]

Read More →

Using filter hooks in WooCommerce

May 29, 2017 in Code Snippets by

Filter hooks are called throughout are code using apply_filter( ‘filter_name’, $variable );. To manipulate the passed variable, you can do something like the following. add_filter( 'filter_name', 'your_function_name' ); function your_function_name( $variable ) { // Your code return $variable; } With filters, you must return a value.

Read More →

Do not allow PO BOX shipping in WooCommerce

May 21, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Prevent PO box shipping […]

Read More →

Custom sorting options (asc/desc) in WooCommerce

May 16, 2017 in Code Snippets by

This example code shows how you can add an order option for ‘random’, but this also works in similar ways for other ways to order your products on the catalog page. Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky […]

Read More →

Display cart discounts in Shopify

May 13, 2017 in Code Snippets by

Display between the subtotal and total if a discount applies to the cart as a whole. Subtotal: {{ cart.items_subtotal_price | money }} {% if cart.cart_level_discount_application.size > 0 %} Discounts: <ul> {% for discount_application in cart.cart_level_discount_applications %} <li> {{ discount_application.title }}-{{ discount_application.total_allocated_amount | money }} </li> {% endfor %} </ul> {% endif %} Total: {{ cart.total_price […]

Read More →

Show cart contents / total in WooCommerce

May 13, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. To display the cart contents and […]

Read More →

Allowing customer access to WP Admin and enabling the admin bar in WooCommerce

May 5, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. By default, WooCommerce blocks non-admin users […]

Read More →

Customize a tab in WooCommerce

April 27, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. The following snippet will replace the […]

Read More →

Adding Custom Fields To Emails in WooCommerce

April 20, 2017 in Code Snippets by

To add a custom field value to WooCommerce emails use the following snippet. /* To use: 1. Add this snippet to your theme's functions.php file 2. Change the meta key names in the snippet 3. Create a custom field in the order post – e.g. key = "Tracking Code" value = abcdefg 4. When next […]

Read More →

Re-ordering Tabs in WooCommerce

February 20, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Use the following snippet to change […]

Read More →

Disabling WooCommerce styles

January 22, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. add_filter( 'woocommerce_enqueue_styles', '__return_false' );

Read More →

Rename a country in WooCommerce

January 15, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. /** * Rename a country */ […]

Read More →

Renaming Tabs in WooCommerce

January 10, 2017 in Code Snippets by

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Dessky Snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be overwritten completely when you update the theme. Use the following snippet to rename […]

Read More →

How to Create a Section in WooCommerce

January 3, 2017 in Code Snippets by

We’ll go over doing this through individual functions, but you should probably create a Class that stores all of your settings methods. The first thing you need to is add the section, which can be done like this by hooking into the woocommerce_get_sections_products filter: /** * Create the section beneath the products tab **/ add_filter( […]

Read More →

WooCommerce – Autocomplete all Orders

December 8, 2015 in Code Snippets by

What if you just want to automatically complete all of the orders that come in? The following code snippet will do just that!

Read More →

WooCommerce – “Ship to a Different Address” area always visible

December 7, 2015 in Code Snippets by

If you want people to see the shipping fields by default, you want that section ALWAYS visible. Here’s how you do it.

Read More →

WooCommerce – Add an Editable Text at the top of Cart page

December 7, 2015 in Code Snippets by

Sometimes client will ask you to promptly add some text at the top of cart page and they will have to change this text often.

Read More →

WooCommerce – Change the link of the “Continue Shopping” button

December 7, 2015 in Code Snippets by

Below is a quick snippet on how to change link on the “Continue Shopping” button in cart. Hope you will find it helpful.

Read More →

Remove menu items in wp-admin depending on user role

December 7, 2015 in Code Snippets by

You will usually find one or more menu items that you don’t want to show to your guest posters. How do you hide them?

Read More →

WooCommerce – Automatically set order status to completed after payment is received

December 7, 2015 in Code Snippets by

Sometimes you’ll need to automatically check the payment status and mark an order as completed.

Read More →

Remove The URL Field From WordPress Comments

May 5, 2014 in Code Snippets by

Spammers tend to use URL field to post links to their spammy websites. Just to be safe, you may want to remove that URL field altogether.

Read More →

Remove the Product Count on Categories in WooCommerce

May 5, 2014 in Code Snippets by

This snippet will remove the product count functionality site-wide.

Read More →

Automatically Complete Orders in WooCommerce

May 5, 2014 in Code Snippets by

Auto Complete all WooCommerce orders.

Read More →

Change the Billing Address Label on the WooCommerce Checkout Page

May 5, 2014 in Code Snippets by

woocommerce-shipping-billing-address-label

This code snippet lets you change the main label for either the Billing Address section or Shipping Address section.

Read More →

Disable WooCommerce Scripts and Styles

September 18, 2013 in Blog, Code Snippets by

Here’s how to disable all the extra scripts and style-sheets WooCommerce loads throughout your web site, and restricts them to just loading in your store area.

Read More →

Get group details from group_id – BuddyPress Snippet

June 20, 2013 in Code Snippets by

This is a very simple thing to do but it often takes a lots of time to find. Here is how to get group details if you have a group id.

Read More →

Display number of items in cart together with total – WooCommerce

June 9, 2013 in Blog, Code Snippets by

How to display number of items in cart together with total in WooCommerce.

Read More →

Get List of Coupons used by one Customer in all WooCommerce orders

May 3, 2013 in Blog, Code Snippets by

The beauty of code snippets is their ability to save you time when developing a site. If you are a WooCommerce developer you may find this snippet usefull for Coupons listing.

Read More →
trudeau@mailxu.com