Want to hide sale prices on your products? Here is a way to remove all product sale prices on WooCommerce sites. Add the code to your child theme functions.php file:
/**
* Savvy Disable all sales on WooCommerce Stores.
*
*/
function savvy_wc_get_sale_price( $sale_price, $product ) {
return $product->get_regular_price(); // Un-comment this to disable all sale prices
}
add_filter( 'woocommerce_product_get_sale_price', 'savvy_wc_get_sale_price', 50, 2 );
add_filter( 'woocommerce_product_get_price', 'savvy_wc_get_sale_price', 50, 2 );
I do not see a reason you would want to do this, but you can set the WordPress permalink structure via code. To do this, add the following code to your theme’s functions.php file and change the permalink structure as needed:
function set_permalink(){
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%postname%/');
}
add_action('init', 'set_permalink');
Note that mod_rewrite must be active on the server in this case as well.
More on WordPress permalink structure in this post: Choosing the best permalink structure for SEO.
Although we see less and less use of tag clouds on WordPress sites, here is how to add one using code in WordPress templates. This is the same tag cloud that appears in the widgets area of the WordPress admin.
Add the following code where you want in your template and change the parameters as needed:
<?php wp_tag_cloud(array(
'smallest' => 10, // size of least used tag
'largest' => 18, // size of most used tag
'unit' => 'px', // unit for sizing
'orderby' => 'name', // alphabetical
'order' => 'ASC', // starting at A
'exclude' => 6 // ID of tag to exclude from list
));
?>
For more on tags and taxonomies, see Taxonomies in WordPress.
To do this, add the following code to your functions.php file and change the CSS as needed. If you want to know the right way to add assets (CSS and JS files) to the WordPress admin, take a look at the post Loading scripts correctly in the WordPress admin.
function my_custom_fonts() {
echo '<style>
body {
font-size: 12px;
}
</style>';
}
add_action('admin_head', 'my_custom_fonts');
If you want to display the post featured image in the RSS feed (essential if you use RSS for Mailchimp for example), add the following code to your functions.php file:
// Put post thumbnails into rss feed
function savvy_feed_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'savvy_feed_post_thumbnail');
add_filter('the_content_feed', 'savvy_feed_post_thumbnail');
The following filter lets you remove the WordPress admin toolbar. Add the code to your functions.php file:
add_filter('show_admin_bar', '__return_false');
This uses a WordPress filter. Learn more in WordPress Hooks Explained.
Here is an example of how to change the default PayPal icon on the WooCommerce checkout page. Live example:
<?php
/**
* Add custom gateway icons
*
* @param string $icon Image HTML.
* @param string $gateway_id Gateway ID.
*
* @return string
*/
function custom_wc_gateway_icons( $icon, $gateway_id ) {
// Example for PayPal:
if ( 'paypal' == $gateway_id ) {
$icon = '<img src="' . WC_HTTPS::force_https_url( 'http://your-site.com/image.png' ) . '" alt="' . __( 'PayPal' ) . '" />'
}
return $icon;
}
add_filter( 'woocommerce_gateway_icon', 'custom_wc_gateway_icons', 10, 2 );
For a complete WooCommerce setup guide, see Create a Virtual Store with WooCommerce.
To change the number of items on the WooCommerce orders page (on the front end), use the following code where X is the maximum number of items to display (functions.php):
function custom_my_account_orders( $args ) {
$args['posts_per_page'] = X;
return $args;
}
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
More on managing and handling orders in WooCommerce sites in the post Managing orders in WooCommerce.
By default, there is an automatic redirect from the WooCommerce checkout page to the (empty) cart page when there are no products in the cart.
Try accessing the checkout page (link in footer) at this sample store and you will see the redirect.
There are uncommon situations where you may want to disable this redirect and show the user the checkout page anyway. You can do this by adding the following code to your functions.php file:
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
Yoast automatically adds schema according to the settings you configured in the plugin. You can disable or remove this code using one of the following filters (depending on version).
For Yoast versions below 11.0, remove the schema with this filter:
function savvy_remove_yoast_json($data){
$data = array();
return $data;
}
add_filter('wpseo_json_ld_output', 'savvy_remove_yoast_json', 10, 1);
For Yoast versions 11.0 and above, use this filter:
add_filter( 'wpseo_json_ld_output', '__return_false' );