【问题标题】:Add quantity unit in quantity-input.php for Woocommerce为 Woocommerce 在 quantity-input.php 中添加数量单位
【发布时间】:2019-01-06 08:41:41
【问题描述】:

我有一个自定义字段“woocommerce_product_rate”,用于在数量输入字段之后添加的 woocommerce 产品,因此它不仅可以显示“10”,还可以显示“10 Bananas”。我正在尝试在 quantity-input.php 中检索产品 ID,但遇到了困难。我尝试使用 $product->get_the_id() 通过“$product”全局访问它,但这会导致 php 错误。尝试访问 $post 全局变量也不起作用。

/**
* Product quantity inputs
*/

global $product;
$id = $product->get_id();
$unit = get_field('woocommerce_product_rate', $id);
<?php if( $unit !== '') {echo $unit . " ";} ?>

任何有关如何解决此问题的建议将不胜感激,谢谢。

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

    直接global $productglobal $post 在全局模板中不起作用。 您需要将产品 id 作为参数从 woocommerce/cart/cart.php 文件中传递,在该文件中添加过滤器:

    $product_quantity = woocommerce_quantity_input( array(
                                    'input_name'   => "cart[{$cart_item_key}][qty]",
                                    'input_value'  => $cart_item['quantity'],
                                    'max_value'    => $_product->get_max_purchase_quantity(),
                                    'min_value'    => '0',
                                    'product_name' => $_product->get_name(),
                                    'product_id' => $product_id
                                ), $_product, false );
    
    echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
    

    然后产品 ID 可以在全局模板中访问,在您的情况下 woocommerce/cart/quantity-input.php 您可以使用 $args['product_id'] 访问产品 ID

    【讨论】:

      【解决方案2】:

      我发现你可以使用过滤器woocommerce_quantity_input_args修改数量输入参数:

      <?php 
      
      add_filter("woocommerce_quantity_input_args", function ($args, $product) {
          if (!empty($product)) {
              $args["product_id"] = $product->get_id();
          }
          return $args;
      }, 10, 2);
      
      

      之后,在使用模板文件 quantity-input.php 时,您甚至应该在购物车中定义 $args["product_id"]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        • 2014-10-26
        • 1970-01-01
        • 2021-06-03
        • 2018-06-30
        • 2022-01-20
        • 1970-01-01
        相关资源
        最近更新 更多