How to create and show a condition through a variable in the cart?

Asked

Viewed 143 times

2

Next, I have a problem to solve in my e-commerce, one of these problems I will explain below:

I need to create the following condition:

Today my e-commerce has a variable that is applied to each product, this variable is production days, precisely to inform my client that the product will be manufactured in x working days.

To execute this variable I use 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;
} ?>

Well, up to this point everything works perfectly, in case it shows in my cart, in each product, the running time of the same. Now that my doubt comes, I need to insert a field, as the image below, where will be shown the longest manufacturing time among the items of the cart,below follows an image demonstrating better my need.

inserir a descrição da imagem aqui

Remembering that I only need to insert this information that is in green in the image.

Thank you all from now on!

  • summarizing you need to know where to move to enter this information and how to take those of the products to make the calculation is this?

  • I just want to show in the cart, as in the image, a sentence containing the largest variable among the products of the cart, in the case of the image are 2 products, where the product "Elephant" has 14 working days is larger than the "Giraffe" which is 10 working days, so the sentence will be using the variable with value 14.

  • Each product will not contain the cooking time and only that appears the longer cooking time in the green? And this?

1 answer

0

The problem has been solved, the code below has been tested and approved!

Follows:

add_action('woocommerce_before_cart', 'days_of_manufacture');
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>";
        }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.