[英]Woocommerce - Sum subtotal of a product with fixed + price in cart
我的問題是如何將固定價格與產品小計計算相加。 Woocommerce 價格基於數量,但我想要購物車 sum+ 中產品的小計,並具有特定價格我的代碼:
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// $cart_item['data']->set_price($custom_price);
$_product = wc_get_product( $cart_item['data']->get_id() );
$getProductDetail = wc_get_product( $cart_item['product_id'] );
$quantity_cr = $cart_item['quantity'];
$price = get_post_meta($cart_item['product_id'] , '_price', true);
/*Regular Price and Sale Price*/
//echo "Regular Price: ".get_post_meta($cart_item['product_id'] , '_regular_price', true)."<br>";
//echo "Sale Price: ".get_post_meta($cart_item['product_id'] , '_sale_price', true)."<br>";
$opdruk = $cart_item['thwepof_options'];
foreach($opdruk as $cr_item) {
$total_price_each_prduct = $cart_item['line_total'];
$opdruk_value = $cr_item['value'];
//print_r($opdruk_value);
if ($opdruk_value != "Geen opdruk") {
$price_opdruk = $price + 39; // this is the fixed price i want to add (not on quantity based)
}
}
}
您好,您可以這樣做,例如在總價中添加稅金/運費/等
function prefix_add_price_line( $cart ) {
$add_price = 39;
$cart->add_fee( __( 'Vat Exempt Tax', 'yourtext-domain' ) , $add_price );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_price_line' );
試試這個代碼
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart_object->get_cart() as $cart_item ) {
## Price calculation ##
$price = $cart_item['data']->price * 12;
## Set the price with WooCommerce compatibility ##
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$cart_item['data']->price = $price; // Before WC 3.0
} else {
$cart_item['data']->set_price( $price ); // WC 3.0+
}
}
}
你可以嘗試使用這個鈎子:
function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance ) {
// some logic
return $product_subtotal;
};
// add the filter
add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.