Last active
December 5, 2021 21:07
-
-
Save mairagall/80f03a948dc0eaed66e17a89dd741de0 to your computer and use it in GitHub Desktop.
WooCommerce Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Add a stylesheet after WC styles | |
add_action( 'wp_enqueue_scripts', 'maira_add_stylesheet' ); | |
function maira_add_stylesheet() { | |
wp_register_style( 'woocommerce', get_stylesheet_directory_uri() . '/woocommerce/woocommerce.css' ); | |
if ( class_exists( 'woocommerce' ) ) { | |
wp_enqueue_style( 'woocommerce' ); | |
} | |
} | |
//Full Width Pages on WooCommerce - Genesis | |
add_filter( 'genesis_site_layout', 'maira_cpt_layout' ); | |
function maira_cpt_layout() { | |
if( is_page ( array( 'cart', 'checkout' )) || is_shop() || 'product' == get_post_type() ) { | |
return 'full-width-content'; | |
} | |
} | |
// AJAX Cart | |
// Cart | |
add_action( 'genesis_hook_here', 'maira_cart_count', 12 ); | |
function maira_cart_count() { | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
$count = WC()->cart->cart_contents_count; | |
?><div class="carrito"><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e(' View Cart', 'maira' ); ?>"><?php | |
if ( $count > 0 ) { | |
?> | |
<div class="cart-contents-count"><svg xmlns="http://www.w3.org/2000/svg" viewBox="12.5 7.7 172 206.3"><path d="M180 58.2h-43V45.5c0-20.9-17.2-37.8-38.9-37.8S59.2 24.5 59.2 45.5v12.7H17l-4.5 147.5c0 4.9 3.7 8.2 8.6 8.2h154.8c4.9 0 8.6-3.7 8.6-8.2L180 58.2zM68.2 45.5c0-16.1 13.5-29.5 30.3-29.5s30.3 13.1 30.3 29.5v12.7H68.2L68.2 45.5 68.2 45.5zM62 92c-3.4 0-6.5-2.8-6.5-6.2s2.8-6.2 6.5-6.2c3.4 0 6.5 2.8 6.5 6.2S65.4 92 62 92zM134.9 92c-3.4 0-6.5-2.8-6.5-6.2s2.8-6.2 6.5-6.2c3.4 0 6.5 2.8 6.5 6.2S138.7 92 134.9 92z" fill="#FFF"/></svg><span><?php echo esc_html( $count ); ?></span></div> | |
<?php | |
} | |
?></a></div><?php | |
} | |
} | |
function maira_add_to_cart( $fragments ) { | |
ob_start(); | |
$count = WC()->cart->cart_contents_count; | |
?><div class="carrito"><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View Cart', 'maira' ); ?>"><?php | |
if ( $count > 0 ) { | |
?> | |
<div class="cart-contents-count"><svg xmlns="http://www.w3.org/2000/svg" viewBox="12.5 7.7 172 206.3"><path d="M180 58.2h-43V45.5c0-20.9-17.2-37.8-38.9-37.8S59.2 24.5 59.2 45.5v12.7H17l-4.5 147.5c0 4.9 3.7 8.2 8.6 8.2h154.8c4.9 0 8.6-3.7 8.6-8.2L180 58.2zM68.2 45.5c0-16.1 13.5-29.5 30.3-29.5s30.3 13.1 30.3 29.5v12.7H68.2L68.2 45.5 68.2 45.5zM62 92c-3.4 0-6.5-2.8-6.5-6.2s2.8-6.2 6.5-6.2c3.4 0 6.5 2.8 6.5 6.2S65.4 92 62 92zM134.9 92c-3.4 0-6.5-2.8-6.5-6.2s2.8-6.2 6.5-6.2c3.4 0 6.5 2.8 6.5 6.2S138.7 92 134.9 92z" fill="#FFF"/></svg><span><?php echo esc_html( $count ); ?></span></div> | |
<?php | |
} | |
?></a></div><?php | |
$fragments['a.cart-contents'] = ob_get_clean(); | |
return $fragments; | |
} | |
add_filter( 'woocommerce_add_to_cart_fragments', 'maira_add_to_cart' ); | |
// Disable Variable Product Price Range | |
add_filter( 'woocommerce_variable_sale_price_html', 'maira_variation_price_format', 10, 2 ); | |
add_filter( 'woocommerce_variable_price_html', 'maira_variation_price_format', 10, 2 ); | |
function maira_variation_price_format( $price, $product ) { | |
// Main Price | |
$min_var_reg_price = $product->get_variation_regular_price( 'min', true ); | |
$min_var_sale_price = $product->get_variation_sale_price( 'min', true ); | |
// Sale Price | |
if ( $product->is_on_sale() ) { | |
$price = sprintf( __( 'From: <del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price( $min_var_sale_price ) ); | |
} else { | |
$price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) ); | |
} | |
return $price; | |
} | |
// Remove tabs | |
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 ); | |
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 ); | |
function woo_remove_product_tabs( $tabs ) { | |
unset( $tabs['description'] ); // Remove the description tab | |
unset( $tabs['reviews'] ); // Remove the reviews tab | |
unset( $tabs['additional_information'] ); // Remove the additional information tab | |
return $tabs; | |
} | |
// Change Add to Cart text and behaviour in shop page | |
add_filter( 'woocommerce_product_add_to_cart_text' , 'mg_woocommerce_product_add_to_cart_text' ); | |
function mg_woocommerce_product_add_to_cart_text() { | |
global $product; | |
$product_type = $product->get_type(); | |
switch ( $product_type ) { | |
case 'external': | |
return __( 'Buy Now', 'woocommerce' ); | |
break; | |
case 'grouped': | |
return __( 'View products', 'woocommerce' ); | |
break; | |
case 'simple': | |
return __( 'View Details', 'woocommerce' ); | |
break; | |
case 'variable': | |
return __( 'View Details', 'woocommerce' ); | |
break; | |
default: | |
return __( 'Read more', 'woocommerce' ); | |
} | |
} | |
// Conditional for porduct types | |
if( function_exists('get_product') ) { | |
$product = wc_get_product( $post->ID ); | |
if( $product->is_type( 'variable' ) ) { | |
echo //do something for variable products | |
} | |
else { | |
echo //do something else | |
} | |
} | |
// Sort products by SKU | |
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_catalog_orderby'); | |
function custom_woocommerce_catalog_orderby( $args ) { | |
$args['meta_key'] = '_sku'; | |
$args['orderby'] = 'meta_value'; | |
$args['order'] = 'asc'; | |
return $args; | |
} | |
// Add SKU in category page before price | |
add_action( 'woocommerce_after_shop_loop_item_title', 'prima_custom_shop_item', 1); | |
function prima_custom_shop_item() { | |
global $post, $product; | |
/* product sku */ | |
echo '<p>SKU: '.$product->get_sku().'</p>'; | |
} | |
//Show empty product categories | |
add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_empty_categories', 10, 1 ); | |
function show_empty_categories ( $show_empty ) { | |
$show_empty = true; | |
// You can add other logic here too | |
return $show_empty; | |
} | |
//Hide subcategory count in WC | |
add_filter( 'woocommerce_subcategory_count_html', 'custom_hide_category_count' ); | |
function custom_hide_category_count() { | |
// No count | |
} | |
//Remove Related Products | |
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20); | |
//Remove Product Sort Dropdown on Category Page | |
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 ); | |
//Remove Result Count on Category Page | |
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 ); | |
//Disable Search Results Redirect to Product Page | |
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' ); | |
//Force display empty subcategories in category pages | |
add_filter( 'woocommerce_product_subcategories_hide_empty', 'so_28060317', 10, 1 ); | |
function so_28060317 ( $show_empty ) { | |
$show_empty = true; | |
return $show_empty; | |
} | |
// Change number or products per row to 3 | |
add_filter('loop_shop_columns', 'loop_columns'); | |
if (!function_exists('loop_columns')) { | |
function loop_columns() { | |
return 3; // 3 products per row | |
} | |
} | |
// Display 24 products per page. | |
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 ); | |
// Display category image on category page | |
add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 ); | |
function woocommerce_category_image() { | |
if ( is_product_category() ){ | |
global $wp_query; | |
$cat = $wp_query->get_queried_object(); | |
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); | |
$image = wp_get_attachment_url( $thumbnail_id ); | |
if ( $image ) { | |
echo '<img class="cat-thumbnail" src="' . $image . '" alt="" />'; | |
} | |
} | |
} | |
//Remove related products from single product page | |
remove_action ( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20); | |
//Remove price from single product page | |
remove_action ( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10); | |
// Hide product price on category page | |
function woo_product_listing_remove_price(){ | |
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10); | |
} | |
add_action( 'init', 'woo_product_listing_remove_price' ); | |
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10); | |
add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10); | |
// WooCommerce Loop Subcategory Thumbs - Add Image Wrapper | |
if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) { | |
function woocommerce_subcategory_thumbnail( $category ) { | |
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' ); | |
$dimensions = wc_get_image_size( $small_thumbnail_size ); | |
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true ); | |
if ( $thumbnail_id ) { | |
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size ); | |
$image = $image[0]; | |
} else { | |
$image = wc_placeholder_img_src(); | |
} | |
if ( $image ) { | |
// Prevent esc_url from breaking spaces in urls for image embeds | |
// Ref: http://core.trac.wordpress.org/ticket/23605 | |
$image = str_replace( ' ', '%20', $image ); | |
echo '<div class="imagewrapper"><img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" /></div>'; | |
} | |
} | |
} | |
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10); | |
add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10); | |
// Minimum Order Dollar Amount for a specific product (this example: Curtain Wall must be $100 or more) | |
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' ); | |
function spyr_set_min_total() { | |
// Only run in the Cart or Checkout pages | |
if( is_cart() || is_checkout() ) { | |
global $woocommerce; | |
// Set minimum Curtain Wall total | |
$minimum_cart_total = 100; | |
// Total we are going to be using for the Math | |
// This is before taxes and shipping charges | |
$total = 0; | |
//foreach products in the cart () | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); | |
//curtain wall product id == 3288 | |
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key ); | |
if (strcmp($cart_item['product_id'], "3288") == 0 ) { | |
$subtotal = $_product->get_price() * (float)$cart_item['quantity']; | |
$total = $total + $subtotal; | |
} | |
} | |
// Will display a message along the lines of | |
// A Minimum of 100 USD is required before checking out. (Cont. below) | |
// Current cart total: 6 USD | |
if( $total <= $minimum_cart_total ) { | |
// Display our error message | |
wc_add_notice( sprintf( '<strong>A Minimum subtotal of $%s %s is required for Curtain Walls before checking out.</strong>' | |
.'<br />Current cart\'s total: $%s %s', | |
$minimum_cart_total, | |
get_option( 'woocommerce_currency'), | |
$total, | |
get_option( 'woocommerce_currency') ), | |
'error' ); | |
} | |
} | |
} | |
// Add Continue Shopping Button on Cart Page | |
add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' ); | |
function woo_add_continue_shopping_button_to_cart() { | |
$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) ); | |
echo '<div class="woocommerce-message">'; | |
echo ' <a href="'.$shop_page_url.'" class="button">Continue Shopping →</a> Would you like some more goods?'; | |
echo '</div>'; | |
} | |
// Añadir producto X automáticamente al carrito cuando se agrega Y. | |
add_action( 'woocommerce_add_to_cart', 'adb_add_product_to_cart', 10, 2 ); | |
function adb_add_product_to_cart( $item_key, $product_id ) { | |
$product_category_id = 73; // Libro category id | |
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' ); | |
if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) { | |
$free_product_id = 2396; // Product Id of the free product which will get added to cart | |
$found = false; | |
//check if product already in cart | |
if ( sizeof( WC()->cart->get_cart() ) > 0 ) { | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->get_id() == $free_product_id ) | |
$found = true; | |
} | |
// if product not found, add it | |
if ( ! $found ) | |
WC()->cart->add_to_cart( $free_product_id ); | |
} else { | |
// if no products in cart, add it | |
WC()->cart->add_to_cart( $free_product_id ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment