Skip to content

Instantly share code, notes, and snippets.

@KoolPal
Created February 26, 2020 09:45
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 KoolPal/3fe5c3132663d9d251069c9599c27ca8 to your computer and use it in GitHub Desktop.
Save KoolPal/3fe5c3132663d9d251069c9599c27ca8 to your computer and use it in GitHub Desktop.
WooCommerce functions
/* Add Custom field to WooCommerce product */
add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
woocommerce_wp_text_input( array(
'id' => '_delivery_field',
'label' => __( 'Delivery', 'pvc' ),
'description' => __( 'Enter the delivery time in days.', 'pvc' ),
'desc_tip' => 'true'
) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
update_post_meta( $post_id, '_delivery_field', esc_attr( $_POST['_delivery_field'] ) );
}
/* Adds prefix and/or suffix to WooCommerce Prices */
add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
function bbloomer_price_prefix_suffix( $price, $product ){
$price = 'prefix here ' . $price . ' suffix here';
return apply_filters( 'woocommerce_get_price', $price );
}
/* To ajaxify your cart viewer so it updates when an item is added (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_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
/* Automatically add product to cart on visit */
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
/* Customise ‘add to cart’ text on single product pages */
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
return __( 'My Button Text', 'woocommerce' );
}
/* Change the ‘add to cart’ text on product archives */
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' ); // < 2.1
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
return __( 'My Button Text', 'woocommerce' );
}
/* Customise the number of columns and products displayed per page */
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );
/* Hide the coupon form on the cart or checkout page to encourage a steamlined order process */
function hide_coupon_field( $enabled ) {
if ( is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
/* Hide products from a particular category on the shop page? */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma.
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
/* Personalise a product tab */
add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
function woo_custom_description_tab( $tabs ) {
$tabs['description']['callback'] = 'woo_custom_description_tab_content'; // Custom description callback
return $tabs;
}
/* Remove the orderby dropdown for products */
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
/* Remove specific (or all) of the product tabs */
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
/* Remove product content based on the category */
add_action( 'wp', 'remove_product_content' );
function remove_product_content() {
// If a product in the 'Cookware' category is being viewed...
if ( is_product() && has_term( 'Cookware', 'product_cat' ) ) {
//... Remove the images
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
// For a full list of what can be removed please see woocommerce-hooks.php
}
}
/* Show cart contents/total */
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
/* Show the product description underneath an image */
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment