Vir*_*rik 5 php wordpress coupon woocommerce product-variations
如何隐藏产品页面下拉列表中的变体,但仍允许通过 WooCommerce URL 优惠券购买?
如果我将变体设置为不活动,则它会从下拉列表中隐藏,但我会在购物车中收到消息“无法购买此产品”。我只是想将其从列表中隐藏,而不是完全禁用它。
以下解决方案适用于我的主题,但您正在运行 Bootstrap,因此可能会遇到问题。
我们将修改option
您想要使用该属性隐藏的选项的标签hidden
。获取以下代码并将其添加到您的主题functions.php
或自定义插件中:
自定义代码
function custom_woocommerce_dropdown_variation_attribute_options_html( $html, $args )
{
$product = $args[ 'product' ];
$attribute = $args[ 'attribute' ];
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
$options = $args[ 'options' ];
if ( empty( $options ) && !empty( $product ) && !empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) && ***SOME CONDITION***) {
$html = str_replace( '<option value="' . esc_attr( $term->slug ) . '" ', '<option hidden value="' . esc_attr( $term->slug ) . '" ', $html );
}
}
return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'custom_woocommerce_dropdown_variation_attribute_options_html', 10, 2 );
Run Code Online (Sandbox Code Playgroud)
请注意,某些浏览器无法识别该hidden
属性。如果您想要完全的跨浏览器兼容性,您需要查看如何使用 CSS 在 <select> 菜单中隐藏 <option> 中的答案?。添加 css 属性style="display:none"
也可能适用于某些浏览器。
高级自定义字段
现在,在上面的代码中,我已经编写了***SOME CONDITION***
. 此条件需要检查是否应隐藏选项。要添加此信息,我们需要为该属性创建一个自定义字段。您可以手动执行此操作,但我使用高级自定义字段插件 (ACF) 来执行此操作。
Taxonomy Term
is equal to
Product **your attribute**
。***SOME CONDITION***
其删除并替换为get_field( 'hidden', $term ) )
. 这是一个 ACF 函数,它将获取该属性的 tern 的“隐藏”字段的值。毕竟,您勾选为隐藏的术语不应出现在产品页面的下拉列表中。在我的示例中,您可以看到下拉列表中缺少绿色。
归档时间: |
|
查看次数: |
16430 次 |
最近记录: |