【问题标题】:Display of custom field value in Woocommerce widget area…在 Woocommerce 小部件区域中显示自定义字段值...
【发布时间】:2018-09-17 11:07:20
【问题描述】:

我很难在 woocommerce 购物篮的自定义字段中显示所有数值相加的结果。所以基本上有一个自定义字段存储项目的 m3。

我想显示所有自定义字段组合的值 - 即侧边栏小部件区域中的总运输量。下面的代码是为我生成的,它显示了结帐页面的总运输量很棒

add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
function display_cart_volume_total() {
    $total_volume = 0;

    // Loop through cart items and calculate total volume
    foreach( WC()->cart->get_cart() as $cart_item ){
        $product_volume = (float) get_post_meta( $cart_item['product_id'], '_item_volume', true );
        $total_volume  += $product_volume * $cart_item['quantity'];
    }

    if( $total_volume > 0 ){

        // The Output
        echo ' <tr class="cart-total-volume">
            <th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
            <td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
        </tr>';
    }
}

我如何在某个页面上的某个地方(比如在小部件区域)显示总数?

我尝试了以下方法:

<?php 
echo ' <tr class="cart-total-volume">
            <th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
            <td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
        </tr>';

?>

【问题讨论】:

    标签: php wordpress woocommerce widget shortcode


    【解决方案1】:

    您可以通过这种方式从您的函数中创建自定义简码:

    add_shortcode( 'shipping_volume', 'display_total_shipping_volume' );
    function display_total_shipping_volume( $atts ) {
        ob_start(); // Start buffering output
    
        echo '<table>';
    
        display_cart_volume_total();
    
        echo '</table>';
    
        // Render output from buffer
        return ob_get_clean();
    }
    

    您可以在默认的 Wordpress“文本”小部件中使用 [shipping_volume]...

    或作为 php sn-p:

    <?php echo '<table>'; display_cart_volume_total(); echo '</table>'; ?>
    

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多