Just a handy collection of small snippets for customising WooCommerce.
Last active
November 13, 2023 00:12
-
-
Save mishterk/f70bf9a3fd82dafae51f8faf6058f2ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_checkout_create_order_line_item', function ( WC_Order_Item_Product $item, $item_key, $values ) { | |
$item->add_meta_data( 'Some key', 'Some value' ); | |
$item->add_meta_data( 'some_other_key', 'Some other value' ); | |
// $values will contain additional item data passed when adding item to cart. e.g; | |
// WC()->cart->add_to_cart( $product_id, 1, 0, [], ['key1' => 'value1', 'key2' => 'value2']] ); | |
if ( ! empty( $values['key1'] ) ) { | |
$item->add_meta_data( 'Key 1', $values['key1'] ); | |
} | |
}, 10, 3 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('gettext', 'wolfd_change_product_text', 20, 3); | |
add_filter('ngettext', 'wolfd_change_product_text', 20, 3); | |
function wolfd_change_product_text($translated_text, $text, $domain) { | |
if ('woocommerce' === $domain && 'Product' === $text) { | |
$translated_text = 'Item'; | |
} | |
return $translated_text; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('woocommerce_return_to_shop_text', function() { | |
return 'Your Custom Text Here'; // Replace with your desired button text | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Disable the link in order item lists | |
add_filter('woocommerce_order_item_permalink', function ( $permalink, $item, $order ) { | |
return ''; | |
}, 10, 3); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wc_get_product( wc_get_product_id_by_sku( 'sku-here' ) ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('rewrite_rules_array', function ($rules) { | |
$new_rules = array( | |
'shop/(.+)/?$' => 'index.php?product_cat=' . $matches[1] | |
); | |
return $new_rules + $rules; | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('template_redirect', function() { | |
if ((is_shop() || is_product_category() || is_product_tag() || is_product()) && !is_cart() && !is_checkout() && !is_account_page()) { | |
$redirect_page_url = 'http://yourdomain.com/your-specific-page/'; // Change to your desired URL | |
wp_safe_redirect($redirect_page_url); | |
exit; | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Redirect user after registration. | |
add_filter( 'woocommerce_registration_redirect', function () { | |
return home_url('thankyou'); | |
} ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Remove quantity from order items | |
add_filter( 'woocommerce_order_item_quantity_html', function ( $quantity_html, $item ) { | |
return ''; | |
}, 10, 2 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_get_item_data', function ( $item_data, $cart_item ) { | |
$item_data[] = array( | |
'key' => 'SKU', | |
'display' => 'SKU: ' . $cart_item['data']->get_sku(), | |
); | |
return $item_data; | |
}, 10, 2 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Redirects on add to cart (not tested with AJAX) | |
add_filter('woocommerce_add_to_cart_redirect', function ($url) { | |
return wc_get_checkout_url(); | |
}); | |
// Redirects requests made to the cart page. | |
add_action("template_redirect", function () { | |
if (is_cart()) { | |
// Woo checkout redirects from checkout back to cart when there is nothing to check out. | |
// So, if the cart is empty, redirect to the shop page to prevent a redirect loop. | |
if (WC()->cart->is_empty()) { | |
wp_safe_redirect(wc_get_page_permalink('shop')); | |
} else { | |
wp_safe_redirect(wc_get_checkout_url()); | |
} | |
exit(); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment