gpt4 book ai didi

php - 如何在 Woocommerce 中的自定义产品类型中添加变体选项卡?

转载 作者:可可西里 更新时间:2023-11-01 00:56:08 27 4
gpt4 key购买 nike

  • 我已经为“礼品卡”安装了一个插件,基本上就是添加一个产品类型“礼品卡”。
    • 我需要增强它的功能。
    • 还有其他选项卡可用于此,但“变体”选项卡是不是。
    • 如何添加此选项卡以便我的自定义产品类型也可以表现得像一个可变产品。
    • 屏幕截图:http://prntscr.com/ekogwd
    • 而且我必须严格使用我的自定义产品类型,这意味着我不能将其更改为可变产品类型。
    • 是否有任何 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;

}

您可以通过过滤 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;
}

编辑 您确实需要添加一些 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();
}
});

});

关于php - 如何在 Woocommerce 中的自定义产品类型中添加变体选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42835590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com