doulu1867 2017-01-26 10:53
浏览 69
已采纳

Woocommerce可变产品下拉列表

I am trying to implement quantity dropdown for my woocommerce shop, i found the code below to enable for single products but I cannot get it working for products with variations.

<?php
// Place the following code in your theme's functions.php file
// override the quantity input with a dropdown
// Note that you still have to invoke this function like this:
/*
$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'
), $_product, false );
*/
function woocommerce_quantity_input($data) {
    global $product;
  $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; margin-right:10px;', $product )
  );
  if ( ! empty( $defaults['min_value'] ) )
    $min = $defaults['min_value'];
  else $min = 1;
  if ( ! empty( $defaults['max_value'] ) )
    $max = $defaults['max_value'];
  else $max = 20;
  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>';
}
?>

The result i am after is something like the image below

Desired result

Is this possible with woocommerce?

  • 写回答

2条回答 默认 最新

  • drwkqwa82140 2017-02-04 12:58
    关注

    Turns out the best way of doing this was to use grouped products

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题