pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 Woocommerce 中添加新產(chǎn)品時自動添加所有產(chǎn)品屬

Auto add all product attributes when adding a new product in Woocommerce(在 Woocommerce 中添加新產(chǎn)品時自動添加所有產(chǎn)品屬性)
本文介紹了在 Woocommerce 中添加新產(chǎn)品時自動添加所有產(chǎn)品屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我的一個客戶要求根據(jù) Wordpress 的插件 Woocommerce 進行這種奇怪的更改,以便讓事情變得更容易"..

A client of mine requested this weird change based on Wordpress' plugin Woocommerce in order to make things "easier"..

是否有可能自動擁有所有產(chǎn)品屬性在創(chuàng)建產(chǎn)品時添加?

如果屬性中沒有輸入值,是否可以自動禁用在產(chǎn)品頁面上可見"復(fù)選框?

Also is it possible to have the "Visible on product page" checkbox automatically disabled if there is no value inputted in the attribute?

非常感謝任何幫助.

編輯(解釋):

這就是上面解釋的:

推薦答案

這是在創(chuàng)建新產(chǎn)品時自動添加所有現(xiàn)有產(chǎn)品變體 + 術(shù)語的方法.

Here is the way to auto add all existing product variations + terms when creating a new product.

代碼(評論):

add_action( 'save_post', 'auto_add_product_attributes', 50, 3 );
function auto_add_product_attributes( $post_id, $post, $update  ) {

    ## --- Checking --- ##

    if ( $post->post_type != 'product') return; // Only products

    // Exit if it's an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // Exit if it's an update
    if( $update )
        return $post_id;

    // Exit if user is not allowed
    if ( ! current_user_can( 'edit_product', $post_id ) )
        return $post_id;

    ## --- The Settings for your product attributes --- ##

    $visible   = ''; // can be: '' or '1'
    $variation = ''; // can be: '' or '1'

    ## --- The code --- ##

    // Get all existing product attributes
    global $wpdb;
    $attributes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );

    $position   = 0;  // Auto incremented position value starting at '0'
    $data       = array(); // initialising (empty array)

    // Loop through each exiting product attribute
    foreach( $attributes as $attribute ){
        // Get the correct taxonomy for product attributes
        $taxonomy = 'pa_'.$attribute->attribute_name;
        $attribute_id = $attribute->attribute_id;

        // Get all term Ids values for the current product attribute (array)
        $term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));

        // Get an empty instance of the WC_Product_Attribute object
        $product_attribute = new WC_Product_Attribute();

        // Set the related data in the WC_Product_Attribute object
        $product_attribute->set_id( $attribute_id );
        $product_attribute->set_name( $taxonomy );
        $product_attribute->set_options( $term_ids );
        $product_attribute->set_position( $position );
        $product_attribute->set_visible( $visible );
        $product_attribute->set_variation( $variation );

        // Add the product WC_Product_Attribute object in the data array
        $data[$taxonomy] = $product_attribute;

        $position++; // Incrementing position
    }
    // Get an instance of the WC_Product object
    $product = wc_get_product( $post_id );

    // Set the array of WC_Product_Attribute objects in the product
    $product->set_attributes( $data );

    $product->save(); // Save the product
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經(jīng)測試有效.

Code goes in function.php file of your active child theme (or active theme). tested and works.

這篇關(guān)于在 Woocommerce 中添加新產(chǎn)品時自動添加所有產(chǎn)品屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 气体热式流量计-定量控制流量计(空气流量计厂家)-湖北南控仪表科技有限公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 微动开关厂家-东莞市德沃电子科技有限公司 | 福建珂朗雅装饰材料有限公司「官方网站」 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | B2B网站_B2B免费发布信息网站_B2B企业贸易平台 - 企资网 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 技德应用| MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 开云(中国)Kaiyun·官方网站 - 登录入口| 无锡门窗-系统门窗-阳光房-封阳台-断桥铝门窗厂[窗致美] | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 博莱特空压机|博莱特-阿特拉斯独资空压机品牌核心代理商 | R507制冷剂,R22/R152a制冷剂厂家-浙江瀚凯制冷科技有限公司 | 山东成考网-山东成人高考网 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 首页_中夏易经起名网| 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 众能联合-提供高空车_升降机_吊车_挖机等一站工程设备租赁 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 密封圈_泛塞封_格莱圈-[东莞市国昊密封圈科技有限公司]专注密封圈定制生产厂家 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 |