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
} );
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
} );