wordpress 自定义 WooCommerce 数量选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14161852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customize WooCommerce Quantity Selector
提问by Tomelower
I'm trying to change the default +/- quantity selector into a drop down menu with a given amount of number options (i.e. 1-10).
我正在尝试将默认的 +/- 数量选择器更改为具有给定数量选项(即 1-10)的下拉菜单。
Anyone know how to accomplish this?
有谁知道如何做到这一点?
Let me know if you'd like me to post any of the code related to this.
如果您希望我发布与此相关的任何代码,请告诉我。
回答by Tyler Wiest
There is a plugin that will do this for you called WooCommerce Advanced Product Quantities, it's free and will allow you to set Minimum, Maximum and Step values for all product quantity inputs. Set rules on a per product, per category/tag or site wide.
有一个插件可以为您执行此操作,称为 WooCommerce 高级产品数量,它是免费的,并且允许您为所有产品数量输入设置最小值、最大值和步长值。针对每个产品、每个类别/标签或站点范围设置规则。
http://wordpress.org/plugins/woocommerce-incremental-product-quantities/
http://wordpress.org/plugins/woocommerce-incremental-product-quantities/
It also works with WooCommerce Thumbnail Input Quantities which will put those quantity boxes in all of your product thumbnail boxes.
它还适用于 WooCommerce 缩略图输入数量,它将把这些数量框放在您所有的产品缩略图框中。
http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/
http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/
Enjoy! Full disclosure, I'm the plugin author.
享受!完全披露,我是插件作者。
回答by seron
I'd like to do this too. So far I found that the quantity markup is generated in woocommerce/templates/single-product/add-to-cart/quantity.php
. You could make a copyof this file in a minimal mirror of the woocommerce/templates
directory structure in your theme folder, e.g. in this case copy it to yourtheme/woocommerce/single-product/add-to-cart
. There you can edit it without altering the plug-in and risk it being overwritten when the plug-in is updated.
我也想做这个。到目前为止,我发现数量标记是在woocommerce/templates/single-product/add-to-cart/quantity.php
. 您可以在主题文件夹中目录结构的最小镜像中制作此文件的副本woocommerce/templates
,例如在这种情况下将其复制到yourtheme/woocommerce/single-product/add-to-cart
. 在那里您可以在不更改插件的情况下对其进行编辑,并且在插件更新时冒着被覆盖的风险。
回答by Nickfmc
I have not tried this, but I found this code http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html
我没有试过这个,但我发现这个代码http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html
/* Change Product Quantity Input to Dropdown */
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 ),
'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){
$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>';
}
回答by WisdmLabs
You need to override the template "quantity-input.php" to do so add a file with the name as "quantity-input.php" in your-theme/woocommerce/global/ folder then you can make the changes in that file, now wordpress will use your file to display quantity input html.
您需要覆盖模板“quantity-input.php”以在 your-theme/woocommerce/global/ 文件夹中添加一个名为“quantity-input.php”的文件,然后您可以在该文件中进行更改,现在 wordpress 将使用您的文件来显示数量输入 html。