John Davidson

php - Error adding standard WooCommerce product widget to blog post

0 comments
Message:


I am using a code that changes the text and style of the Add to Cart button for a product added to the cart. (All CSS styles for my theme)


/* Change the text of the add to cart button for the archive and categories */
add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );

function new_products_button_text( $text, $product ) {

if(
$product->is_type( 'simple' )
&& $product->is_purchasable()
&& $product->is_in_stock()
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
) {
$text = 'Product in Cart';
}

return $text;

}

/* Change the add to cart button text for the product page */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'new_single_product_button_text' );

function new_single_product_button_text( $text ) {

if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( get_the_ID() ) ) ) {
$text = 'Product in Cart';
}

return $text;

}

add_action( 'wp_footer', 'action_wp_footer' );
function action_wp_footer() {
?>
<script>
jQuery(document).ready(function($) {
var selector = '.add_to_cart_text:contains("Product in Cart")';

// Selector contains specific text
if ( $( selector ).length > 0 ) {
$( selector ).addClass( 'product-is-added' );
} else {
$( selector ).removeClass( 'product-is-added' );
}

});
</script>
<?php
}

add_action( 'wp_footer', 'ajax_button_text_js_script' );
function ajax_button_text_js_script() {
$text = __('Product in Cart', 'woocommerce');
?>
<script>
jQuery(function($) {
var text = '<?php echo $text; ?>', $this;

$(document.body).on('click', '.ajax_add_to_cart', function(event){
$this = $(this); // Get button jQuery Object and set it in a variable
});

$(document.body).on('added_to_cart', function(event,b,data){
var buttonText = '<span class="add_to_cart_text product-is-added">'+text+'</span><i class="cart-icon pe-7s-cart"></i>';

// Change inner button html (with text) and Change "data-tip" attribute value
$this.html(buttonText).attr('data-tip',text);
});
});
</script>
<?php
}

The code works correctly, but there was a problem with the blogs. In blog posts, I add a standard WooCommerce product widget. I use WPBakery Page Builder, the visual editor.


When publishing or saving a post with a standard WooCommerce widget, I get a critical error:


Fatal error: Uncaught Error: Call to a member function find_product_in_cart() on null in /public_html/wp-content/themes/functions.php:703 Stack trace: #0 /public_html/wp-includes/class-wp-hook.php(292): new_products_button_text('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #1 /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Array) #2 /public_html/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', '\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #3 /public_html/wp-content/themes/cores/nasa-woo-functions.php(393): WC_Product_Simple->add_to_cart_text() #4 /public_html/wp-content/themes/cores/nasa-woo- in /public_html/wp-content/themes/functions.php on line 703


Without a product widget, the post is published without any problems.


Line 703 is part of the above code:


&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )

As I understand it, the cart is not initialized in the admin panel, since it is not needed here.


How can this be fixed?


I will be glad for your help!



My Answer:
>is_type( 'simple' ) ||_ $product->is_type( 'variable' ) ||_ $product->is_type( 'grouped' ) ||_ $product->is_type( 'external' )_ ) {_ _ $text = __( 'Buy Now', 'woocommerce' );_ }_ _ return $text;_}_/* Change the style of the add to cart button for the archive and categories */_add_action( 'wp_head', 'custom_add_to_cart_button_style' );_ _function custom_add_to_cart_button_style() {_ _ ?>_ _
I have tried adding the same code to the functions.php file of my theme, but it does not affect the Add to Cart button in the product widget on the blog post.

Is there a different filter or action hook that I need to use to change the text and style of the Add to Cart button in the product widget on a blog post?

Rate this post

4 of 5 based on 3376 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us