下拉菜单中的WooCommerce数量,而不是经典

时间:2019-02-09 02:02:44

标签: php wordpress woocommerce

我有此代码,该代码为我提供了WooCommerce中“添加到卡中”部分中“数量”的下拉菜单:

function woocommerce_quantity_input() {
global $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 ),
    'contact' => apply_filters('woocommerce_quantity_input_max', 'Contact'),
    '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 = 6;
if ( ! empty( $defaults['step'] ) )
    $step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
    $options .= '<option value="' . $count . '">' . $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>';
}

这给出了类似的内容,请参见以下屏幕截图:https://prnt.sc/mima4f

现在我需要在数字6下方输入“与我们联系”之类的文字。您能向我解释一下我该怎么做。

谢谢

2 个答案:

答案 0 :(得分:0)

use this code in functions.php:

function woocommerce_quantity_input( $args = array(), $product = null, $echo = true )         
{
if ( is_null( $product ) ) {
$product = $GLOBALS['product'];
}
$defaults = array(
'input_id'    => uniqid( 'quantity_' ),
'input_name'  => 'quantity',
'input_value' => '1',
'max_value'   => apply_filters( 'woocommerce_quantity_input_max', -1, $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 );

// Apply sanity to min/max args - min cannot be lower than 0.
$args['min_value'] = max( $args['min_value'], 0 );
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : '';

// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}

$options = '';

 for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count + 
 $args['step'] ) {

// Cart item quantity defined?
if ( '' !== $args['input_value'] && $args['input_value'] > 1 && $count == 
$args['input_value'] ) {
    $selected = 'selected';     
} else $selected = '';

$options .= '<option value="' . $count . '"' . $selected . '>' . $count . 
'</option>';

}

$string = '<div class="quantity"><span>Qty</span><select name="' . 
$args['input_name'] . '">' . $options . '</select></div>';

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

}

答案 1 :(得分:0)

我找到了类似的解决方案。为我工作。

function woocommerce_quantity_input() {
global $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 ),
    'contact' => apply_filters('woocommerce_quantity_input_max', 'Contact'),
    '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 = 6;
if ( ! empty( $defaults['step'] ) )
    $step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max + 1; $count = $count+$step ) {
    if($count <= $max){
        $options .= '<option value="' . $count . '">' . $count . '</option>';
    }else{
        $options .= '<option value="contact-us">Contact us</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>';
}