按照自定义属性对WooCommerce产品进行排序
创始人
2024-08-26 10:00:13
0

要按照 WooCommerce 产品的自定义属性进行排序,可以使用以下代码示例:

// 注册自定义属性
function custom_attribute_register() {
    global $wpdb;
    $wpdb->query("ALTER TABLE {$wpdb->prefix}woocommerce_attribute_taxonomies ADD `attribute_custom` int(1) DEFAULT 0");
}
 
add_action( 'init', 'custom_attribute_register' );
 
// 显示自定义属性在 WooCommerce 后端
function custom_attribute_type( $types ) {
    $types['custom'] = __( 'Custom', 'woocommerce' );
    return $types;
}
 
add_filter( 'woocommerce_product_attribute_types', 'custom_attribute_type', 99, 1 );
 
// 显示自定义属性选项在 WooCommerce 后端
function custom_attribute_type_options() {
    global $woocommerce, $wpdb;
    $table_name = $wpdb->prefix . 'woocommerce_attribute_taxonomies';
    $attribute_id = absint( get_the_terms( $the_id, 'pa_custom' )[0]->term_id );
 
    $attribute_custom = $wpdb->get_var( "SELECT attribute_custom FROM {$table_name} WHERE attribute_id = {$attribute_id}" );
 
    woocommerce_wp_checkbox(
        array(
            'id'      => 'attribute_custom',
            'label'   => __( 'Custom', 'woocommerce' ),
            'value'   => $attribute_custom,
            'desc_tip' => true,
            'description' => __( 'Enable custom attribute.', 'woocommerce' ),
        )
    );
}
 
add_action( 'woocommerce_after_product_attribute_settings', 'custom_attribute_type_options', 10, 1 );
 
// 保存自定义属性选项
function save_custom_attribute_type_options( $term_id, $i, $name ) {
    global $wpdb;
 
    if ( isset( $_POST['attribute_custom'][ $i ] ) ) {
        $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_attribute_taxonomies SET attribute_custom = 1 WHERE attribute_id = {$term_id}" );
    } else {
        $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_attribute_taxonomies SET attribute_custom = 0 WHERE attribute_id = {$term_id}" );
    }
}
 
add_action( 'woocommerce_attribute_added', 'save_custom_attribute_type_options', 10, 3 );
add_action( 'woocommerce_attribute_updated', 'save_custom_attribute_type_options', 10, 3 );
 
// 对具有自定义属性的产品进行排序
function custom_attribute_sort( $query ) {
    if ( !is_admin() && $query->is_main_query() && is_shop() ) {
        $query->set( 'meta_key', 'pa_custom' );
        $query->set( 'orderby

相关内容

热门资讯

第8分钟辅助!卡五星游戏有挂吗... 第8分钟辅助!卡五星游戏有挂吗,一直存在有辅助神器(了解有挂)1、用户打开应用后不用登录就可以直接使...
第六分钟辅助!随意玩免费辅助器... 第六分钟辅助!随意玩免费辅助器,总是真的是有辅助插件(有挂技术)1、玩家可以在随意玩免费辅助器透视最...
7分钟辅助!广东雀伸辅助器,切... 7分钟辅助!广东雀伸辅助器,切实是真的有辅助工具(有挂细节)1、首先打开广东雀伸辅助器辅助器下载最新...
5分钟辅助!蜀山四川破解版安卓... 5分钟辅助!蜀山四川破解版安卓,其实是真的有辅助技巧(有挂助手)一、蜀山四川破解版安卓游戏安装教程牌...
1分钟辅助!途游手游辅助脚本平... 1分钟辅助!途游手游辅助脚本平台,总是真的有辅助器(有挂工具)1、任何途游手游辅助脚本平台透视是真的...
两分钟辅助!闲娱江西打盾插件,... 两分钟辅助!闲娱江西打盾插件,原来是有辅助工具(揭秘有挂)1、金币登录送、破产送、升级送、活动送。详...
六分钟辅助!财神辅助功能,确实... 六分钟辅助!财神辅助功能,确实有辅助脚本(有挂教程)1、财神辅助功能辅助器安装包、财神辅助功能辅助器...
第6分钟辅助!闲来软件辅助,一... 第6分钟辅助!闲来软件辅助,一贯有辅助脚本(有挂详情)1、每一步都需要思考,不同水平的挑战会更加具有...
第一分钟辅助!摆八张辅助,本来... 第一分钟辅助!摆八张辅助,本来真的有辅助软件(的确有挂)暗藏猫腻,小编详细说明摆八张辅助破解器有用吗...
6分钟辅助!潮汕激k辅助软件,... 您好,潮汕激k辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...