我使用的代码可以更改添加到购物车的产品的“添加到购物车”按钮的文本和样式。 (我的主题的所有 CSS 样式)
/* 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
}
代码工作正常,但博客存在问题。在博客文章中,我添加了一个标准的 WooCommerce 产品小部件。我使用 WPBakery Page Builder,可视化编辑器。
使用标准 WooCommerce 小部件发布或保存帖子时,我收到严重错误:
fatal error :未捕获错误:在/public_html/wp-content/themes/functions.php:703 中的 null 上调用成员函数 find_product_in_cart():703 堆栈跟踪:#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...', 对象(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...',数组) #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...',对象(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- 在/public_html/wp-content/themes/functions.php 第 703 行
没有产品小部件,发布帖子不会出现任何问题。
第 703 行是上述代码的一部分:
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
据我了解,购物车未在管理面板中初始化,因为这里不需要它。
如何解决这个问题?
我很高兴得到您的帮助!
最佳答案
在特殊情况下或由于插件/主题的组合,您确实可能会遇到错误消息。
解决此问题的方法可能是在执行下一段代码之前添加多项检查,这样就可以防止出现错误消息。
例如,您可以将现有的 new_products_button_text
回调函数替换为:
function new_products_button_text( $text, $product ) {
if ( is_admin() ) return $text;
if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) {
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Find product in cart
if ( $cart->find_product_in_cart( $cart->generate_cart_id( $product->get_id() ) ) ) {
$text = 'Product in Cart';
}
}
}
}
return $text;
}
这可以写得更紧凑,但是通过将条件分散到多个 if 语句中,如果您仍然收到错误消息,您可以快速确定哪里出了问题。
关于php - 将标准 WooCommerce 产品小部件添加到博客文章时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67308711/