Skip to content

Instantly share code, notes, and snippets.

@nicomollet
Created April 23, 2018 08:06
Show Gist options
  • Save nicomollet/d44c672c6233dfcb50947947dc10cd13 to your computer and use it in GitHub Desktop.
Save nicomollet/d44c672c6233dfcb50947947dc10cd13 to your computer and use it in GitHub Desktop.
WooCommerce Product Price Shortcode, compatible with multisite
<?php
/**
* WooCommerce Product Price Shortcode Compatible with Multisite
* Example: [woocommerce_price_multisite id="6452" site="6"]
*
* @param $atts
*
*/
function woocommerce_price_multisite( $atts ) {
$atts = shortcode_atts( array(
'id' => '',
'site' => '',
), $atts, 'woocommerce_price_multisite' );
if ( ! empty( $atts['site'] ) ) {
switch_to_blog ($atts['site']);
if ( ! empty( $atts['id'] ) ) {
$product_data = get_post( $atts['id'] );
$product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
if ( !empty($product) ) {
echo $product->get_price_html();
}
}
restore_current_blog();
}
}
add_shortcode( 'woocommerce_price_multisite', 'woocommerce_price_multisite' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment