__( 'Invalid video.', 'addonn-shoppable-video' ) ) ); } $tagged_products = get_post_meta( $video_id, '_addoshvi_products', true ); $tagged_products = is_array( $tagged_products ) ? array_map( 'absint', $tagged_products ) : array(); if ( ! in_array( $product_id, $tagged_products, true ) ) { wp_send_json_error( array( 'message' => __( 'This product is not available from this video.', 'addonn-shoppable-video' ) ) ); } $product = wc_get_product( $product_id ); if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) { wp_send_json_error( array( 'message' => __( 'This product is currently unavailable.', 'addonn-shoppable-video' ) ) ); } if ( $product->is_type( 'variable' ) ) { // Variable products need the shopper to pick options — send them // to the product page instead of guessing a variation. wp_send_json_error( array( 'message' => __( 'Please choose options for this product.', 'addonn-shoppable-video' ), 'redirect' => get_permalink( $product_id ), 'needs_options' => true, ) ); } $cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity ); if ( ! $cart_item_key ) { wp_send_json_error( array( 'message' => __( 'Could not add product to cart.', 'addonn-shoppable-video' ) ) ); } ADDOSHVI_Analytics::increment( $video_id, 'cart' ); wp_send_json_success( array( 'message' => __( 'Added to cart!', 'addonn-shoppable-video' ), 'cart_count' => WC()->cart->get_cart_contents_count(), 'cart_url' => wc_get_cart_url(), ) ); } }