我试图在一个短代码内显示一个自定义购物车的总金额。该代码获取购物车总价,然后减去“新葬礼类型”类别中任何产品的价格,以显示小计。代码如下:
add_shortcode( 'quote-total', 'quote_total' );
function quote_total(){
$total = $woocommerce->cart->total;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( has_term( 'funeral-types-new', 'product_cat', $_product->id) ) {
$disbursement = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
}
}
$subtotal = $total-$disbursement;
echo '<div>'.$subtotal.'</div><div> + '.$disbursement.'</div>';
}
美元支付显示良好,但美元小计显示0,因此我认为$subtotal=$total-$PANDEMENT;?
非常感谢您的帮助。
您的代码中有许多错误,例如:
因此,请尝试:
add_shortcode( 'quote-total', 'get_quote_total' );
function get_quote_total(){
$total = WC()->cart->total;
$disbursement = 0; // Initializng
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( array('funeral-types-new'), 'product_cat', $cart_item['product_id'] ) ) {
$disbursement += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
$subtotal = $total - $disbursement;
return '<div>'.wc_price($subtotal).'</div><div> + '.wc_price($disbursement).'</div>';
}
// USAGE: [quote-total]
// or: echo do_shortcode('[quote-total]');
它应该更好地工作。
你没想过用
WC()->cart->get_subtotal();