Skip to content

Instantly share code, notes, and snippets.

@bradleysa
bradleysa / gist:7a984fb4fe3f04942a2ee264177fed37
Created April 22, 2022 08:26
WC: Change 'add to cart' text on single product page
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_add_to_cart_text');
function woocommerce_custom_add_to_cart_text() {
return __("That's My Jam!", 'woocommerce');
}
/** https://avada.io/woocommerce/docs/change-add-to-cart-button-text.html **/
add_action('template_redirect', 'my_custom_disable_author_page');
function my_custom_disable_author_page() {
global $wp_query;
if ( is_author() ) {
// Redirect to homepage, set status to 301 permenant redirect.
// Function defaults to 302 temporary redirect.
wp_redirect(get_option('home'), 301);
exit;
@bradleysa
bradleysa / gist:1ae9fb7af399faa7df9a1a7bd08f9faa
Created April 22, 2022 08:25
Read More Link with Excerpt
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() ) {
$output = sprintf( '%1$s <p class="read-more-container"><a class="ast-button" href="%2$s">Read more</a></p>',
$excerpt,
get_permalink()
);
}
@bradleysa
bradleysa / gist:6d49582676af5f43239b9ca1acfbea8b
Created April 22, 2022 08:24
WC: Trim zeros in price decimals
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
/** https://krokedil.com/dont-display-prices-with-0-decimals-in-woocommerce/ **/
@bradleysa
bradleysa / gist:f72bd558d5e8e7ffe5213a396991ad0f
Created April 22, 2022 08:24
WC: Change 'add to cart' text on single product category
add_filter( 'woocommerce_product_add_to_cart_text', 'product_cat_add_to_cart_button_text', 20, 1 );
function product_cat_add_to_cart_button_text( $text ) {
// Only for a specific product category
if( has_term( array('gift-pack'), 'product_cat' ) )
$text = __( 'Yes! The Perfect Gift!', 'woocommerce' );
return $text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_cat_single_add_to_cart_button_text', 20, 1 );
@bradleysa
bradleysa / gist:d49394dc5c450fe76e376d1a94ec28da
Created April 22, 2022 08:22
WC: Show Free Shipping and Local Pickup Plus
/**
* Hide shipping rates when free shipping is available, but keep "Local Pickup Plus"
* Updated to support WooCommerce 2.6 Shipping Zones
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
add_filter('get_the_archive_title', function ($title) {
return preg_replace('/^\w+: /', '', $title);
});
/** https://wordpress.stackexchange.com/questions/175884/how-to-customize-the-archive-title **/
@bradleysa
bradleysa / gist:d03b571fd2e64d3ceb8296fd3e330e87
Created April 22, 2022 08:21
Jetpack: Remove Feature Hints
add_filter( 'jetpack_psh_active', '__return_false' );
/** https://jetpack.com/support/feature-hints/ **/
@bradleysa
bradleysa / gist:f41cf8c6e8dc7534d276b84f28b1564f
Created April 22, 2022 08:20
WC: Slashed Cart Subtotal if Coupon Applied
add_filter( 'woocommerce_cart_subtotal', 'bbloomer_slash_cart_subtotal_if_discount', 99, 3 );
function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $obj ){
global $woocommerce;
if ( $woocommerce->cart->get_cart_discount_total() <> 0 ) {
$new_cart_subtotal = wc_price( WC()->cart->subtotal - $woocommerce->cart->get_cart_discount_tax_total() - $woocommerce->cart->get_cart_discount_total() );
$cart_subtotal = sprintf( '<del>%s</del> <b>%s</b>', $cart_subtotal , $new_cart_subtotal );
}
return $cart_subtotal;
}
@bradleysa
bradleysa / gist:fad71d9b129614a404e3a6dcc44134b9
Created April 22, 2022 08:19
WC: Change Text "You may be interested in…" & "Related Products"
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'You may be interested in&hellip;':