Skip to content

Instantly share code, notes, and snippets.

@j-mccarthy
Last active December 11, 2022 08:16
Show Gist options
  • Save j-mccarthy/9807287 to your computer and use it in GitHub Desktop.
Save j-mccarthy/9807287 to your computer and use it in GitHub Desktop.
Woo commerce Snippets
<?php
// 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' );
}
// Change the 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' );
}
// Add Product Description Tab
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['test_tab'] = array(
'title' => __( 'Terms', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>Terms and Conditions</h2>';
echo apply_filters('the_content', get_post_field('post_content', $post_id));
}
// Change the number of related products displayed
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'child_after_single_product_summary', 20 );
function child_after_single_product_summary() {
woocommerce_related_products( 4, 4 );
}
/**
* Optimize WooCommerce Scripts
* Remove WooCommerce Generator tag, styles, and scripts from non WooCommerce pages.
*/
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_style( 'woocommerce_frontend_styles' );
wp_dequeue_style( 'woocommerce_fancybox_styles' );
wp_dequeue_style( 'woocommerce_chosen_styles' );
wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
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( 'prettyPhoto' );
wp_dequeue_script( 'prettyPhoto-init' );
wp_dequeue_script( 'jquery-blockui' );
wp_dequeue_script( 'jquery-placeholder' );
wp_dequeue_script( 'fancybox' );
wp_dequeue_script( 'jqueryui' );
}
}
}
// Change Add to cart text
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 __( 'Awaiting Release', 'woocommerce' );
}
// Add the Special field to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
// Add new field to order notes section on Check out
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['group_order'] = array(
'label' => __('Camp next to', 'woocommerce'),
'placeholder' => _x('Friends, Family order Number', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
// Add new section to check out page
add_action( 'woocommerce_after_order_notes', 'camplight_extra_checkout_field' );
function camplight_extra_checkout_field( $checkout ) {
echo '<div id="some_custom_checkout_field">';
woocommerce_form_field( 'group_order', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Camp with'),
'placeholder' => __('Friends, Family order Number'),
), $checkout->get_value( 'group_order' ));
echo '</div>';
}
// Process
add_action( 'woocommerce_checkout_update_order_meta', 'camplight_extra_checkout_field_update_order_meta' );
function camplight_extra_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['group_order'] ) ) {
update_post_meta( $order_id, 'Camp with', sanitize_text_field( $_POST['group_order'] ) );
}
}
// Show in admin
add_action( 'woocommerce_admin_order_data_after_billing_address', 'camplight_extra_checkout_field_display_admin_order_meta', 10, 1 );
function camplight_extra_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Camp with').':</strong> ' . get_post_meta( $order->id, 'Camp with', true ) . '</p>';
}
add_filter('woocommerce_email_order_meta_keys', 'camplight_extra_checkout_field_woocommerce_email_order_meta_keys');
function camplight_extra_checkout_field_woocommerce_email_order_meta_keys( $keys ) {
$keys['group_order'] = 'group_order';
return $keys;
}
// Add prefix to order numbers
add_filter( 'woocommerce_order_number', 'prefix_woocommerce_order_number', 1, 2 );
function prefix_woocommerce_order_number( $oldnumber, $order ) {
return 'PREFIX_' . $order->id;
}
/* Change Sold Out Text
**************************************************************************************/
add_filter( 'woocommerce_get_availability', 'camplight_availability', 1, 2);
function camplight_availability( $availability, $_product ) {
if ( !$_product->is_in_stock() ) $availability['availability'] = __('Awaiting release', 'woocommerce');
return $availability;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment