How to Force a Plugin to Claim Compatibility with WooCommerce HPOS

If you want to try WooCommerce HPOS in your website and it says some plugins are incompatible with HPOS, then you should check those plugins’ code first.

First make sure their codes don’t contain “update_post_meta or get_post_meta for orders. (for products it is ok). If they have, it can cause a problem.

If you are sure that the incompatible plugin has nothing to do with HPOS and doesn’t affect order processing, then you can claim it is compatible – without modifying its source code.

Open your theme’s functions.php and place such a block there:

Continue reading “How to Force a Plugin to Claim Compatibility with WooCommerce HPOS”

Set Incremental SKU-s to your WooCommerce products

When you have products with no SKU and you would like to set Incremental SKU numbers for them directly in the website – then this snippet will help you.

<?php
if(isset($_GET["set_sku"]))
add_action('wp',function(){
$base_sku = 100;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'NOT EXISTS'
)
)
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$product_id = get_the_ID();
$sku = $base_sku++;
update_post_meta($product_id, '_sku', "SK_".$sku);
}
wp_reset_query();
die("DONE. You can remove this code from your website!");
});

That’s it. Now just run yourwebsitecom/?set_sku=1 once and done!

The code will create SK_100, SK_101, … SKU-s for your website.

If you want to create SKU-s for variation as well, we can improve our code and get this snippet below:

Continue reading “Set Incremental SKU-s to your WooCommerce products”

Set different “products per page” number for specified WooCommerce Category

You might want to get different product per pages for each category.

For example for Phones category, you want to display 10 phones per page, for Furniture, you want only 4 products per page, for Accessories, you want to display 100 products per page.

To achieve that is easy, here is how:

Continue reading “Set different “products per page” number for specified WooCommerce Category”

Remove parent categories from WooCommerce Related Products

Remove parent categories from WooCommerce Related Products

Say you are selling Phones, Tablets, Smart Watches etc. – and all of those are children of parent category called Gadgets. In this case you have probably faced non-relevant related-products in product-single page of your WooCommerce website.

So, the idea is this: To remove parent categories from related-categories algorithm and to force WooCommerce to show related categories by last child category only (tablets for a tablet, phones for a phone etc. not tablets for a phone)

So let’s write small snippet for this:

Continue reading “Remove parent categories from WooCommerce Related Products”

Remove pagination trails from WooCommerce Breadcrumb

Today i am sharing small snippet that removes Pagination Trail from WooCommerce breadcrumbs.

Some shop owners may dislike this built-in feature and would like remove WooCommerce pagination links from WooCommerce navigation breadcrumb.

First let’s see how is before-after state of this change.

Before:

After:

Now let’s write a snippet for that:

Continue reading “Remove pagination trails from WooCommerce Breadcrumb”

Add custom recipient to WordPress comment notification

WordPress sends email notification to Comment Post Author by default. And if it is a reply, it also send a notification to the comment author.

So, what if we need to send email to a different person, using some different logic?

In that case we can use WP’s corresponding filter, called comment_notification_recipients.

This filter sits in wp-includes/pluggable.php.

So, we just add this small snippet to functions.php file of our active theme:

add_filter( 'comment_notification_recipients',function( $emails, $comment_ID){
    $emails[]='[email protected]';
    return $emails;
},10,2);

What if this email should be fetched by using some logic, f.e. there is special meta field in each post, where we need to fetch it from. Here is how it should look like:

add_filter( 'comment_notification_recipients',function( $emails, $comment_ID){
    $comment_obj=get_comment($comment_ID);
    if(!empty($comment_obj->comment_post_ID)){
        $email_owner=get_post_meta($comment_obj->comment_post_ID,
                      'email_meta_field',true); 
        if(is_email($email_owner))$emails[]=$email_owner;
    }
    return $emails;
},10,2);

That’s all. If any question on this subject, you can leave a reply. Don’t forget to subscribe my newsletter feed club to get more and more useful small snippets for WP websites.

Quick fix for Cloudflare and PHP REMOTE_ADDR IP Detection

Some CMS-s, Frameworks, Custom Codes have built-in functions on collecting user logs (registrations, updates, submits, payments etc.) where they are using PHP’s $_SERVER[“REMOTE_ADDR”] variable to get the visitor’s IP address.

But as you know very popular Cloudflare Cloud Service passes all your traffic through its own servers – and that’s why all log function which i mentioned above writes Cloudflare’s IP address to the DataBase, not visitor’s actual IP address.

What to do for that.

Continue reading “Quick fix for Cloudflare and PHP REMOTE_ADDR IP Detection”