Answer the question
In order to leave comments, you need to log in
How to save the value of the custom input field in the cart, in the session for each product?
Good afternoon!
I'm trying to implement the rental functionality in the cart by adding a custom field to enter the rental period (_quantity_per_day).
The problem is that the value stored in the session is the same for everyone. And how to save it for each product is not clear to me.
<?php
//functions.php
// добавление пользовательского значения в объект корзины
add_filter('woocommerce_add_cart_item_data', 'qpd_add_cart_item_data');
function qpd_add_cart_item_data($cart_item_data)
{
$cart_item_data['_quantity_per_day'] = 1;
return $cart_item_data;
}
//cart.php
// изменение значения переменной quantity_per_day при изменении 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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question