在Woocommerce中添加新产品时自动添加所有产品属性 [英] Auto add all product attributes when adding a new product in Woocommerce

查看:383
本文介绍了在Woocommerce中添加新产品时自动添加所有产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个客户要求基于Wordpress的插件Woocommerce进行此奇怪的更改,以使事情更轻松".

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

是否可以通过某种方式自动拥有所有产品属性 是在创建产品时添加的?

Is it somehow possible to have all product attributes automatically added when a product is created?

如果在属性中没有输入任何值,是否还可以自动禁用在产品页面上可见"复选框?

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

任何帮助将不胜感激.

编辑(解释):

这是上面所解释的:

This is what was explained above :

推荐答案

这里是创建新产品时自动添加所有现有产品变体和条款的方法.

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文件中.经过测试并可以正常工作.

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

这篇关于在Woocommerce中添加新产品时自动添加所有产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆