对于要在数量输入字段之后添加的woocommerce产品,我有一个自定义字段“ woocommerce_product_rate”,因此它不仅可以显示“ 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 . " ";} ?>
非常感谢您提供有关如何解决此问题的建议。
答案 0 :(得分:0)
直接global $product
或global $post
在全局模板中不起作用。
您需要从 woocommerce / cart / cart.php 文件传递产品ID作为参数,并在其中添加过滤器:
$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']