在 Woocommerce 小部件区域显示自定义字段值…
Display of custom field value in Woocommerce widget area…
我在显示 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>';
?>
您可以通过这种方式从您的函数创建自定义短代码:
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 "Text" 小部件中使用 [shipping_volume]
…
或作为 php 片段:
<?php echo '<table>'; display_cart_volume_total(); echo '</table>'; ?>
我在显示 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>';
?>
您可以通过这种方式从您的函数创建自定义短代码:
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 "Text" 小部件中使用 [shipping_volume]
…
或作为 php 片段:
<?php echo '<table>'; display_cart_volume_total(); echo '</table>'; ?>