【问题标题】:Woocommerce Variable products dropdownWoocommerce 可变产品下拉菜单
【发布时间】:2017-06-11 19:58:43
【问题描述】:

我正在尝试为我的 woocommerce 商店实施数量下拉菜单,我发现下面的代码可以为单一产品启用,但我无法让它适用于有变化的产品。

<?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>';
}
?>

我追求的结果如下图所示

woocommerce 可以做到这一点吗?

【问题讨论】:

  • 您的屏幕截图更像是一个组合产品而不是变体......
  • 所以目前只有一个下拉列表被替换,其他的仍然是输入字段?您对上面发布的代码做了什么?
  • 我同意 Reigel 的观点,它看起来有点像分组产品。或者(无耻的自我宣传)Mix and Match Product。虽然两者都不会让你拥有“成人”和“儿童”的头衔。从那里您仍然可以覆盖可插入的 woocommerce_quantity_input() 函数,尽管您为此发布的代码看起来有点过时。

标签: php drop-down-menu woocommerce variations


【解决方案1】:

这是woocommerce_quantity_input()的更新版本

/**
 * Output the quantity input for add to cart forms.
 *
 * @param  array $args Args for the input
 * @param  WC_Product|null $product
 * @param  boolean $echo Whether to return or echo|string
 */
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', '20', $product ),
        'min_value'   => apply_filters( 'woocommerce_quantity_input_min', '0', $product ),
        'step'        => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
        'pattern'     => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
        'inputmode'   => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
    );


    $args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );

    // Set min and max value to empty string if not set.
    $args['min_value'] = isset( $args['min_value'] ) ? $args['min_value'] : '1';
    $args['max_value'] = isset( $args['max_value'] ) ? $args['max_value'] : '20';

    // Apply sanity to min/max args - min cannot be lower than 0
    if ( '' !== $args['min_value'] && is_numeric( $args['min_value'] ) && $args['min_value'] < 0 ) {
        $args['min_value'] = 0; // Cannot be lower than 0
    }

    // Max cannot be lower than 0 or min
    if ( '' !== $args['max_value'] && is_numeric( $args['max_value'] ) ) {
        $args['max_value'] = $args['max_value'] < 0 ? 0 : $args['max_value'];
        $args['max_value'] = $args['max_value'] < $args['min_value'] ? $args['min_value'] : $args['max_value'];
    }

    ob_start();

    $options = '';

    for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step'] ) {
        $options .= '<option value="' . $count . '"'. selected( $count, $args['input_value'], false ) .'>' . $count . '</option>';
    }
    echo '<div class="quantity_select" style="' . $args['style'] . '"><select name="' . esc_attr( $args['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';

    if ( $echo ) {
        echo ob_get_clean();
    } else {
        return ob_get_clean();
    }
}

【讨论】:

    【解决方案2】:

    事实证明,最好的方法是使用分组产品

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-16
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2016-08-14
      • 2017-10-30
      • 2019-09-30
      相关资源
      最近更新 更多