![](/img/trans.png)
[英]Change cart item prices based on specific product attribute value in Woocommerce
[英]Change cart item prices for specific product categories in Woocommerce
我只想將購物車中產品的常規價格更改為自定義價格(僅針對特定類別(“ t恤衫d”,“襪子d”,“慢跑者d”,“拳擊手d”)),因為每個產品共有2個不同的類別。
我嘗試過這樣做,但效果很好,但是自定義價格也會影響其他類別,因此我只想顯示其他類別的原始價格(“ t恤”,“襪子”,“慢跑者”,“拳擊手”)。
我需要幫助。
到目前為止,這是我的代碼:
function changeprice($html, $cart_item, $cart_item_key){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//$thepro = $woocommerce->cart->get_cart();
$product = $cart_item['data'];
$heading_nicename = array('t-shirts-d','socks-d','joggers-d','boxers-d');
//$heading_nicename1 = array('t-shirts','socks','joggers','boxers');
$termsother = $heading_nicename;
foreach( $termsother as $termsnew ) {
if (is_cart()) {
$price_adjusted = 666.666666667; // your adjustments here
$price_base = $cart_item['data']->sale_price;
if (!empty($price_adjusted)) {
if (intval($price_adjusted) > 0) {
$cart_item['data']->set_price($price_adjusted);
} /*else {
$html = '<span class="amount">' . wc_price($price_base)
. '</span>';
}*/
}
}
}
}
}
add_filter('woocommerce_cart_item_price', 'changeprice', 100, 3);
正確的工作鈎(已更新) :
add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( has_term( array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'] ) ){
$price_adjusted = 666.666666667; // your adjustments here
if ( ! empty( $price_adjusted ) && intval( $price_adjusted ) > 0) {
$cart_item['data']->set_price( $price_adjusted );
}
}
}
}
代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。
這次,一切都可以在購物車和結帳頁面上進行。 總計也會更新 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.