WooCommerce – HiDonny.com https://hidonny.com Page Speed Optimization Specialist Thu, 23 Mar 2023 07:08:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://hidonny.com/wp-content/uploads/2022/03/cropped-logo-icon.fw_-32x32.png WooCommerce – HiDonny.com https://hidonny.com 32 32 Get Product Attributes Using Shortcode on WooCommerce Product Page https://hidonny.com/2022/02/get-product-attributes-using-shortcode/ Sun, 13 Feb 2022 13:08:06 +0000 https://hidonny.com/?p=422 I were looking for a shortcode to build product layout page on WooCommerce. Thankfully I came across a Stack Overflow answer right here. The GeneratePress elements function is really useful. So many things we can do to dress out our website. We can even customize custom post type layout using the block element and the dynamic content feature from GenerateBlocks. Anyway, here’s the code to show product attributes using shortcode, originally written by LoicTheAztec from Stack Overflow.

if ( ! function_exists( 'display_product_additional_information' ) ) {

    function display_product_additional_information($atts) {

        // Shortcode attribute (or argument)
        $atts = shortcode_atts( array(
            'id'    => ''
        ), $atts, 'product_additional_information' );

        // If the "id" argument is not defined, we try to get the post Id
        if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
           $atts['id'] = get_the_id();
        }

        // We check that the "id" argument is a product id
        if ( get_post_type($atts['id']) === 'product' ) {
            $product = wc_get_product($atts['id']);
        }
        // If not we exit
        else {
            return;
        }

        ob_start(); // Start buffering

        do_action( 'woocommerce_product_additional_information', $product );

        return ob_get_clean(); // Return the buffered outpout
    }

    add_shortcode('product_additional_information', 'display_product_additional_information');

}

Usages:

  1. with a defined product id: [product_additional_information id=’37’] or in php: echo do_shortcode("[product_additional_information id='37']");
  2. In an existing product pageĀ (when “additional information” product tab is removed for example): [product_additional_information] or in php: echo do_shortcode("[product_additional_information]");
]]>
WooCommerce Useful Snippets https://hidonny.com/2022/01/woocommerce-useful-snippets/ Sun, 30 Jan 2022 04:26:52 +0000 https://hidonny.com/?p=389 Continuing my journey of developing my newest smartphone community and comparison website, BestGadgetForum.com, I list useful snippets for WooCommerce customizations. In some areas I want elements to be removed, or modified so it can fit the look I’ve dreamt of. Check it out below!

Remove WooCommerce Breadcrumbs

I’m gonna use a more SEO friendly breadcrumb that uses Schema markup, so the default WooCommerce breadcrumb located on the product page needs to be removed.

/**
 * Remove the breadcrumbs 
 */
add_action( 'init', 'woo_remove_wc_breadcrumbs' );
function woo_remove_wc_breadcrumbs() {
    remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}

Remove Category (Product Meta) on WooCommerce Product Page

add_action( 'after_setup_theme', function() {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
} );

Remove Short Description on WooCommerce Product Page

add_action( 'after_setup_theme', function() {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
} );

Remove Product Price on WooCommerce Product Page

add_action( 'after_setup_theme', function() {
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
} );

Disable Shopping Functions / Remove Add to Cart Button

This filter below works for simple product. I’ve tried to use it on external/affiliate product, but the Buy Button is still there. So the best practice is to use simple product in your product type.

add_filter( 'woocommerce_is_purchasable', '__return_false');

Or use this one if you want to remove Add to Cart/Read More button on product archive page

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); //Remove Add to Cart/Read More Button on Product Archive Page
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 ); //Remove Single Product Image

Remove Single Page Product Tabs: Description, Additional Information, Reviews

add_filter( 'woocommerce_product_tabs', 'diko_remove_product_tabs', 81 );
function diko_remove_product_tabs( $tabs ) {
    unset( $tabs['description'] );
	unset( $tabs['additional_information']);
	unset( $tabs['reviews']);
    return $tabs;
}
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

Remove WooCommerce Title

add_filter( 'woocommerce_show_page_title', 'diko_remove_woo_title');
function diko_remove_woo_title(){
    if(is_shop()){
        return false;
    }
}

Sources

  1. https://woocommerce.com/document/customise-the-woocommerce-breadcrumb/
  2. https://wpbeaches.com/removing-the-product-meta-categories-in-a-product-page-woocommerce/
  3. https://rudrastyh.com/woocommerce/make-products-non-purchasable.html
]]>