Search
Close this search box.
Search
Close this search box.

WooCommerce snippets

Woocommerce product reviews

add_shortcode(
"product_reviews",
function ($atts = array(), $content = "") {
ob_start();
comments_template();
return ob_get_clean();
},
10,
2
);

How to get product category IDs only

global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
foreach( $product_cats_ids as $cat_id ) {
    $term = get_term_by( 'id', $cat_id, 'product_cat' );
    echo $term->name;
}

How to get the category name of a product

$term_list = wp_get_post_terms(get_the_ID(), 'product_cat');
echo $term_list[0]->name;

How to get the category url of a product

$term_list = wp_get_post_terms(get_the_ID(), 'product_cat');
echo get_term_link( $term_list[0]->slug, 'product_cat' );

How to get the tag name of a product

$term_list = wp_get_post_terms(get_the_ID(), 'product_tag');
echo $term_list[0]->name;

How to remove iCanLocalize prompts from WooCommerce admin

Add this in your functions.php

define("ICL_DONT_PROMOTE", true);

How to automatically add feature image to existing product, if image filename is equal to product’s sku

// When an image is added in the library, set feature image to existing product if filename equals sku
function add_image_to_product_if_sku_is_equal_to_filename($attachmentID) {
    if (!class_exists('WC_Product'))
        return; // if no WooCommerce do nothing
        
    // an attachment was just saved, first of all get the file name
    $src = wp_get_attachment_image_src($attachmentID, 'full');
    $filename = pathinfo($src[0], PATHINFO_FILENAME);

    // now let's see if exits a product with the sku that match filename
    $args = array(
        'meta_key' => '_sku',
        'meta_value' => $filename,
        'post_type' => 'product',
        'posts_per_page' => '1' // assuming sku is unique get only one post
    );
    $prods = get_posts($args);
    if (!empty($prods)) {

        // ok we have a match, exists a product having sku that match filename
        $product = array_pop($prods);

        // set the thumbnail for the product
        set_post_thumbnail($product, $attachmentID);

        // now "attach" the post to the product setting 'post_parent'
        $attachment = get_post($attachmentID);
        $attachment->post_parent = $product->ID;
        wp_update_post($attachment);
    }
}

add_action('add_attachment', 'add_image_to_product_if_sku_is_equal_to_filename');

Return to shop button

By default WooCommerce cart and checkout page does not have a ‘Return to shop’ button. The HTML code to enter it is:

<a class="button wc-backward" href="<?php echo apply_filters('woocommerce_return_to_shop_redirect', get_permalink(wc_get_page_id('shop'))); ?>"><?php _e('Return To Shop', 'woocommerce') ?></a>

And here are the files you need to edit:

woocommerce/cart/cart.php
woocommerce/checkout/review-order.php

How to get the current logged in user’s role

function get_user_role() {
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    return $user_role;
}