Adding a notice on Cart and checkout page based on item custom field highest value

后端 未结 2 1395
谎友^
谎友^ 2021-01-06 18:13

I have a a custom field value for each product. This custom field is inserted with the code below :



        
相关标签:
2条回答
  • 2021-01-06 18:45

    Here is a custom function that will display a message on cart and checkout (before coupon notice) pages with the highest number of days of manufacture…

    Here is the code:

    add_action('woocommerce_before_cart', 'days_of_manufacture');
    add_action('woocommerce_before_checkout_form', 'days_of_manufacture', 5);
    function days_of_manufacture() {
            $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
            $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
            $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
            $max_days = 0;
    
            foreach( WC()->cart->get_cart() as $cart_item )
                if($cart_item['days_manufacture'] > $max_days)
                    $max_days = $cart_item['days_manufacture'];
    
            if($max_days != 0) {
                if ($max_days == 1)
                    $days_txt = $day_txt;
    
                $output = $text . $max_days . $days_txt;
                echo "<div class='woocommerce-info'>$output</div>";
            }
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    0 讨论(0)
  • 2021-01-06 19:08

    Store the variable in the $_SESSION, then loop thru them and pick the highest, so you can use it later anywhere in your code

    0 讨论(0)
提交回复
热议问题