Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active November 13, 2023 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/f70bf9a3fd82dafae51f8faf6058f2ae to your computer and use it in GitHub Desktop.
Save mishterk/f70bf9a3fd82dafae51f8faf6058f2ae to your computer and use it in GitHub Desktop.

WooCommerce Snippets

Just a handy collection of small snippets for customising WooCommerce.

<?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 );
<?php
add_filter( 'woocommerce_product_add_to_cart_text', function ( $text, \WC_Product $product ) {
return 'Add to order';
}, 10, 2 );
<?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;
}
<?php
add_filter( 'woocommerce_account_menu_items', function ( $items, $endpoints ) {
// Hide the "Downloads" area by removing it from the array
unset( $items['downloads'] );
// Hide the "Dashboard" tab
unset( $items['dashboard'] );
//unset( $items['orders'] );
//unset( $items['edit-address'] );
//unset( $items['payment-methods'] );
//unset( $items['edit-account'] );
//unset( $items['customer-logout'] );
return $items;
}, 10, 2 );
// Redirect account dashboard through to a desired sub page
add_action( 'template_redirect', function() {
if ( is_account_page() && ! is_wc_endpoint_url() ) {
wp_redirect( wc_get_endpoint_url( 'orders' ) );
exit;
}
});
<?php
add_filter('woocommerce_return_to_shop_text', function() {
return 'Your Custom Text Here'; // Replace with your desired button text
});
<?php
wc_get_product( wc_get_product_id_by_sku( 'sku-here' ) );
<?php
add_filter('rewrite_rules_array', function ($rules) {
$new_rules = array(
'shop/(.+)/?$' => 'index.php?product_cat=' . $matches[1]
);
return $new_rules + $rules;
});
<?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;
}
});
<?php
// Redirect user after registration.
add_filter( 'woocommerce_registration_redirect', function () {
return home_url('thankyou');
} );
<?php
// Remove quantity from order items
add_filter( 'woocommerce_order_item_quantity_html', function ( $quantity_html, $item ) {
return '';
}, 10, 2 );
<?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 );
<?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