栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在WooCommerce中添加到购物车后,重新标记“添加到购物车”按钮

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在WooCommerce中添加到购物车后,重新标记“添加到购物车”按钮

更新: 在下面,您将找到使此重新标记正常工作的正确条件:

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文件中,或者在任何插件文件中。

经过测试和工作。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号