4

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

<?php
    // Insert a Custom Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

function woo_add_custom_general_fields() {

    echo '<div class="options_group">';

    woocommerce_wp_text_input( array(
        'id'                => 'days_manufacture',
        'label'             => __( 'Days for Manufacture', 'woocommerce' ),
        'placeholder'       => '',
        'description'       => __( 'Insert here', 'woocommerce' ),
        'type'              => 'number',
        'custom_attributes' => array(
            'step'  => 'any',
            'min'   => '1'
        ),
    ) );

    echo '</div>';
}


// Save the field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['days_manufacture'];
if( !empty( $woocommerce_number_field ) )
    update_post_meta( $post_id, 'days_manufacture', esc_attr( $woocommerce_number_field ) );
}


// Store custom field
add_action( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );

function save_days_field( $cart_item_data, $product_id ) {
    $special_item = get_post_meta( $product_id , 'days_manufacture',true );
    if(!empty($special_item)) {
        $cart_item_data[ 'days_manufacture' ] = $special_item;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'days_manufacture', $special_item );
    }
    return $cart_item_data;
}


// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );

function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['days_manufacture'] ) ) {
        $custom_items[] = array( "name" => __( "Days", "woocommerce" ), "value" => $cart_item['days_manufacture'] );
    }
    return $custom_items;
} ?>

This code works perfectly.

Now I would like on Cart and checkout (before coupon notice) pages to display the highest value of this custom field (days_manufacture) on a custom message, when multiple items are in Cart, like in this screenshot.

Screenshot

How can I achieve this?

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Felipe Honorato
  • 159
  • 1
  • 9
  • `woocommerce_process_product_meta` is for when you are saving a product, not ordering a product. You probably need `woocommerce_add_order_item_meta`. It looks like you need to add custom product data, in which case I always recommend [Product Add-Ons](https://woocommerce.com/products/product-add-ons/) – helgatheviking Sep 08 '16 at 22:15
  • Of course! For example : I have in my cart two products , Product A has value as the variable " days_manufacture " 15 and the product B has 25. I would like the greatest value , in which case it was 25 presented above shop_table with the following phrase : Your order will be produced in " higher variable " days . – Felipe Honorato Sep 08 '16 at 22:19
  • @LoicTheAztec I've just insert a imagem on my post. Tks for patience! =) – Felipe Honorato Sep 09 '16 at 12:30

2 Answers2

4

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Please could you look at this question ?http://stackoverflow.com/questions/39459649/woocommerce-custom-notice-on-in-thankyou-and-my-account-view-order-pages. Thanks!! – Felipe Honorato Sep 12 '16 at 22:22
0

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

JorgeObregon
  • 3,020
  • 1
  • 12
  • 12