简体   繁体   English

更改Woocommerce中特定产品类别的购物车项目价格

[英]Change cart item prices for specific product categories in Woocommerce

I would like to change regular price to custom price of products in the cart for specific categories only ('t-shirts-d','socks-d','joggers-d','boxers-d') as each of the products share 2 different categories. 我只想将购物车中产品的常规价格更改为自定义价格(仅针对特定类别(“ t恤衫d”,“袜子d”,“慢跑者d”,“拳击手d”)),因为每个产品共有2个不同的类别。

I tried doing so and it worked but the custom price affects on other categories too, and I want to only show the original price for other categories('t-shirts','socks','joggers','boxers'). 我尝试过这样做,但效果很好,但是自定义价格也会影响其他类别,因此我只想显示其他类别的原始价格(“ t恤”,“袜子”,“慢跑者”,“拳击手”)。

I Need help on this. 我需要帮助。

Here is my code so far: 到目前为止,这是我的代码:

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);

THE RIGHT WORKING HOOK (Updated) : 正确的工作钩(已更新)

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 );
            }
        }
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This time everything work on cart and on checkout pages. 这次,一切都可以在购物车和结帐页面上进行。 Totals are updated too . 总计也会更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Woocommerce中的特定产品属性值更改购物车商品价格 - Change cart item prices based on specific product attribute value in Woocommerce 在 Woocommerce 3 中更改购物车商品价格 - Change cart item prices in Woocommerce 3 在 WooCommerce 3+ 中通过特定类别的挂钩更改产品价格 - Change product prices via a hook for specific categories in WooCommerce 3+ 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce WooCommerce中特定产品类别的最低购物车数量 - Minimum cart amount for specific product categories in WooCommerce 根据 Woocommerce 中的自定义购物车项目数据更改购物车项目价格 - Change cart item prices based on custom cart item data in Woocommerce 在 Woocommerce 中动态更改特定产品的购物车项目价格和名称 - Change cart item price and name for a specific product dynamically in Woocommerce 在 Woocommerce 3 中以编程方式设置产品销售价格和购物车项目价格 - Set programmatically product sale price and cart item prices in Woocommerce 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM