I have plugin that adds custon data field to order called "delivery_time". Plugin also add extra product in cart as an deposit. Now custom data is added to every product including that extra product. Goal is to only have "delivery time" field only on rental product not on extra "deposit" product.
How i can only add extra field to rental product not to deposit?
function delivery_time_before_add_to_cart_field() {
global $product; // awesome - we have a global product object here
// let's add the input field only for products in a specific category with "rent" slug
if ( ! has_term( 'rent', 'product_cat', $product->get_id() ) ) {
return;
}
echo '<div class="delivery_time-before-add-to-cart-field">
<label for="delivery_time"><h4>Desired delivery time:</h4></label>
<input class="delivery_class" type="time" id="delivery_time" name="delivery_time" placeholder="12:00" maxlength="15">
</div>';
}
add_action( 'woocommerce_add_to_cart_validation', 'delivery_field_validation', 10, 3 );
function delivery_field_validation( $result, $product_id, $quantity ) {
if( has_term( 'rent', 'product_cat', $product_id ) && empty( $_REQUEST[ 'delivery_time' ] ) ) {
wc_add_notice( 'We need desired delivery time please!', 'error' );
return false;
}
return $result;
}
// add field value to cart item data
add_filter( 'woocommerce_add_cart_item_data', 'delivery_time_save_field_value_to_cart_data', 10, 3 );
function delivery_time_save_field_value_to_cart_data( $cart_item_data, $product_id, $variation_id ) {
if( ! empty( $_POST[ 'delivery_time' ] ) && $product_id = 149 ) { // here could be another validation if you need
$cart_item_data[ 'delivery_time' ] = sanitize_text_field( $_POST[ 'delivery_time' ] );
}
return $cart_item_data;
}
// display "Delivery time in Cart
add_filter( 'woocommerce_get_item_data', 'delivery_time_display_field', 10, 2 );
function delivery_time_display_field( $item_data, $cart_item ) {
if( ! empty( $cart_item[ 'delivery_time' ] ) ) {
$item_data[] = array(
'key' => 'Desired delivery time',
'value' => $cart_item[ 'delivery_time' ],
'display' => '', // in case you would like to display "value" in another way (for users)
);
}
return $item_data;
}
add_action( 'woocommerce_checkout_create_order_line_item', 'delivery_time_add_order_item_meta', 10, 4 );
function delivery_time_add_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( ! empty( $values[ 'delivery_time' ] ) ) {
$item->add_meta_data( 'Desired delivery time', $values[ 'delivery_time' ] );
}
}
/*
* START Automatically adding the product to the cart.
*/
function deposit_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 23; // category id that needs to have extra item in cart
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
$free_product_id = 240; // Product Id of the extram item which will get added to cart
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
add_action( 'woocommerce_add_to_cart', 'deposit_add_product_to_cart', 10, 2 );
It was quite simple. I added one if clause.
// add field value to cart item data
add_filter( 'woocommerce_add_cart_item_data', 'delivery_time_save_field_value_to_cart_data', 10, 3 );
function delivery_time_save_field_value_to_cart_data( $cart_item_data, $product_id, $variation_id ) {
if( ! empty( $_POST[ 'delivery_time' ] ) ) { // here could be another validation if you need
$cart_item_data[ 'delivery_time' ] = sanitize_text_field( $_POST[ 'delivery_time' ] );
}
if ( $product_id == 149) {
return $cart_item_data;
}
}