【问题标题】:Woocommerce Quantity Dropdown Selector Fatal ErrorWoocommerce 数量下拉选择器致命错误
【发布时间】: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/

购物车页面:http://www.ticketexpress.com.au/cart/

【问题讨论】:

  • 这个函数叫什么?它不应该被称为购物车页面上的方式......
  • 我不知道它只是被调用
  • @Inigo 找到谁打电话给woocommerce_quantity_input 或寻找类似add_action('*some hook here*', 'woocommerce_quantity_input'); 的东西
  • @Reigel 我在很多地方都可以找到,我该怎么处理它?
  • 让我们看看它是什么......顺便说一句,这应该是你的functions.php中我们想要看到的那个......因为在这里调用global $product;是错误的......当我们知道谁在调用这个函数时,我们就会知道......

标签: wordpress woocommerce


【解决方案1】:

我可以看到您正在覆盖 woocommerce 功能。我不会建议做你所做的事情,尤其是在插件上。这很危险。其他插件可能正在使用该功能。您可以使用一个过滤器来更改参数。还有一个用于数量输入的 woocommerce 模板,您可以将其复制到您的主题并进行编辑。这应该是最安全、最正确的做法。

如果您需要更改$default 或给定的$args,请使用woocommerce_quantity_input_args 过滤器...

类似这样的...复制到你的functions.php

add_filter('woocommerce_quantity_input_args','my_quantity_input_args',10,2);
function my_quantity_input_args( $args, $product ){

    // add style to the $default;
    $args['style'] = apply_filters( 'woocommerce_quantity_style', 'float:left;', $product );

    // check for min
    if ( empty( $args['min_value'] ) )
        $args['min_value'] = 1;

    // check for max
    if ( empty( $args['max_value'] ) )
        $args['max_value'] = 8; // I'm not sure where to get your max_qty.

    return $args;
}

然后在您的插件文件夹woocommerce/templates/global/quantity-input.php 中找到quantity-input.php 文件。复制到yourtheme/woocommerce/global/quantity-input.php

进行编辑。删除那里的代码并用下面的代码替换它。接下来的这些代码行将准确地告诉你你现在想要做什么。

  for ( $count = $min_value; $count <= $max_value; $count = $count+$step ) {
    $selected = $count === $input_value ? ' selected' : '';
    $options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
  }

  echo '<div class="quantity_select" style="' . $style . '" ><select name="' . esc_attr( $input_name ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';

【讨论】:

  • 感谢您的回答雷格尔。不幸的是,这不起作用。
  • 为什么投反对票?如果我的回答有问题,您可以发表评论...投反对票它不会做任何事情来纠正错误..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2017-05-04
  • 2015-05-20
  • 2016-04-17
  • 2016-01-29
  • 1970-01-01
相关资源
最近更新 更多