更新: 在下面,您将找到使此重新标记正常工作的正确条件:
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );function customizing_add_to_cart_button_text( $button_text, $product ){ $is_in_cart = false; foreach ( WC()->cart->get_cart() as $cart_item ) if ( $cart_item['product_id'] == $product->get_id() ) {$is_in_cart = true;break; } if( $is_in_cart ) $button_text = __( 'Add one more to cart', 'woocommerce' ); return $button_text;}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
经过测试和工作。
如果启用了Ajax,则对于归档页面 (例如商店页面或产品类别页面) 上的NON可变产品, 并且想要获得此实时事件,也应该添加以下内容:
add_action('wp_footer','custom_jquery_add_to_cart_script');function custom_jquery_add_to_cart_script(){ if ( is_shop() || is_product_category() || is_product_tag() ): // only for archives pages $new_text = __( 'Add one more to cart', 'woocommerce' ); ?> <script type="text/javascript"> // Ready state (function($){ $('a.add_to_cart_button').click( function(){ $this = $(this); $( document.body ).on( 'added_to_cart', function(){ $($this).text('<?php echo $new_text; ?>'); console.log('EVENT: added_to_cart'); }); }); })(jQuery); // "jQuery" Working with WP (added the $ alias as argument) </script> <?php endif;}
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
经过测试和工作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)