How to save custom field value you enter in the shopping cart in session for each product?

Good day!
Trying to implement the functionality of the rental in the basket, by adding a custom field to enter the term of the lease (_quantity_per_day).

The problem is that the value stored in session is the same for all. And how to save for each item I do not understand.

5e9a287730e4d109117589.png

<?php
//functions.php
// add custom values to the shopping cart object

add_filter('woocommerce_add_cart_item_data', 'qpd_add_cart_item_data');
qpd_add_cart_item_data function($cart_item_data)
{
 $cart_item_data['_quantity_per_day'] = 1;
 return $cart_item_data;
}


//cart.php
// change the value of a variable quantity_per_day when you change input
?>

<td class="product-quantity"
 data-title="<?php esc_attr_e('Quantity per day', 'woocommerce'); ?>">
<?php
 $session_quantity_per_day = WC()->session->get('_quantity_per_day');

 if (empty(filter_input(INPUT_POST, "quantity_per_day"))) {
 $cart_item['_quantity_per_day'] = $session_quantity_per_day;

 } else {
 $cart_item['_quantity_per_day'] = filter_input(INPUT_POST, "quantity_per_day");
 WC()->session->set('_quantity_per_day', $cart_item['_quantity_per_day']);;
}

 echo '<div class="quantity">';
 echo '<label class="screen-reader-text" for="quantity_per_day"></label>';
 echo '<input type="number" class="input-text qty text quantity_per_day" name="quantity_per_day" value="' . $cart_item['_quantity_per_day'] . '">';
 echo '</div>';

?>
</td>
April 19th 20 at 12:37
0 answer

Find more questions by tags WooCommerce