Skip to content

Instantly share code, notes, and snippets.

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

Caleb Burks WPprodigy

🖥️
🍕🍕🍕
View GitHub Profile
function wc_ninja_custom_variable_price( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
@WPprodigy
WPprodigy / example.php
Last active November 25, 2015 18:12
Show products from a category conditionally on the cart page
<?php
function get_products_in_cart2() {
$cart_ids = array();
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values['data'];
$cart_ids[] = $cart_product->id;
}
return $cart_ids;
}
@WPprodigy
WPprodigy / functions.php
Created January 19, 2016 17:14
Add subtitle field to Storefront
<?php
// Display subtitle after the header for posts and pages
add_action( 'storefront_single_post', 'storefront_add_subtitle_after_header', 15 );
add_action( 'storefront_page', 'storefront_add_subtitle_after_header', 15 );
function storefront_add_subtitle_after_header() {
the_subtitle( "<h2 class='subtitle'>", "</h2>" );
}
// Display subtitle in the header for posts
@WPprodigy
WPprodigy / functions.php
Created January 20, 2016 17:11
Remove the password strength meter from WooCommerce checkout
function wc_ninja_remove_password_strength() {
if ( wp_script_is( 'wc-password-strength-meter', 'enqueued' ) ) {
wp_dequeue_script( 'wc-password-strength-meter' );
}
}
add_action( 'wp_print_scripts', 'wc_ninja_remove_password_strength', 100 );
@WPprodigy
WPprodigy / functions.php
Created March 31, 2016 21:36
Hide a shipping method based on a use role
function wc_ninja_role_base_shipping_method( $rates, $package ) {
// Make sure flat rate is available
if ( isset( $rates['flat_rate'] ) && ! current_user_can( 'wholesale' ) ) {
unset($rates['flat_rate']);
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_role_base_shipping_method', 10, 2 );
@WPprodigy
WPprodigy / functions.php
Created April 12, 2016 23:12
Show brand image around the product short description.
add_action( 'woocommerce_single_product_summary', 'wc_ninja_add_brand_to_product_page', 19 );
function wc_ninja_add_brand_to_product_page() {
echo do_shortcode('[product_brand width="64px" height="64px" class="alignright"]');
}
@WPprodigy
WPprodigy / functions.php
Created April 12, 2016 23:36
Make product brands sortable
add_filter( 'woocommerce_sortable_taxonomies','wt_sort_brands' );
function wt_sort_brands( $sortable ) {
$sortable[] = 'product_brand';
return $sortable;
}
@WPprodigy
WPprodigy / functions.php
Created November 5, 2016 16:07
Replace author url in WordPress with their twitter link based on their username in twitter.
add_filter( 'the_author_posts_link', 'wc_ninja_change_author_url' );
function wc_ninja_change_author_url($link) {
$username = get_the_author_meta('login');
return "<a href='http://twitter.com/" . $username . "'>" . get_the_author() . "</a>";
}
@WPprodigy
WPprodigy / functions.php
Created November 16, 2016 22:11
Filter products by visibility
add_action( 'restrict_manage_posts', 'wc_ninja_create_filter_dropdown' );
function wc_ninja_create_filter_dropdown(){
$type = 'product';
if ( isset( $_GET['post_type'] ) ) {
$type = $_GET['post_type'];
}
if ( 'product' == $type ){
$values = array(
'Catalog &amp; search' => 'visible',
@WPprodigy
WPprodigy / functions.php
Created December 15, 2016 11:22
Require bundled product input if it only contains optional products.
add_filter( 'woocommerce_bundle_requires_input', 'wc_ninja_require_bundle_input', 10, 2 );
function wc_ninja_require_bundle_input( $requires_input, $product ) {
if ( ! $product->contains( 'mandatory' ) && $product->contains( 'optional' ) ) {
$requires_input = true;
}
return $requires_input;
}