'Woocommerce WC_Form_Handler not found
I'm newly using classes in my Plugin. Before I've used classes, my Ajax add to cart function worked fine. Now I'm getting this error:
Uncaught Error: Class 'system\core\WC_Form_Handler' not found
("system\core" is the namespace of my file)
Before I am calling Wc_Form_Handler::add_to_cart_action(); I'm checking if this specific class exists. So if the class wouldn't exist, the class wouldn't be called later...)
I've also taken a look into get_declared_classes(); to make sure that my classes are called after the Wc_Form_Handler.
Does someone have any idea how I can deal with that problem?
namespace system\core;
class Woocommerce{
public function __construct() {
add_action( 'wp_footer', array( __CLASS__, 'ace_product_page_ajax_add_to_cart_js' ) );
add_action( 'wc_ajax_ace_add_to_cart', array( __CLASS__, 'ace_ajax_add_to_cart_handler' ) );
add_action( 'wc_ajax_nopriv_ace_add_to_cart', array( __CLASS__, 'ace_ajax_add_to_cart_handler' ) );
add_action('init', array( __CLASS__, 'remove_add_cart_handler' ),10);
}
* JS for AJAX Add to Cart handling
*/
public static function ace_product_page_ajax_add_to_cart_js() {
?><script type="text/javascript" charset="UTF-8">
jQuery(function($) {
$('form.cart').on('submit', function(e) {
e.preventDefault();
var form = $(this);
form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6, "border-radius": "30px", } });
var formData = new FormData(form[0]);
formData.append('add-to-cart', form.find('[name=add-to-cart]').val() );
// Ajax action.
$.ajax({
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'ace_add_to_cart' ),
data: formData,
type: 'POST',
processData: false,
contentType: false,
complete: function( response ) {
response = response.responseJSON;
if ( ! response ) {
return;
}
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
}
var $thisbutton = form.find('.single_add_to_cart_button'); //
// var $thisbutton = null; // uncomment this if you don't want the 'View cart' button
// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
// Remove existing notices
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
// Add new notices
form.closest('.product').before(response.fragments.notices_html)
form.unblock();
}
});
});
});
</script><?php
}
/**
* Add to cart handler.
*/
public static function ace_ajax_add_to_cart_handler() {
if (class_exists('WC_Form_Handler')){
WC_Form_Handler::add_to_cart_action();
WC_AJAX::get_refreshed_fragments();
}
}
public static function remove_add_cart_handler() {
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
(edit)
Because the class gets loaded I think that this isn't the problem but could my autoloader be the problem?
spl_autoload_register(function ($class_name) {
$location = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class_name) . '.php';
if (file_exists($location)) {
try {
require_once $location;
return;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
});
Solution 1:[1]
Alright, I got it! You need to define the WC-Form_Handler class as global.
public static function ace_ajax_add_to_cart_handler() {
if (class_exists('WC_Form_Handler')){
\WC_Form_Handler::add_to_cart_action();
\WC_AJAX::get_refreshed_fragments();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gamernamer |
