Esa*_*smi 3 php wordpress woocommerce hook-woocommerce
我假设您已经将礼品卡添加到产品类型选择器中。但是,我在这里添加它是为了完整性,因为您必须在第二个函数中使用相同的名称。
add_filter( 'product_type_selector', 'so_42835590_add_product_type' );
function so_42835590_add_product_type( $types ){
// Key should be exactly the same as in the class product_type parameter
$types[ 'gift-card' ] = __( 'Gift Card', 'your-plugin' );
return $types;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过过滤添加自己的自定义选项卡,woocommerce_product_data_tabs但您也可以向现有选项卡添加类。类是 metabox javascript 用来决定在产品类型更改时显示什么和隐藏什么的。
add_filter( 'woocommerce_product_data_tabs', 'so_42835590_product_data_tabs' );
function so_42835590_product_data_tabs( $tabs ) {
$product_type = 'gift-card'; // must match array key in previous snippet
// Add an additional class to an existing tab, ex: Variations.
$tabs[ 'variations' ][ 'class' ][] = 'show_if_' . $product_type;
return $tabs;
}
Run Code Online (Sandbox Code Playgroud)
编辑您确实需要添加一些 javascript 来控制现有属性中“启用变体”选项卡的可见性,以及何时添加属性。这应该可以解决问题:
jQuery( function ( $ ) {
// Variable type options are valid for variable workshop.
$( '.show_if_variable:not(.hide_if_gift-card)' ).addClass( 'show_if_gift-card' );
// Trigger change
$( 'select#product-type' ).change();
// Show variable type options when new attribute is added.
$( document.body ).on( 'woocommerce_added_attribute', function(e) {
$( '#product_attributes .show_if_variable:not(.hide_if_gift-card)' ).addClass( 'show_if_gift-card' );
var $attributes = $( '#product_attributes' ).find( '.woocommerce_attribute' );
if ( 'gift-card' == $( 'select#product-type' ).val() ) {
$attributes.find( '.enable_variation' ).show();
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3614 次 |
| 最近记录: |