【问题标题】:WooCommerce | Add notification in shipping rate table based on the total weight of the cartWooCommerce |根据购物车的总重量在运费表中添加通知
【发布时间】:2021-05-20 13:38:40
【问题描述】:

我正在尝试在满足两个条件后立即向 WooCommerce 购物车页面的 shipping totals 行添加通知

  1. 提供本地取货方式

  1. 我购物车中的产品总重量超过 14999 克。

我一直在玩 uderneath 的代码,它已经隐藏了某些运输方式(成功),但我无法在满足条件时添加简单的通知。

add_filter( 'woocommerce_package_rates', 'woocommerce_shipping_notification', 9999, 2 );
    
function woocommerce_shipping_notification( $rates, $package ) {
     
     if ( WC()->cart->get_cart_contents_weight() > 14999 ) {
       
         if ( isset( $rates['local_pickup:1'] ) )
             
             unset( $rates['flat_rate:2'], $rates['local_pickup:7'] );
            
            echo "My notification";
     }

    return $rates;
}

【问题讨论】:

    标签: php woocommerce shipping-method


    【解决方案1】:
    if ( !class_exists( 'WooCommerce' ) ) return;
    
    add_action('wp', function() {
    
        if ( is_cart() && !WC()->cart->is_empty() ) {
            
            $cart = WC()->cart;
            $products = $cart->get_cart_contents();
            $cat_ids = array('20', '22');
            $product_categories = array();
            $product_categories_ids = array();
            foreach($cat_ids as $key => $cat_id) {
                $product_categories = array_merge(
                    $product_categories,
                    get_categories(array(
                        'taxonomy' => 'product_cat',
                        'child_of' => $cat_id,  
                    ))
                );
            }
    
            foreach($product_categories as $key => $category) {
                $product_categories_ids[] = $category->term_id;
            }
    
            $categories_included = function() use ( $products, $product_categories_ids ) {
                foreach( $products as $key => $product ) {              
                    $terms = wp_get_object_terms(array($product['product_id']), 'product_cat');
                    foreach( $terms as $_key => $term ) {       
                        if ( in_array( $term->term_id, $product_categories_ids) )
                            return true;
                    }               
                }
                return false;
            };
            
            $only_pick_up = $categories_included() || $cart->get_cart_contents_weight() > 15000;    
            if ( $only_pick_up ) {
    
                add_action( 'woocommerce_before_cart', function() {
                    echo 
                        '<div>
                            <h3>Please contact us to discuss the delivery options</h3>
                        </div>';
                });                 
    
                add_action('woocommerce_cart_totals_before_shipping', function() {
                    foreach(WC()->shipping()->packages[0]['rates'] as $key => $rate) {
                        if ( $rate->__get('instance_id') != 18 ) {
                            unset(WC()->shipping()->packages[0]['rates'][$key]);
                        }
                    }
                });
    
            }
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多