【问题标题】:Change single add to cart text when product is out of stock in WooCommerce当 WooCommerce 中的产品缺货时更改单个添加到购物车的文本
【发布时间】:2019-09-10 10:30:00
【问题描述】:

在 Woocommerce 中,当产品缺货时,对于简单产品和可变产品的产品变体,我尝试将单个产品页面上的添加到购物车文本从“添加到购物车”更改为“缺货”。

我们目前没有使用任何形式的库存管理,因为我们所有的产品都是手工制作的,而且我们确实手动设置了库存状态。

有什么方法可以实现吗?

【问题讨论】:

    标签: php jquery wordpress woocommerce stock


    【解决方案1】:

    试图写成评论,但似乎代码格式不起作用,所以写成答案。

    对于添加到购物车短代码的单一变体,此 hack 会有所帮助。

    // For variation product
        add_filter( 'woocommerce_product_add_to_cart_text', 'product_variation_add_to_cart_text_filter_callback', 20, 2 );
        function product_variation_add_to_cart_text_filter_callback( $button_text, $product ) {
            if ( ! $product->is_in_stock() && $product->is_type( 'variation' ) ) {
                $button_text = __( "Out of stock", "woocommerce" );
            }
    
            return $button_text;
        }
    

    【讨论】:

      【解决方案2】:

      对于除了可变产品之外的所有产品,都非常简单……但是对于可变产品及其变体,它需要更多的代码和javascript/jQuery的使用。

      因此,当当前产品缺货时,即使对于可变产品的选定产品变体,以下代码也会将添加到购物车的文本更改为“缺货”:

      // For all products except variable product
      add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_text_filter_callback', 20, 2 );
      function product_single_add_to_cart_text_filter_callback( $button_text, $product ) {
          if( ! $product->is_in_stock() && ! $product->is_type('variable') ) {
              $button_text = __("Out of stock", "woocommerce");
          }
          return $button_text;
      }
      
      // For all product variations (on a variable product)
      add_action( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button_action_callback', 0 );
      function after_add_to_cart_button_action_callback() {
          global $product;
      
          if( $product->is_type('variable') ) :
      
          $data = [];
      
          // Loop through variation Ids
          foreach( $product->get_visible_children() as $variation_id ){
              $variation = wc_get_product( $variation_id );
              $data[$variation_id] = $variation->is_in_stock();
          }
      
          $outofstock_text = __("Out of Stock", "woocommerce");
          ?>
          <script type="text/javascript">
          jQuery(function($){
              var b = 'button.single_add_to_cart_button',
                  t = $(b).text();
      
              $('form.variations_form').on('show_variation hide_variation found_variation', function(){
                  $.each(<?php echo json_encode($data); ?>, function(j, r){
                      var i = $('input[name="variation_id"]').val();
                      if(j == i && i != 0 && !r ) {
                          $(b).html('<?php echo $outofstock_text; ?>');
                          return false;
                      } else {
                          $(b).html(t);
                      }
                  });
              });
          });
          </script>
          <?php
          endif;
      }
      

      代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

      【讨论】:

      • 感谢您的代码,但它似乎对我不起作用。我已将该代码放入 function.php 但“添加到购物车”按钮变灰且文本未更改。我忘了提到我正在使用使用 Elementor 创建的自定义产品页面。也许这就是为什么这段代码对我不起作用
      • @AwaisAhmed 所以它对你不起作用......在提出问题时,请始终提供所有必要的细节,以避免每个人都浪费时间和精力。我不使用 Elementor,也不会为 Elementor 主题的自定义页面做出回答。
      猜你喜欢
      • 2022-11-13
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多