首页 文章

在Woocommerce中添加新产品时自动添加所有产品属性

提问于
浏览
2

我的一个客户要求基于Wordpress的插件Woocommerce进行这种奇怪的改变,以使事情变得“更容易” .

在创建产品时,是否有可能自动添加所有产品属性?

如果属性中没有输入值,也可以自动禁用“产品页面上的可见”复选框吗?

任何帮助将非常感谢 .


Edit (explanation):

这就是上面解释的:
This is what was explained above :)

2 回答

  • 0

    如果有人正在使用@LoicTheAztec中的代码段,则设置另一个数组项可能会有所帮助 .

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

    希望它可以帮助任何人 .

  • 1

    以下是在创建新产品时自动添加所有现有产品变体术语的方法 .

    代码(评论):

    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文件中 . 测试和工作 .

相关问题