Created
April 23, 2018 08:06
-
-
Save nicomollet/d44c672c6233dfcb50947947dc10cd13 to your computer and use it in GitHub Desktop.
WooCommerce Product Price Shortcode, compatible with multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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