add_action( 'init', 'my_setcookie_example' );
function my_setcookie_example() {
setcookie( "wp_id", time()+3600 );
}
add_action('wp_ajax_id_add', 'add_to_id');
add_action('wp_ajax_nopriv_id_add', 'add_to_id');
function add_to_id() {
setcookie( "wp_id", time()+3600 );
$arr_id = [];
if ( !empty( $_POST['variation_id'] ) ) {
$id = absint( esc_attr( $_POST['variation_id'] ) );
} else {
$id = absint( esc_attr( $_POST['product_id'] ) );
}
if ( $arr_id == explode( ',', $_COOKIE["wp_id"] ) ) {
return;
}
array_push($arr_id, $id);
$_COOKIE["wp_id"] = implode( ",", $arr_id );
wp_die();
}
add_action( 'init', 'my_setcookie_example' );
function my_setcookie_example() {
setcookie( "wp_id", "", time()+3600 );
}
add_action('wp_ajax_id_add', 'add_to_id');
add_action('wp_ajax_nopriv_id_add', 'add_to_id');
function add_to_id() {
$arr_id = [];
if ( !empty( $_POST['variation_id'] ) ) {
$id = absint( esc_attr( $_POST['variation_id'] ) );
} else {
$id = absint( esc_attr( $_POST['product_id'] ) );
}
if ( $arr_id == explode( ',', $_COOKIE["wp_id"] ) ) {
return;
}
array_push($arr_id, $id);
setcookie( "wp_id", implode( ",", $arr_id ), time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
wp_die();
}
if ( isset( $_COOKIE['wp_id'] ) ) {
$data = explode( ',', $_COOKIE['wp_id'] );
array_push( $data, $value );
if ($data == explode( ',', $_COOKIE['wp_id'] )) {
return;
}
$value = implode( ',', $data );
}
function add_to_id() {
if ( isset( $_POST['variation_id'] ) && ! empty( $_POST['variation_id'] ) ) {
$value = intval( $_POST['variation_id'] );
} else if ( isset( $_POST['product_id'] ) && ! empty( $_POST['product_id'] ) ) {
$value = intval( $_POST['product_id'] );
} else {
wp_send_json_error();
}
if ( isset( $_COOKIE['wp_id'] ) ) {
$data = [];
$data = explode( ',', $_COOKIE['wp_id'] );
wp_send_json_success( count( $data ) );
if ( in_array( $value, $data ) ) {
return;
}
array_push( $data, $value );
$value = implode( ',', $data );
}
setcookie( "wp_id", implode( ",", $value ), time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
}
function add_to_id() {
if ( isset( $_POST['variation_id'] ) && ! empty( $_POST['variation_id'] ) ) {
$value = intval( $_POST['variation_id'] );
} else if ( isset( $_POST['product_id'] ) && ! empty( $_POST['product_id'] ) ) {
$value = intval( $_POST['product_id'] );
} else {
wp_send_json_error();
}
if ( isset( $_COOKIE['wp_id'] ) ) {
$data = [];
$data = explode( ',', $_COOKIE['wp_id'] );
if ( in_array( $value, $data ) ) {
unset( $data[ array_search( $value, $data ) ] );
$value = implode( ',', $data );
setcookie( "wp_id", $value, time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
wp_send_json_success( count( $data ) );
return;
}
array_push( $data, $value );
$value = implode( ',', $data );
}
setcookie( "wp_id", $value, time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
wp_send_json_success( count( $data ) );
}
function add_to_id() {
if ( isset( $_POST['variation_id'] ) && ! empty( $_POST['variation_id'] ) ) {
$value = intval( $_POST['variation_id'] );
} else if ( isset( $_POST['product_id'] ) && ! empty( $_POST['product_id'] ) ) {
$value = intval( $_POST['product_id'] );
} else {
wp_send_json_error();
}
$counter = 0;
if ( isset( $_COOKIE['wp_id'] ) ) {
$data = [];
$data = explode( ',', $_COOKIE['wp_id'] );
if ( in_array( $value, $data ) ) {
unset( $data[ array_search( $value, $data ) ] );
$value = implode( ',', $data );
setcookie( "wp_id", $value, time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
wp_send_json_success( count( $data ) );
return;
}
array_push( $data, $value );
$value = implode( ',', $data );
}
setcookie( "wp_id", $value, time()+86400 * 30, COOKIEPATH, COOKIE_DOMAIN );
if ( !empty( $data ) ) {
wp_send_json_success( count( $data ) );
} else {
wp_send_json_success( $counter + 1 );
}
}