Skip to content

Instantly share code, notes, and snippets.

View WPprodigy's full-sized avatar
🖥️
🍕🍕🍕

Caleb Burks WPprodigy

🖥️
🍕🍕🍕
View GitHub Profile
@WPprodigy
WPprodigy / bookings-functions.php
Created February 2, 2017 11:41
Show cost per night in cart for WooCommerce Accommodation Bookings
add_action( 'woocommerce_get_item_data', 'wc_ninja_add_custom_product_meta', 15, 2 );
function wc_ninja_add_custom_product_meta( $other_data, $cart_item ) {
if ( ! empty( $cart_item['booking'] ) ) {
$item = $cart_item['booking'];
if ( 'night' === $item['_duration_unit'] ) {
$per_night = $item['_cost'] / $item['duration'];
$other_data[] = array(
'name' => 'Cost Per Night',
@WPprodigy
WPprodigy / functions.php
Created February 1, 2017 00:49
Change WooCommerce attribute labels based on the Product ID
add_action( 'woocommerce_attribute_label', 'wc_ninja_change_attribute_label', 20, 3 );
function wc_ninja_change_attribute_label( $label, $name, $product ) {
// Check if on single product page and if the attribute name is 'color'
if ( is_product() && 'color' === $name ) {
global $product;
// Change the label text based on the product ID
switch ( $product->id ) {
case '357':
$label = 'Custom Label One';
@WPprodigy
WPprodigy / functions.php
Created January 30, 2017 22:11
Add brands listing underneath products on archive pages: http://cld.wthms.co/6ud1/586aeCgL
add_action( 'woocommerce_after_shop_loop_item', 'wc_ninja_add_brands_to_archives', 6 );
function wc_ninja_add_brands_to_archives() {
global $post;
$brand_count = sizeof( get_the_terms( $post->ID, 'product_brand' ) );
$taxonomy = get_taxonomy( 'product_brand' );
$labels = $taxonomy->labels;
echo get_brands( $post->ID, ', ', ' <div class="posted_in">' . sprintf( _n( '%1$s: ', '%2$s: ', $brand_count ), $labels->singular_name, $labels->name ), '</div>' );
}
@WPprodigy
WPprodigy / functions.php
Last active December 4, 2022 16:17
Change add to cart button text in WooCommerce if product is on backorder.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_ninja_change_backorder_button', 10, 2 );
function wc_ninja_change_backorder_button( $text, $product ){
if ( $product->is_on_backorder( 1 ) ) {
$text = __( 'Pre-Order', 'woocommerce' );
}
return $text;
}
@WPprodigy
WPprodigy / functions.php
Created January 26, 2017 23:42
Add billing phone and email to PIP invoice
function wc_ninja_add_to_billing_address( $formatted_billing_address, $type, $order ) {
if ( $order->billing_phone ) {
$formatted_billing_address .= "<br>" . $order->billing_phone;
}
if ( $order->billing_email ) {
$formatted_billing_address .= "<br>" . $order->billing_email;
}
return $formatted_billing_address;
@WPprodigy
WPprodigy / functions.php
Created January 25, 2017 23:52
Add an extra button on single product pages in WooCommerce
add_action( 'woocommerce_after_add_to_cart_button', 'wc_ninja_add_extra_button', 20 );
function wc_ninja_add_extra_button() {
echo "<a href='#' class='button'>Return to shop</a>";
}
@WPprodigy
WPprodigy / index.js
Last active January 23, 2017 07:27
Find out how long you have spent watching videos at https://es6.io
const watched = document.querySelectorAll( '.video.completed' );
let totalSeconds = 0;
// Loop over each video watched.
watched.forEach( video => {
let videoTime = video.querySelector( 'span.duration' ).textContent.split( ':' );
// Turn minutes:seconds into just seconds and append to totalSeconds.
videoTime.map( array => {
totalSeconds += ( parseInt( array[0] ) * 60 ) + parseInt( array[1] );
@WPprodigy
WPprodigy / functions.php
Last active June 23, 2017 04:25
Rename the flat rate shipping label when the cost is $0
// Rename the flat rate shipping label when the cost is $0
function wc_ninja_change_flat_rate_label( $label, $method ) {
if ( 'flat_rate' == $method->method_id && $method->cost <= 0 ) {
$label = "Free Shipping";
}
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'wc_ninja_change_flat_rate_label', 10, 2 );
@WPprodigy
WPprodigy / functions.php
Created January 17, 2017 17:14
Unhook cart addons from the cart page in WooCommerce
add_action( 'woocommerce_after_cart_table', 'wc_ninja_remove_cart_addons', 10 );
function wc_ninja_remove_cart_addons() {
global $sfn_cart_addons;
remove_action( 'woocommerce_after_cart_table', array( $sfn_cart_addons, 'cart_display_addons' ), 20 );
}
@WPprodigy
WPprodigy / functions.php
Created January 13, 2017 22:20
Use unit pricing in WooCommerce
add_action( 'woocommerce_get_price_html', 'wc_ninja_customize_price_html', 2, 10 );
function wc_ninja_customize_price_html( $price, $product ) {
// Enter your product's ID here
$product_id = 87;
if ( $product_id == $product->id ) {
$price .= ' ($2.00 per piece)';
}
return $price;