私はWooCommerceストアに2種類の数量入力を実装しようとしています。カートページでは、プラスとマイナスのボタンを使用して値を変更しています。コードは次のとおりです。数量をドロップダウンリストとしてカートに入れるページ
<?php
/**
* Product quantity inputs
*
* This template can be overridden by copying it to yourtheme/woocommerce/global/quantity-input.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if (! defined('ABSPATH')) {
exit; // Exit if accessed directly
}
?>
<div class="quantity">
<button class="qty-minus" type="button" value=""></button>
<input class="qty-input" type="number" step="<?php echo esc_attr($step); ?>" min="<?php echo esc_attr($min_value); ?>" max="<?php echo esc_attr($max_value); ?>" name="<?php echo esc_attr($input_name); ?>" value="<?php echo esc_attr($input_value); ?>" readonly title="<?php echo esc_attr_x('Qty', 'Product quantity input tooltip', 'woocommerce') ?>" class="input-text qty text" size="4" />
<button class="qty-plus" type="button" value=""></button>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.quantity').on('click', '.qty-plus', function(e) {
$input = $(this).prev('input.qty-input');
var val = parseInt($input.val());
$input.val(val+1).change();
});
$('.quantity').on('click', '.qty-minus',
function(e) {
$input = $(this).next('input.qty-input');
var val = parseInt($input.val());
if (val > 0) {
$input.val(val-1).change();
}
});
});
</script>
これは完全に機能しています。ただし、カートページの数量フィールドもこのフォーマットを継承します。代わりに選択可能なドロップダウンリストにしたいと思います。ドロップダウン表示さ
<?php
$defaults = array(
'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),
);
if (! empty($defaults['min_value']))
$min = $defaults['min_value'];
else $min = 1;
if (! empty($defaults['max_value']))
$max = $defaults['max_value'];
else $max = 10;
if (! empty($defaults['step']))
$step = $defaults['step'];
else $step = 1;
?>
<div class="quantity_select">
<select name="<?php echo esc_attr($input_name); ?>" title="<?php _ex('Qty', 'Product quantity input tooltip', 'woocommerce') ?>" class="qty">
<?php
for ($count = $min; $count <= $max; $count = $count+$step) {
if ($count == $input_value)
$selected = ' selected';
else $selected = '';
echo '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
}
?>
と選択可能であるが、私は別の数量を選択し、「更新カート」をクリックした場合、数量は同じまま:私は、カートのページにこのコードを使用してみました。それは変化していない。これどうやってするの?
ありがとうございました!
はい、動作しています。本当にありがとう。私も解決策を思いつきました。誰かがそれを必要とする場合に備えて、追加の回答として追加します。 – Dot123