I am working on woocommerce right now and I am testing few things ,
I need to retrieve the data of the user and the product that is added to the cart,
for example when the user add the product to the list ,execute a function that
<script type="text">
"user _id":'.get_current_user_id().',
"product_id":'.$product->get_id().',
"product_price":'.$product->get_price().',
"product_name":'.$product->get_name().',
</script>';
but I don’t know what is the correct hook to write it in the add_action(hook name,function name)
I tried the same thing when the user enter the product page and it is working well by the following code:
add_action( 'woocommerce_single_product_summary','get_product_data');
function get_product_data() {
global $product;
global $current_user;
echo '
<script type="text/test" id="product_data">
{
"user":
{
"user_id":'.get_current_user_id().',
"user_name":'.$current_user->user_login.',
"user_email":'.$current_user->user_email.'
},
"product":
{
"product_id":'.$product->get_id().',
"product_price":'.$product->get_price().',
"product_name":'.$product->get_name().',
"product_description":'.$product->get_description().',
"product_type":'.$product->get_type().',
"product_average_rating":'.$product->get_average_rating().',
"product_parent_id":'.$product->get_parent_id().',
"product_rating_count":'.$product->get_rating_count().',
"product_review_count":'.$product->get_review_count().',
"product_children":'.$product->get_children().',
"product_image_source":'.$src.'
}
}
</script>
';
}
then I tried this to the add to cart:
add_action( 'woocommerce_add_to_cart',array($this, 'get_add_to_cart_product_data'));
function get_add_to_cart_product_data(){
global $product;
global $current_user;
echo ' <script> alert("the user '.$current_user->user_login.' added succesfully the '.$product->get_name().' product to the cart"); </script>';
}
it displays the correct user but it don’t access the product,it gives me an error : Uncaught Error: Call to a member function get_name() on null
that means it does not have access on the product like the ‘woocommerce_single_product_summary’ hook.
any idea it would help me to solve my problem thanks you