首页 >后端开发 >php教程 >如何以编程方式创建具有新属性值的 WooCommerce 产品变体?

如何以编程方式创建具有新属性值的 WooCommerce 产品变体?

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-11-10 11:36:02465浏览

How to Create a WooCommerce Product Variation with New Attribute Values Programmatically?

如何以编程方式创建具有新属性值的 WooCommerce 产品变体

简介

WooCommerce允许您创建具有不同属性和值的可变产品。要以编程方式添加新变体,您需要考虑以下事项:

  • 确保变量产品(父级)具有所需的属性集。
  • 对于每个变体,创建新的属性值或设置它(如果已存在)。

创建产品的自定义函数变体

以下自定义函数将为给定的变量产品 ID 创建产品变体:

function create_product_variation( $product_id, $variation_data ){
    // Get Variable product object (parent)
    $product = wc_get_product($product_id);

    // Create variation post data
    $variation_post = array(
        'post_title'    => $product->get_name(),
        'post_name'     => 'product-'.$product_id.'-variation',
        'post_status'   => 'publish',
        'post_parent'   => $product_id,
        'post_type'     => 'product_variation',
        'guid'          => $product->get_permalink()
    );

    // Insert variation post and create WC_Product_Variation object
    $variation_id = wp_insert_post( $variation_post );
    $variation = new WC_Product_Variation( $variation_id );

    // Iterate through variation attributes
    foreach ($variation_data['attributes'] as $attribute => $term_name )
    {
        $taxonomy = 'pa_'.$attribute; // Attribute taxonomy

        // Create taxonomy if doesn't exist
        if( ! taxonomy_exists( $taxonomy ) )
            register_taxonomy(
                $taxonomy,
                'product_variation',
                array(
                    'hierarchical' => false,
                    'label'        => ucfirst( $attribute ),
                    'query_var'    => true,
                    'rewrite'      => array( 'slug' => sanitize_title($attribute) ),
                ),
            );

        // Check if term exists and create if not
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create term

        $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get term slug

        // Check if post term exists and set if not
        $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );
        if( ! in_array( $term_name, $post_term_names ) )
            wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

        // Set attribute data in variation
        update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
    }

    // Set other variation data
    if( ! empty( $variation_data['sku'] ) )
        $variation->set_sku( $variation_data['sku'] );

    if( empty( $variation_data['sale_price'] ) ){
        $variation->set_price( $variation_data['regular_price'] );
    } else {
        $variation->set_price( $variation_data['sale_price'] );
        $variation->set_sale_price( $variation_data['sale_price'] );
    }
    $variation->set_regular_price( $variation_data['regular_price'] );

    if( ! empty($variation_data['stock_qty']) ){
        $variation->set_stock_quantity( $variation_data['stock_qty'] );
        $variation->set_manage_stock(true);
        $variation->set_stock_status('');
    } else {
        $variation->set_manage_stock(false);
    }
    
    $variation->set_weight(''); // Reset weight

    $variation->save();
}

示例用法

对于具有“尺寸”和“颜色”属性的可变产品,您可以创建一个变体:如下:

$parent_id = 746; // Variable product ID

$variation_data = array(
    'attributes' => array(
        'size'  => 'M',
        'color' => 'Green',
    ),
    'sku'           => '',
    'regular_price' => '22.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

create_product_variation( $parent_id, $variation_data );

这会将具有指定属性和数据的变体添加到变量产品中。

以上是如何以编程方式创建具有新属性值的 WooCommerce 产品变体?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn