如何在商品数量中增加步长100并记入? WooCommerce
How to add a step of 100 and inscribe in the quantity of goods? WooCommerce
我有一个网站。我编写了一个代码,将“每 100 克”添加到产品价格中(在图片点 2 中)。这是代码:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
function wb_change_product_html( $price ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
//return $a[0]->name;
$temp = 0;
foreach ($cat_arr as $singl_cat) {
if($singl_cat->name == 'perunit') {
$temp = 1;
}
}
if ( $temp ) {
$price_html = '<span class="amount">' . 'per unit \' ' . $price . '</span>';
} else {
$price_html = '<span class="amount">' . ' per 100g \ ' . $price . '</span>';
}
return $price_html;
}
我需要在“添加到购物车的商品数量”中添加题词(在图片点 1 中)。
逻辑是,如果我有一个“每单位”类别,那么默认情况下一切都会保留。但如果没有类别,则在商品数量中添加“每 X 克的价格”字样,其中 X 为克数(最小 100)并以 100 克为步长。
我不知道该怎么做,有人可以建议吗?
这很简单(来自官方文档 https://docs.woocommerce.com/document/adjust-the-quantity-input-values/)- 将其添加到您的 functions.php
/**
* Adjust the quantity input values
*/
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products
function jk_woocommerce_quantity_input_args( $args, $product ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
if( 'perunit' == $cat_arr[0]->name ) {
if ( is_singular( 'product' ) ) {
$args['input_value'] = 100; // Starting value (we only want to affect product pages, not cart)
}
$args['max_value'] = 1000; // Maximum value
$args['min_value'] = 100; // Minimum value
$args['step'] = 100; // Quantity steps
return $args;
}
}
我有一个网站。我编写了一个代码,将“每 100 克”添加到产品价格中(在图片点 2 中)。这是代码:
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
function wb_change_product_html( $price ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
//return $a[0]->name;
$temp = 0;
foreach ($cat_arr as $singl_cat) {
if($singl_cat->name == 'perunit') {
$temp = 1;
}
}
if ( $temp ) {
$price_html = '<span class="amount">' . 'per unit \' ' . $price . '</span>';
} else {
$price_html = '<span class="amount">' . ' per 100g \ ' . $price . '</span>';
}
return $price_html;
}
我需要在“添加到购物车的商品数量”中添加题词(在图片点 1 中)。 逻辑是,如果我有一个“每单位”类别,那么默认情况下一切都会保留。但如果没有类别,则在商品数量中添加“每 X 克的价格”字样,其中 X 为克数(最小 100)并以 100 克为步长。
我不知道该怎么做,有人可以建议吗?
这很简单(来自官方文档 https://docs.woocommerce.com/document/adjust-the-quantity-input-values/)- 将其添加到您的 functions.php
/**
* Adjust the quantity input values
*/
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products
function jk_woocommerce_quantity_input_args( $args, $product ) {
$cat_arr = get_the_terms(get_the_ID(),'product_cat');
if( 'perunit' == $cat_arr[0]->name ) {
if ( is_singular( 'product' ) ) {
$args['input_value'] = 100; // Starting value (we only want to affect product pages, not cart)
}
$args['max_value'] = 1000; // Maximum value
$args['min_value'] = 100; // Minimum value
$args['step'] = 100; // Quantity steps
return $args;
}
}