Getting error message with code

Joined
Sep 26, 2024
Messages
1
Reaction score
0
So, I got this piece of code from a developer. He is now unresponsive.

When I paste this code in the functions.php file and try to save the file, I get an error :
"Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP."

Can anyone have a look and help with the code?

function wooac_added_text() {
return 'View Your Cart';
}

function wooac_check_in_cart( $product ) {
if ( ! class_exists( 'WPCleverWooac' ) || ! isset( WC()->cart ) ) {
return false;
}
if ( is_a( $product, 'WC_Product' ) ) {
$product_id = $product->get_id();
} else {
$product_id = absint( $product );
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
return true;
}
}

return false;
}

add_filter( 'woocommerce_product_add_to_cart_text', function ( $text, $product ) {
if ( wooac_check_in_cart( $product ) ) {
$text = wooac_added_text();
}

return $text;
}, 99, 2 );

add_filter( 'woocommerce_product_add_to_cart_url', function ( $url, $product ) {
if ( wooac_check_in_cart( $product ) ) {
$url = wc_get_cart_url();
}

return $url;
}, 99, 2 );

add_filter( 'woocommerce_product_single_add_to_cart_text', function ( $text, $product ) {
if ( wooac_check_in_cart( $product ) ) {
$text = wooac_added_text();
}

return $text;
}, 99, 2 );

add_filter( 'woocommerce_loop_add_to_cart_args', function ( $args, $product ) {
if ( wooac_check_in_cart( $product ) ) {
$args['class'] = str_replace( 'ajax_add_to_cart', '', $args['class'] );
}

return $args;
}, 99, 2 );

add_filter( 'woocommerce_add_to_cart_form_action', function ( $action ) {
global $product;

if ( wooac_check_in_cart( $product ) ) {
$action = wc_get_cart_url();
}

return $action;
}, 99 );

add_action( 'woocommerce_before_add_to_cart_button', function () {
global $product;

if ( wooac_check_in_cart( $product ) ) {
?>
<button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php
echo '<div class="wooac-added-to-cart" style="display: none">';
}
} );

add_action( 'woocommerce_after_add_to_cart_button', function () {
global $product;

if ( wooac_check_in_cart( $product ) ) {
echo '</div><!-- /wooac-added-to-cart -->';
}
} );

add_action( 'wp_enqueue_scripts', function () {
wp_localize_script( 'wooac-frontend', 'wooac_added_vars', [
'added_text' => wooac_added_text(),
'cart_url' => wc_get_cart_url(),
] );
}, 99 );

add_action( 'wp_footer', function () {
?>
<script>
(function($) {
$(document.body).on('added_to_cart', function(e, fragments, cart_hash, $button) {
$button.text(wooac_added_vars.added_text).
removeClass('ajax_add_to_cart').
attr('href', wooac_added_vars.cart_url);
});
})(jQuery);
</script>
<?php
} );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,903
Messages
2,570,002
Members
46,357
Latest member
MittieLin

Latest Threads

Top