Last active
December 11, 2022 08:13
-
-
Save bagerathan/905b2ef1a6d6fd2046b4778022737a1c to your computer and use it in GitHub Desktop.
[WooCommerce Snippets] helpful snippets for WooCommerce development #woo
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
function woo_add_cart_fee() { | |
global $woocommerce; | |
if ( is_cart() ) { | |
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 ); | |
} | |
} | |
add_action( 'woocommerce_before_cart_table', 'woo_add_cart_fee' ); |
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_filter('woocommerce_get_price_html', 'soli_change_price_in_loop', 10, 2); | |
function soli_change_price_in_loop($price, $product){ | |
if(is_shop() && !is_admin()){ | |
$discount_price = get_post_meta($product->id, 'discount_price_new', true); | |
$actual_price = $product->get_price(); | |
if($discount_price){ | |
$price_html = '<div class="soli-new-price">'; | |
$price_html .= '<div class="prow"><span>Medlemspris</span><span>'.wc_price($discount_price).'</span></div>'; | |
$price_html .= '<div class="prow grey"><span>Normalpris</span><span>'.wc_price($actual_price).'</span></div>'; | |
$price_html .= '</div>'; | |
return $price_html; | |
} else { | |
return $price; | |
} | |
} else { | |
return $price; | |
} | |
} |
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
//diable all tabs. | |
function woo_remove_product_tab($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; | |
} | |
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tab', 98); | |
//rename a tab | |
add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98); | |
function woo_rename_tab($tabs) { | |
$tabs['description']['title'] = 'More info'; | |
return $tabs; | |
} |
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
/** | |
* @snippet Disable WooCommerce Payment Gateway for a Specific User Role | |
* @author Misha Rudrastyh | |
* @url https://rudrastyh.com/woocommerce/disable-payment-gateway-by-user-role.html | |
*/ | |
add_filter( 'woocommerce_available_payment_gateways', 'rudr_turn_off_cod' ); | |
function rudr_turn_off_cod( $available_gateways ) { | |
if( current_user_can( 'subscriber' ) ) { | |
if ( isset( $available_gateways[ 'cod' ] ) ) { | |
unset( $available_gateways[ 'cod' ] ); | |
} | |
// if you need to disable multiple payment gateways just add similar code | |
// if ( isset( $available_gateways[ 'payment_gateway_2' ] ) ) { | |
// unset( $available_gateways[ 'payment_gateway_2' ] ); | |
// } | |
} | |
return $available_gateways; | |
} |
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_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' ); | |
function custom_add_to_cart_message() { | |
global $woocommerce; | |
if (get_option('woocommerce_cart_redirect_after_add')=='yes') : | |
$return_to = get_permalink(woocommerce_get_page_id('shop')); | |
$message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') ); | |
else : | |
$message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') ); | |
endif; | |
return $message; | |
} |
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_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 ); | |
function hide_all_shipping_when_free_is_available( $available_methods ) { | |
if( isset( $available_methods['free_shipping'] ) ) : | |
// Get Free Shipping array into a new array | |
$freeshipping = array(); | |
$freeshipping = $available_methods['free_shipping']; | |
// Empty the $available_methods array | |
unset( $available_methods ); | |
// Add Free Shipping back into $avaialble_methods | |
$available_methods = array(); | |
$available_methods[] = $freeshipping; | |
endif; | |
return $available_methods; | |
} |
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 payment gateway to admin email | |
add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 ); | |
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) { | |
if ( $is_admin_email ) { | |
echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>'; | |
} | |
} | |
//Up-sells products per page / per line | |
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 ); | |
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 ); | |
if ( ! function_exists( 'woocommerce_output_upsells' ) ) { | |
function woocommerce_output_upsells() { | |
woocommerce_upsell_display( 3,3 ); // Display 3 products in rows of 3 | |
} | |
} | |
//remove product categories from shop page. | |
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); | |
function custom_pre_get_posts_query( $q ) { | |
if ( ! $q->is_main_query() ) return; | |
if ( ! $q->is_post_type_archive() ) return; | |
if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) { | |
$q->set( 'tax_query', array(array( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => array( 'color', 'flavor', 'spices', 'vanilla' ), // Don't display products in these categories on the shop page | |
'operator' => 'NOT IN' | |
))); | |
} | |
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); | |
} | |
//translate any string | |
add_filter('gettext', 'translate_text'); | |
add_filter('ngettext', 'translate_text'); | |
function translate_text($translated) { | |
$translated = str_ireplace('Choose and option', 'Select', $translated); | |
return $translated; | |
} | |
//add custom field to product variation | |
//Display Fields | |
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 ); | |
//JS to add fields for new variations | |
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' ); | |
//Save variation fields | |
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 ); | |
function variable_fields( $loop, $variation_data ) { ?> | |
<tr> | |
<td> | |
<div> | |
<label></label> | |
<input type="text" size="5" name="my_custom_field[]" value=""/> | |
</div> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<div> | |
<label></label> | |
</div> | |
</td> | |
</tr> | |
<?php } | |
function variable_fields_process( $post_id ) { | |
if (isset( $_POST['variable_sku'] ) ) : | |
$variable_sku = $_POST['variable_sku']; | |
$variable_post_id = $_POST['variable_post_id']; | |
$variable_custom_field = $_POST['my_custom_field']; | |
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) : | |
$variation_id = (int) $variable_post_id[$i]; | |
if ( isset( $variable_custom_field[$i] ) ) { | |
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) ); | |
} | |
endfor; | |
endif; | |
} | |
//Change out of stock to Sold | |
add_filter('woocommerce_get_availability', 'availability_filter_func'); | |
function availability_filter_func($availability){ | |
$availability['availability'] = str_ireplace('Out of stock', 'Sold', $availability['availability']); | |
return $availability; | |
} | |
//Say product already in cart instead of add to cart. | |
/** | |
* Change the add to cart text on single product pages | |
*/ | |
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); | |
function woo_custom_cart_button_text() { | |
global $woocommerce; | |
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if( get_the_ID() == $_product->id ) { | |
return __('Already in cart - Add Again?', 'woocommerce'); | |
} | |
} | |
return __('Add to cart', 'woocommerce'); | |
} | |
/** | |
* Change the add to cart text on product archives | |
*/ | |
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); | |
function woo_archive_custom_cart_button_text() { | |
global $woocommerce; | |
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if( get_the_ID() == $_product->id ) { | |
return __('Already in cart', 'woocommerce'); | |
} | |
} | |
return __('Add to cart', 'woocommerce'); | |
} | |
//Shortcodes for all sale products | |
function woocommerce_sale_products( $atts ) { | |
global $woocommerce_loop; | |
extract(shortcode_atts(array( | |
'per_page' => '12', | |
'columns' => '4', | |
'orderby' => 'date', | |
'order' => 'desc' | |
), $atts)); | |
$woocommerce_loop['columns'] = $columns; | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => 1, | |
'posts_per_page' => $per_page, | |
'orderby' => $orderby, | |
'order' => $order, | |
'meta_query' => array( | |
array( | |
'key' => '_visibility', | |
'value' => array('catalog', 'visible'), | |
'compare' => 'IN' | |
), | |
array( | |
'key' => '_sale_price', | |
'value' => 0, | |
'compare' => '>', | |
'type' => 'NUMERIC' | |
) | |
) | |
); | |
query_posts($args); | |
ob_start(); | |
woocommerce_get_template_part( 'loop', 'shop' ); | |
wp_reset_query(); | |
return ob_get_clean(); | |
} | |
add_shortcode('sale_products', 'woocommerce_sale_products'); | |
//check weather the shop have sale products | |
function woo_have_onsale_products() { | |
global $woocommerce; | |
// Get products on sale | |
$product_ids_on_sale = array_filter( woocommerce_get_product_ids_on_sale() ); | |
if( !empty( $product_ids_on_sale ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// Example: | |
if( woo_have_onsale_products() ) { | |
echo 'have onsale products'; | |
} else { | |
echo 'no onsale product'; | |
} | |
//Set minimum order amount | |
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' ); | |
function wc_minimum_order_amount() { | |
global $woocommerce; | |
$minimum = 50; | |
if ( $woocommerce->cart->get_cart_total(); < $minimum ) { | |
$woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) ); | |
} | |
} | |
//replace paypal logo | |
function paypal_checkout_icon() { | |
return 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_betalen_met_paypal_nl.jpg'; // write your own image URL here | |
} | |
add_filter( 'woocommerce_paypal_icon', 'paypal_checkout_icon' ); | |
//Remove products from breadcrumbs | |
function woo_custom_filter_breadcrumbs_trail ( $trail ) { | |
foreach ( $trail as $k => $v ) { | |
if ( strtolower( strip_tags( $v ) ) == 'products' ) { | |
unset( $trail[$k] ); | |
break; | |
} | |
} | |
return $trail; | |
} | |
add_filter( 'woo_breadcrumbs_trail', 'woo_custom_filter_breadcrumbs_trail', 10 ); | |
//auto add product to cart | |
function add_product_to_cart() { | |
if ( ! is_admin() ) { | |
global $woocommerce; | |
$product_id = 64; | |
$found = false; | |
//check if product already in cart | |
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { | |
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->id == $product_id ) | |
$found = true; | |
} | |
// if product not found, add it | |
if ( ! $found ) | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} else { | |
// if no products in cart, add it | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} | |
} | |
} | |
add_action( 'init', 'add_product_to_cart' ); | |
//Add custom currency symbol | |
add_filter( 'woocommerce_currencies', 'add_my_currency' ); | |
function add_my_currency( $currencies ) { | |
$currencies['ABC'] = __( 'Currency name', 'woocommerce' ); | |
return $currencies; | |
} | |
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2); | |
function add_my_currency_symbol( $currency_symbol, $currency ) { | |
switch( $currency ) { | |
case 'ABC': $currency_symbol = '$'; break; | |
} | |
return $currency_symbol; | |
} | |
//Change add to cart text. | |
/** | |
* Change the add to cart text on single product pages | |
*/ | |
function woo_custom_cart_button_text() { | |
return __('My Button Text', 'woocommerce'); | |
} | |
add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text'); | |
/** | |
* Change the add to cart text on product archives | |
*/ | |
function woo_archive_custom_cart_button_text() { | |
return __( 'My Button Text', 'woocommerce' ); | |
} | |
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); | |
//Send email after a new order | |
function woo_email_order_coupons( $order_id ) { | |
$order = new WC_Order( $order_id ); | |
if( $order->get_used_coupons() ) { | |
$to = 'youremail@yourcompany.com'; | |
$subject = 'New Order Completed'; | |
$headers = 'From: My Name ' . "\r\n"; | |
$message = 'A new order has been completed.\n'; | |
$message .= 'Order ID: '.$order_id.'\n'; | |
$message .= 'Coupons used:\n'; | |
foreach( $order->get_used_coupons() as $coupon) { | |
$message .= $coupon.'\n'; | |
} | |
@wp_mail( $to, $subject, $message, $headers ); | |
} | |
} | |
add_action( 'woocommerce_thankyou', 'woo_email_order_coupons' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment