It is not a plugin, it is just a simple file call on ajax.
My Js code
$('.pkg-btn').click(function(){
var this_id = $(this).parent().attr('id');
var url = templateUrl+'/tw-add-to-cart-ajax.php';
$.ajax({
type: 'POST',
url: url,
data: ({selected_package_id: this_id}),
dataType: 'json',
success: function(data1) {
console.log(data1);
$('#conditions').show();
}
});
});
My php file code
<?php
// $product_id = $_POST['selected_package_id'];
// echo $product_id; // It displays id - works fine here
// exit();
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
echo 'hook called'; // This is not showing any message
exit();
global $woocommerce;
$product_id = $_POST['selected_package_id'];
echo $product_id; // This is not displaying id
exit();
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id );
}
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
In php file before calling hook I get the passed id anf echo it. It is showing the id, means js passing data to php and php also getting that id but when I call hook it shows error: jquery-3.4.1.min.js:2 POST domain-url/wp-content/themes/my-theme-folder/tw-add-to-cart-ajax.php 500 (Internal Server Error) and message displaying in network This request has no response data available
It is not a plugin, it is just a simple file call on ajax.
My Js code
$('.pkg-btn').click(function(){
var this_id = $(this).parent().attr('id');
var url = templateUrl+'/tw-add-to-cart-ajax.php';
$.ajax({
type: 'POST',
url: url,
data: ({selected_package_id: this_id}),
dataType: 'json',
success: function(data1) {
console.log(data1);
$('#conditions').show();
}
});
});
My php file code
<?php
// $product_id = $_POST['selected_package_id'];
// echo $product_id; // It displays id - works fine here
// exit();
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
echo 'hook called'; // This is not showing any message
exit();
global $woocommerce;
$product_id = $_POST['selected_package_id'];
echo $product_id; // This is not displaying id
exit();
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id );
}
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
In php file before calling hook I get the passed id anf echo it. It is showing the id, means js passing data to php and php also getting that id but when I call hook it shows error: jquery-3.4.1.min.js:2 POST domain-url/wp-content/themes/my-theme-folder/tw-add-to-cart-ajax.php 500 (Internal Server Error) and message displaying in network This request has no response data available
Share Improve this question edited Apr 6, 2020 at 9:07 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Apr 6, 2020 at 7:59 Muhammad Husanin ZafarMuhammad Husanin Zafar 11 Answer
Reset to default 0You are doing it all wrong, place your code like the following in your file.
<?php
echo 'hook called'; // This is not showing any message
exit();
global $woocommerce;
$product_id = $_POST['selected_package_id'];
echo $product_id; // This is not displaying id
exit();
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id );
}
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
The approach you are using to do it by hooks is not correct.