我正在使用代码来更改添加到购物车的项目的“添加到购物车”按钮的文本和样式。
/* Меняем текст кнопки добавления в корзину для каталога и категорий */
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 = 'В корзине';
}
return $text;
}
/* Меняем текст кнопки добавления в корзину для страницы товара */
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 = 'В корзине';
}
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("В корзине")';
// 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 = __('В корзине', '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
}
代码本身可以正常工作,但是博客中存在问题。创建博客文章时,我使用 WPBakery Page Builder 可视化编辑器。我还在博客文章中添加了一个带有产品的标准 WooCommerce 小部件。
使用标准 WooCommerce 小部件发布或保存帖子时,我收到一个致命错误:
致命错误:未捕获错误:在 /public_html/wp-content/themes/functions.php:703 中调用成员函数 find_product_in_cart() 堆栈跟踪:#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- in /public_html/wp-content/themes/functions.php 在第 703 行
如果没有产品小部件,则帖子发布时不会出现问题。
第 703 行是上述代码的一部分:
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
告诉我可能是什么问题以及如何解决?
这是因为购物车没有在管理面板中初始化,因为这里不需要它。要解决此问题,您有两种选择
不要在管理面板中使用过滤器
用力装车