Woocommerce краткий код продукта - PullRequest
1 голос
/ 02 мая 2020

Я не ленивый и искал, чтобы добиться этого самостоятельно, но потерпел неудачу. Я хочу создать шорткод для одного продукта Woocomerce, чтобы отобразить сводку продукта. Мне интересно, знаете ли вы, как этого можно достичь. Спасибо

1 Ответ

1 голос
/ 02 мая 2020

Следующий фрагмент создаст короткий код, который будет использоваться следующим образом: [product_description product_id="633"]

/**
 * Register product_description shortcode
 * 
 * @param array $atts 'product_id' is the only supported attribute
 * 
 */
function kia_woo_short_description( $atts ) {

    if( ! function_exists( 'woocommerce_template_single_excerpt' ) ) {
        return '';
    }

    $atts = shortcode_atts( array(
        'product_id' => 0
    ), $atts, 'product_description' );

    global $post;
    $backup_post    = $post;

    // If not the current post store then temporarily override the $post object.
    if( 0 < $atts['product_id'] ) {
        $post = get_post( $atts['product_id']);

    }

    // Get the short description template:
    ob_start();
    woocommerce_template_single_excerpt();
    $html = ob_get_clean();

    // Restore original $post object.
    $post    = $backup_post;

    return $html;


}
add_shortcode( 'product_description', 'kia_woo_short_description' );
...