【发布时间】:2023-03-29 13:21:02
【问题描述】:
我正在使用 WooCommerce 开发在线商店。我已经设法在函数文件中使用此代码显示数量选择器:
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$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 ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
} else {
$defaults = array(
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'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 ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
}
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
foreach ($product->get_available_variations() as $key) {
if ( ! empty( $defaults['max_value'] ) )
$max = $key['max_qty'];
else $max = $key['max_qty'];
}
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
它在单个产品页面下完美运行,但问题出现在购物车页面上,出现此错误:
致命错误:在第 240 行的 functions.php 中的非对象上调用成员函数 get_available_variations() -> foreach ($product->get_available_variations() as $key) {
有谁知道我该如何解决这个问题?
单品:http://www.ticketexpress.com.au/product/hong-kong-sevens-friday-tickets/
【问题讨论】:
-
这个函数叫什么?它不应该被称为购物车页面上的方式......
-
我不知道它只是被调用
-
@Inigo 找到谁打电话给
woocommerce_quantity_input或寻找类似add_action('*some hook here*', 'woocommerce_quantity_input');的东西 -
@Reigel 我在很多地方都可以找到,我该怎么处理它?
-
让我们看看它是什么......顺便说一句,这应该是你的functions.php中我们想要看到的那个......因为在这里调用
global $product;是错误的......当我们知道谁在调用这个函数时,我们就会知道......
标签: wordpress woocommerce