我想在 Woocommerce 数量字段中的数字旁边添加包裹文本,但没有成功。
add_action( 'woocommerce_before_add_to_cart_quantity', 'add_text_to_quantity_for_specific_category' );
function add_text_to_quantity_for_specific_category() {
// Specify the category ID here
$category_id = 87;
global $product;
// Get category IDs of the current product
$product_categories = $product->get_category_ids();
// If the product belongs to certain category, add the text
if ( in_array( $category_id, $product_categories ) ) {
echo '<div class="qty-wrapper">';
echo '<div class="qty">Sq Ft</div>';
echo '</div>';
}
}
请您帮帮我,我该怎么做?
我想在 WooCommerce 上做与此领域类似的事情。
<div class="product-box-calcute-left col-lg-6 col-xs-12">
<div class="product-quantity-Box">
<input type="hidden" id="hidquantity" value="9000">
<input type="hidden" name="product_id" value="37120">
<button onclick="return false;" class="decrease btn-number" disabled="disabled" data-type="minus" data-field="product_quantity">-</button>
<input type="number" onkeyup="isNumeric(this)" min="5" max="9000" class="product-basket-quantity input-number" name="product_quantity" id="quantity" value="5">
<span class="product_unit">Adet</span>
<button onclick="return false;" class="increase btn-number" data-type="plus" data-field="product_quantity">+</button>
</div>
</div>
要实现与 WooCommerce 示例中显示的功能类似的功能(其中文本显示在数量输入字段旁边),您可以稍微修改现有代码。具体方法如下:
add_action( 'woocommerce_before_add_to_cart_quantity', 'add_text_to_quantity_for_specific_category' );
function add_text_to_quantity_for_specific_category() {
// Specify the category ID here
$category_id = 87;
global $product;
// Get category IDs of the current product
$product_categories = $product->get_category_ids();
// If the product belongs to certain category, add the text
if ( in_array( $category_id, $product_categories ) ) {
echo '<div class="qty-wrapper">';
woocommerce_quantity_input( array(
'input_value' => 1,
'min_value' => 1,
'max_value' => $product->get_max_purchase_quantity(),
'product_name' => $product->get_name(),
) );
echo '<span class="product_unit">Sq Ft</span>';
echo '</div>';
}
}
此代码将用旁边包含文本“Sq Ft”的自定义输入字段替换默认的 WooCommerce 数量输入字段。根据需要调整类名称和样式以匹配您的主题设计。