Update Nov 2020
- Check cart items to be sure the product has been added to cart.
- Handling more product data, product variations and their attributes.
On single product page, as the data is submitted on a form using "post" method (no JS / AJAX involved), So you can detect "added to cart" event using the PHP $_POST
variables, this way:
add_action('wp_footer', 'single_added_to_cart_event');
function single_added_to_cart_event()
{
if( isset($_POST['add-to-cart']) && isset($_POST['quantity']) ) :
// Get added to cart product ID (or variation ID) and quantity (if needed)
$id_to_check = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : esc_attr($_POST['add-to-cart']);
$product_id = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : esc_attr($_POST['add-to-cart']);
$variation_id = isset($_POST['variation_id']) ? esc_attr($_POST['variation_id']) : 0;
$quantity = esc_attr($_POST['quantity']);
$found_in_cart = false; // Initializing
// Check cart items to be sure that the product has been added to cart (and get product data)
foreach( WC()->cart->get_cart() as $item ) {
$product = $item['data']; // The WC_Product Object
if( $product->get_id() == $id_to_check ) {
$product_name = $product->get_name(); // Product name
$sku = $product->get_sku(); // Product sku
$type = $product->get_type(); // Product sku
$price = wc_get_price_to_display($product); // Product price for display
$stock_qty = $product->get_stock_quantity(); // Product sku
$stock_status = $product->get_stock_status(); // Product sku
$attributes = $variation_id > 0 ? json_encode($product->get_attributes()) : "''";
$found_in_cart = true;
break; // Stop the loop
}
}
if( $found_in_cart ) :
// The displayed message (example)
if ( $variation_id > 0 ) {
$message = sprintf( __('Product "%s" has been added to cart. \r\nProduct type: %s \r\nProduct price: %s \r\nProduct_id: %s \r\nVariation_id: %s \r\nQuantity: %s'),
$product_name, $type, $price, $product_id, $variation_id, $quantity );
} else {
$message = sprintf( __('Product "%s" has been added to cart. \r\nProduct type: %s \r\nProduct price: %s \r\nProduct_id: %s \r\nQuantity: %s'),
$product_name, $type, $price, $product_id, $quantity );
}
// JS code goes here below
?>
<script>
jQuery(function($){
// All product data
var product_id = <?php echo esc_attr($_POST['add-to-cart']); ?>,
variation_id = <?php echo $variation_id; ?>,
quantity = <?php echo $quantity; ?>,
attributes = <?php echo $attributes; ?>,
name = '<?php echo $product_name; ?>',
type = '<?php echo $type; ?>',
sku = '<?php echo $sku; ?>',
price = '<?php echo $price; ?>',
stock_qty = '<?php echo $stock_qty; ?>',
stock_status = '<?php echo $stock_status; ?>',
message = '<?php echo $message; ?>';
console.log(message);
if ( variation_id > 0 ) console.log(attributes);
alert(message);
});
</script>
<?php
endif; endif;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and work for single product pages (or if using [product_page id="99"]
shortcode like ).
You can use the code on a template, on a custom function (or in a custom shortcode).