×

 

Home Contact Us Payments SC Commerce SC Commerce Docs SC Commerce Snippets

Simply Computing

We make websites. Simple.

Add shortcode to make link to individual product

This snippet adds a shortcode that can be used to quickly insert a link to any individual product page.

The product is specified by id, for example [product_url id="428"] and the title of the product is used as the link text.

		/**
 * Add shortcode to make link to individual product
 */

add_shortcode('product_url', 'cc_product_url');

function cc_product_url($atts){
  global $wpdb;

  if (empty( $atts )) {
    return '';
  }

  if (isset( $atts['id'])) {
    $product_data = get_post($atts['id']);
  } else {
    return '';
  }

  if ('product' !== $product_data->post_type) {
    return '';
  }

  $_product = wc_get_product($product_data);

  return '<h5><a href="' . get_post_permalink($_product->id) . '">' . get_the_title($_product->id) . '</a><br /></h5>';

}
	
   View Raw Code ID: 9930