Skip to content

Instantly share code, notes, and snippets.

@alexminza
Last active February 5, 2018 18:36
Show Gist options
  • Save alexminza/d08c27646502459a4a07101a13379097 to your computer and use it in GitHub Desktop.
Save alexminza/d08c27646502459a4a07101a13379097 to your computer and use it in GitHub Desktop.
WooCommerce product_url shortcode - Shortcodes, Actions and Filters plugin
//https://stackoverflow.com/questions/30539549/woocommerce-product-link-url-for-simple-products
if(empty($atts)) {
return '';
}
if(isset($atts['id'])) {
$product_data = get_post($atts['id']);
} elseif(isset($atts['sku'])) {
$product_id = wc_get_product_id_by_sku($atts['sku']);
$product_data = get_post($product_id);
} else {
return '';
}
if(is_wp_error($product_data)) {
return $product_data->get_error_message();
}
if('product' !== $product_data->post_type) {
return '';
}
$product = wc_get_product($product_data);
return esc_url(get_post_permalink($product->id));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment