Skip to content

Instantly share code, notes, and snippets.

@jackabox
Last active August 17, 2022 19:23
Show Gist options
  • Select an option

  • Save jackabox/44a047c5f9c8028cdc2c to your computer and use it in GitHub Desktop.

Select an option

Save jackabox/44a047c5f9c8028cdc2c to your computer and use it in GitHub Desktop.
Handy Snippets for WooCommerce

Handy Functions for Manipulating WooCommerce

So far the following areas are covered in this gist:

  • Removing tabs on the product page.
  • Cleaning the WooCommerce loading of scripts/styles on pages with no woo elements.
  • Changing the call order of features on the single product.
  • Removing all WooCommerce called styles.
  • Ajaxifying the cart add
# Ensure cart contents update when products are added to the cart via AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><i class="fa fa-shopping-cart"></i> <?php echo WC()->cart->get_cart_total(); ?> <?php echo sprintf (_n( '(%d)', '(%d)', WC()->cart->cart_contents_count ), WC()->cart->cart_contents_count ); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
# Changing the call order of WooCommerce hooks.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 55 );
# removing the WooCommerce on pages which are not Woo related.
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
function child_manage_woocommerce_styles() {
//remove generator meta tag
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
//first check that woo exists to prevent fatal errors
if ( function_exists( 'is_woocommerce' ) ) {
//dequeue scripts and styles
if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
wp_dequeue_script( 'wc_price_slider' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-add-to-cart' );
wp_dequeue_script( 'wc-cart-fragments' );
wp_dequeue_script( 'wc-checkout' );
wp_dequeue_script( 'wc-add-to-cart-variation' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-cart' );
wp_dequeue_script( 'wc-chosen' );
wp_dequeue_script( 'woocommerce' );
wp_dequeue_script( 'jquery-blockui' );
wp_dequeue_script( 'jquery-placeholder' );
wp_dequeue_script( 'jqueryui' );
}
}
# remove completely
wp_dequeue_script( 'prettyPhoto' );
wp_dequeue_script( 'prettyPhoto-init' );
wp_dequeue_script( 'fancybox' );
}
# Remove all WooCommerce styles
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
# Remove the tab for reviews, this can be changed to remove/add more tabs.
add_filter( 'woocommerce_product_tabs', 'adtrak_woo_remove_reviews_tab', 98);
function adtrak_woo_remove_reviews_tab($tabs) {
unset($tabs['reviews']);
return $tabs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment