Skip to content

Instantly share code, notes, and snippets.

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

Caleb Burks WPprodigy

🖥️
🍕🍕🍕
View GitHub Profile
@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 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
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 / 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 / cart-addons.php
Created February 2, 2017 13:20
Remove cart addons default location on the cart page in WooCommerce
add_action( 'woocommerce_after_cart_table', 'wc_ninja_remove_cart_addons' );
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 February 6, 2017 18:34
Add costs to a WooCommerce shipping rate in a pattern
function wc_ninja_change_shipping_rate_cost( $rates, $package ) {
$shipping_method_id = 'table_rate:14:1';
$cost_per_case = 50;
$products_per_case = 12;
// Make sure the specific table rate is available
if ( isset( $rates[$shipping_method_id] ) ) {
$products_in_cart = WC()->cart->cart_contents_count;
$additional_cost = 0;
@WPprodigy
WPprodigy / account-funds.php
Created February 24, 2017 02:13
Check to add account funds at the processing order status
add_action( 'woocommerce_order_status_processing', 'wc_ninja_add_funds_at_processing', 5 );
function wc_ninja_add_funds_at_processing( $order_id ) {
if ( method_exists( 'WC_Account_Funds', 'add_funds' ) ) {
wc_ninja_maybe_increase_funds( $order_id );
}
}
function wc_ninja_maybe_increase_funds( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
@WPprodigy
WPprodigy / functions.php
Created June 15, 2017 20:23
Add "continue shopping" link to the cart page.
add_action( 'woocommerce_after_cart_totals', 'wc_ninja_continue_shopping_button' );
function wc_ninja_continue_shopping_button() {
$shop_page_url = wc_get_page_permalink( 'shop' );
echo '<a href="' . $shop_page_url . '" class="button">Continue Shopping</a>';
}
@WPprodigy
WPprodigy / functions.php
Last active June 23, 2017 22:36
Add Brands image to archives.
add_action( 'woocommerce_archive_description', 'woocommerce_brand_image_custom', 18 );
function woocommerce_brand_image_custom() {
if ( is_tax( 'product_brand' ) ){
global $wp_query;
$category = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
@WPprodigy
WPprodigy / functions.php
Created July 13, 2017 22:55
Change shipping costs conditionally based on package contents.
add_filter( 'woocommerce_shipping_packages', 'wc_ninja_woocommerce_package_rates', 20 );
function wc_ninja_woocommerce_package_rates( $packages ) {
$is_warm_weather = true;
foreach ( $packages as $package ) {
$has_candy = false;
foreach ( $package['contents'] as $cart_product ) {
if ( has_term( 'clothing', 'product_cat', $cart_product['product_id'] ) ) {
$has_candy = true;