woocommerce product description code example

Example 1: how to get woocommerce product price

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();

Example 2: how to get woocommerce product price

function so_30165014_price_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
         $_product = wc_get_product( $atts['id'] );
         $html = "price = " . $_product->get_price();
    }
    return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );

Example 3: is product category woocommerce

if( is_product_category() ) {
	// yay, we are on a product category page!
} else {
 
}

Example 4: WooCommerce Add Long Description to Products on Shop Page with Character limit

/**
 * WooCommerce, Add Long Description to Products on Shop Page with Character limit
 *
 * @link https://wpbeaches.com/woocommerce-add-short-or-long-description-to-products-on-shop-page
 */
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_add_long_description' , 9);
function wc_add_long_description() {
  global $product;

  ?>
        <div itemprop="description">
            <?php echo substr( apply_filters( 'the_content', $product->post->post_content ), 0,77 ); echo '...' ?>
        </div>
  <?php
}

Example 5: is product category woocommerce

if( has_term( 4, 'product_cat' ) ) {
	// do something if current product in the loop is in product category with ID 4
}

Tags:

Php Example