Skip to content

Instantly share code, notes, and snippets.

View dhirenpatel22's full-sized avatar
🏠
Working from home

Dhiren Patel dhirenpatel22

🏠
Working from home
View GitHub Profile
@dhirenpatel22
dhirenpatel22 / wc-hide-all-shipping-if-free-shipping-is-available.php
Created November 25, 2020 12:03
Hide shipping rates when free shipping is available
<?php
/**
* Hide shipping rates when free shipping is available.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
@dhirenpatel22
dhirenpatel22 / exclude-category-from-shop.php
Created July 31, 2020 13:55
Exclude specific categories from the Shop page
<?php
/**
* Exclude specific categories from the Shop page
* @param $terms
* @param $taxonomies
* @param $args
* @return array
*/
function ts_get_subcategory_terms($terms, $taxonomies, $args ) {
$new_terms = array();
@dhirenpatel22
dhirenpatel22 / change-free-shipping-label.php
Created July 19, 2020 13:22
Change label for free standard shipping, when the shipping method’s rate returns $0.00
<?php
/**
* Change label for free standard shipping, when the shipping method’s rate returns $0.00
* @param $full_label
* @param $method
* @return string
*/
function custom_display_zero_shipping_cost($full_label, $method){
if( $method->cost == 0.0 ) {
$full_label = '<span class=”woocommerce-Price-amount amount”>Free Shipping</span>';
@dhirenpatel22
dhirenpatel22 / redirect-after-reset-password-based-on-userrole.php
Created July 19, 2020 13:16
Redirect user to different page based on user role after password reset from the backend screen
<?php
/**
* Redirect user to different page based on userrole after password reset from backend screen
* @param $user
*/
function custom_redirect_retail_page_based_on_role($user) {
$user_roles = (array) $user->roles;
if ( in_array('userrole1', $user_roles) ) {
wp_redirect( 'Custom Page Link' );
@dhirenpatel22
dhirenpatel22 / new-subscription-webhook-topic.php
Created July 19, 2020 13:06
Add New Webhook topics for WooCommerce Subscription
<?php
function add_custom_filters_and_actions() {
add_filter( 'woocommerce_webhook_topic_hooks', 'add_custom_wcs_topics', 30, 2 );
add_filter( 'woocommerce_valid_webhook_events', 'add_custom_wcs_events', 20, 1 );
add_filter( 'woocommerce_webhook_topics' , 'add_custom_wcs_topics_admin_menu', 20, 1 );
@dhirenpatel22
dhirenpatel22 / after-register-redirect.php
Created June 22, 2020 18:18
After registration, logout the user and redirect to home page
function custom_registration_redirect() {
wp_logout();
return home_url('/');
}
add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
@dhirenpatel22
dhirenpatel22 / logout-redirect.php
Created June 22, 2020 18:17
Redirect users to another page after logout in WooCoommerce
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_redirect( home_url() );
exit();
}
@dhirenpatel22
dhirenpatel22 / postdate.js
Created June 22, 2020 06:05
How to remove your WordPress post date stamp from Google SERPS?
document.write("<?php the_time('F jS, Y') ?>");
@dhirenpatel22
dhirenpatel22 / disable-taxonomy-archive.php
Last active June 11, 2020 08:14
How to disable taxonomy archives in WordPress?
<?php
/**
* How to disable taxonomy archives in WordPress?
* @author Dhiren Patel
* @link http://www.dhirenpatel.me/
*/
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
@dhirenpatel22
dhirenpatel22 / create-custom-subscription-webhooks.php
Created May 8, 2020 12:15
Create custom hooks for WooCommerce Subscription status change to cancelled and subscription status change to active
<?php
function add_custom_filters_and_actions() {
add_filter( 'woocommerce_webhook_topic_hooks', 'add_custom_wcs_topics', 30, 2 );
add_filter( 'woocommerce_valid_webhook_events', 'add_custom_wcs_events', 20, 1 );
add_filter( 'woocommerce_webhook_topics' , 'add_custom_wcs_topics_admin_menu', 20, 1 );