RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1276148
Accepted
Dmitry
Dmitry
Asked:2022-04-29 01:59:27 +0000 UTC2022-04-29 01:59:27 +0000 UTC 2022-04-29 01:59:27 +0000 UTC

将标准 WooCommerce 产品小部件添加到博客文章时出错

  • 772

我正在使用代码来更改添加到购物车的项目的“添加到购物车”按钮的文本和样式。

/* Меняем текст кнопки добавления в корзину для каталога и категорий */
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() ) )

告诉我可能是什么问题以及如何解决?

php
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Darth KYL
    2022-04-29T06:17:46Z2022-04-29T06:17:46Z

    这是因为购物车没有在管理面板中初始化,因为这里不需要它。要解决此问题,您有两种选择

    1. 不要在管理面板中使用过滤器

      add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );
      
      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->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 ( is_admin() ) {
              return $text;
          }
      
          if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( get_the_ID() ) ) ) {
              $text = 'В корзине';
          }
      
          return $text;
      }
      
    2. 用力装车

      add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );
      
      function new_products_button_text( $text, $product ) {
      
          if ( is_admin() && is_null( WC()->cart ) ) {
              wc_load_cart();
          }
      
          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;
      
      }
      
    • 2

相关问题

  • mysqli 类的对象无法转换为字符串

  • 您的系统中缺少 ext-http *,您的系统中缺少 ext-mysql_xdevapi *

  • 如何从csv中删除bom?

  • 当我按下 Enter 键时,如何让 PhpStorm 的 Emmet 插件触发,就像 VS Code 一样?

  • 注释在 Symfony5 中不起作用

  • 搜索最近的地理位置点

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5