【问题标题】:Change cart item prices for specific product categories in Woocommerce在 Woocommerce 中更改特定产品类别的购物车商品价格
【发布时间】:2017-12-10 13:00:05
【问题描述】:

我想将购物车中特定类别('t-shirts-d'、'socks-d'、'joggers-d'、'boxers-d')的常规价格更改为自定义价格每个产品都有 2 个不同的类别。

我试过这样做,但效果很好,但自定义价格也会影响其他类别,我只想显示其他类别的原始价格('t-shirts'、'socks'、'joggers'、'boxers' )。

我需要这方面的帮助。

到目前为止,这是我的代码:

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

【问题讨论】:

    标签: php woocommerce cart custom-taxonomy price


    【解决方案1】:

    正确的工作钩(更新)

    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 文件或任何插件文件中。

    这一次,购物车和结帐页面上的一切都正常。 总数也会更新

    【讨论】:

    • 非常感谢,但该代码仅适用于 6 种产品中的一种。它显示购物车中第二个产品的自定义价格,但不显示其余产品。
    • 价格似乎只在简单产品上发生变化,而在可变产品上没有变化。我尝试了 if is_type = variable 但不起作用
    • @SandeepTete 所以这次更新的版本对你有用……
    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2018-07-31
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多