将 Woocommerce 挂钩函数转换为短代码 [英] Convert a Woocommerce hooked function into a shortcode

查看:55
本文介绍了将 Woocommerce 挂钩函数转换为短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的一个问题得到了回答,其中包含两个独立的功能,一个使用 WooCommerce add_action钩子.我如何接受这个答案并将其转换为可以添加到产品页面的简码?

A previous question of mine was answered and included two separate functions, one using a WooCommerce add_action hook. How can I take this answer and convert it to a shortcode that I could add to a product page?

我正在使用页面构建器(Divi Builder)来创建 WooCommerce 产品页面的自定义布局/模板.有了短代码,我就可以将短代码粘贴到构建器中,并让它在该模板结构内的任何位置输出结果.

I am using a page builder (Divi Builder) to create a custom layout/template of WooCommerce product pages. Having a shortcode will allow me to paste the shortcode into the builder, and have it output the result anywhere within that template structure.

这是我需要变成短代码的代码:

This is the code I need turned into a shortcode:

// Utility funtion: getting and formtting product data
function format_product_data_output( $the_id ){
    $empty =  __( '<em>(empty)</em>', 'woocommerce' );

    // Get an instance of the WC_Product_Variation object
    $product = wc_get_product( $the_id );

    // Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
    $price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
    $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
    $sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;

    $size = $product->get_attribute( 'pa_size' );
    $size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;

    $stock_qty = $product->get_stock_quantity();
    $stock_qty = ! empty( $stock_qty ) ? $stock_qty : $empty;

    $output = '
    <ul>
        <li class="fs-data-price">'.$price.'</li>
        <li class="fs-data-size">Size: '.$size.'</li>
        <li class="fs-data-sale">'.$sale_price.' Preferred Customer Price</li>
        <li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
    </ul>';

    return $output;
}

//
add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
    global $product;

    $output = '<div class="fs-product-data-wrapper">';

    // Variable products
    if( $product->is_type('variable'))
    {
        // Get available variations in the variable product
        $available_variations = $product->get_available_variations();

        if( count($available_variations) > 0 ){
            foreach( $available_variations as $variation )
                $output .= format_product_data_output( $variation['variation_id'] );
        }
    }
    // Simple products
    elseif( $product->is_type('simple'))
    {
        $output .= format_product_data_output( $product->get_id() );
    }
    else return; // Exit

    echo $output .= '</div>'; // Display
}

推荐答案

更新 2:尝试以下操作:

add_shortcode("variation_table", "custom_available_variations_table");
function custom_available_variations_table( $atts ) {

    global $post;

    // Attributes
    $atts = shortcode_atts(
        array(
            'id'    => $post->ID
        ),
        $atts, 'variation_table'
    );

    if( is_admin() ) return; // Only on front end

    $product = wc_get_product($atts['id']); // Get the WC_Product Object

    $output = '<div class="fs-product-data-wrapper">';

    // Variable products
    if( $product->is_type('variable'))
    {
        // Get available variations in the variable product
        $available_variations = $product->get_available_variations();

        if( count($available_variations) > 0 ){
            foreach( $available_variations as $variation )
                $output .= format_product_data_output( $variation['variation_id'] );
        }
    }
    // Simple products
    elseif( $product->is_type('simple'))
    {
        $output .= format_product_data_output( $product->get_id() );
    }
    else return; // Exit

    return $output .= '</div>'; // return always for a shortcode
}

// Utility funtion: getting and formtting product data
function format_product_data_output( $the_id ){
    $empty =  __( '<em>(empty)</em>', 'woocommerce' );

    // Get an instance of the WC_Product_Variation object
    $product = wc_get_product( $the_id );

    // Only wc_get_price_to_display() respect if product is to be displayed with or without including taxes
    $price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
    $sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
    $sale_price = ! empty( $sale_price ) ? wc_price($sale_price) : $empty;

    $size = $product->get_attribute( 'pa_size' );
    $size = ! empty( $size ) ? get_term_by( 'slug', $size, 'pa_size' )->name : $empty;

    $stock_qty = $product->get_stock_quantity();
    $stock_qty = ! empty( $stock_qty ) ? $stock_qty : $empty;

    $output = '
    <ul>
        <li class="fs-data-price">'.$price.'</li>
        <li class="fs-data-size">Size: '.$size.'</li>
        <li class="fs-data-sale">'.$sale_price.' Preferred Customer Price</li>
        <li class="fs-data-stock">Quantity in Stock: '.$stock_qty.'</li>
    </ul>';

    return $output;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试并有效.

短代码用法:

  • 在产品页面:[variation_table](无需定义ID)
  • 定义产品 ID 的任何地方:[variation_table id='27']
  • In a product page: [variation_table] (without any need of define an ID)
  • Anywhere defining the product ID: [variation_table id='27']

这篇关于将 Woocommerce 挂钩函数转换为短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆