Update product attribute when add to cart link is clicked in woocommerce

Question

I have tried to add my custom data as product attribute when add to cart link is clicked. It is working fine when single custom entry is made. The code is as follows

add_filter( 'woocommerce_add_cart_item_data', 'save_in_cart_my_custom_product_field', 10, 2  );
function save_in_cart_my_custom_product_field( $cart_item_data, $product_id ) {
if( isset( $_SESSION['domain'] ) ) {
    $cart_item_data[ 'name-on-tshirt' ] = $_SESSION['domain'];

    // When add to cart action make an unique line item
    $cart_item_data['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'custom_data', $_SESSION['domain'] );
  }
return $cart_item_data;
}

This works absolutely fine. But when the data is of multiple entry like

    $_SESSION['domain'] = 'blue,green';

how can I split these values and save the same as session and later save it as attribute of the product?

0
, Ajzz 2 years 2020-01-15T08:41:12-05:00 0 Answers 71 views 0

Leave an answer

Browse
Browse