我正在尝试在 woocomerce 中的数量框后嵌入单位。这可以在产品页面上轻松实现。为了实现这一点,我使用了 post meta box 并在 woocommerce 产品显示文件( wp-content\plugins\woocommerce\templates\global\quantity-input.php )中使用以下代码调用它。
<?php echo get_post_meta(get_the_ID(), 'wc_price_per_unit_key', true);?>
现在我也想在购物车页面上显示它。它在那里不起作用,因为 get_the_id() 在那里不起作用。所以我尝试从 cart.php 页面传递帖子 ID。
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
'min_value' => '0',
'id' => $product_id,
), $_product, false );
我通过这段代码添加了id
元素。woocommerce_quantity_input
function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
if ( is_null( $product ) ) {
$product = $GLOBALS['product'];
}
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'id' => '1'
);
if ( is_null ( $args['id'] ) ) {
echo 'asd';
}
最后,我echo 'asd'
检查了id
参数是否在woocommerce_quantity_input
函数中,但asd
打印证明数组没有读取元素。
解决方案发现
我正在编辑其中的 cart.php 文件,该文件wp-content\plugins\woocommerce\templates\cart
被\wp-content\themes\testing\woocommerce\cart
. 编辑正确的购物车文件后问题解决。
新问题
任何人都可以指导我如何在自定义插件方法中实现这一点。这就是我在 woocomerce 文件中进行的硬代码编辑。是否可以通过任何过滤器将代码放入 cart.php 文件等等。