0

以下のコードを実行すると、ボタンを押した後、属性「pa_size」と「pa_color」のバリエーションを持つ商品が WooCommerce Products に追加されます。製品をプレビューすると、すべて正常に動作します。しかし、その商品を編集して保存しようとすると、属性が存在しません。その上、製品を再度保存すると、バリエーションが消えます。

少なくとも 3 日間はインターネットを検索したので、誰かが適切な解決策を知っていれば、感謝します。

コード:

class Product {
    function __construct () {
        // In a class constructor
        $this->size_tax = wc_attribute_taxonomy_name( 'Size' );
        $this->color_tax = wc_attribute_taxonomy_name( 'Color' );
    }

    function addProduct() {
        // Insert the main product first
        // It will be used as a parent for other variations (think of it as a container)
        $product_id = wp_insert_post( array(
            'post_title'   => "Product Example",
            'post_content' => "Product post content goes here...",
            'post_status'  => "publish",
            'post_excerpt' => "This is the description",
            'post_name'    => "test_prod_vars2", //name/slug
            'post_type'    => "product"
        ) );
        // Insert the attributes (I will be using size and color for variations)
        $attributes = array(
            $this->size_tax => array(
                'name' => $this->size_tax,
                'value' =>'',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            ),
            $this->color_tax => array(
                'name' => $this->color_tax,
                'value' => '',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
            )
        );
        update_post_meta( $product_id, '_product_attributes', $attributes );
        // Assign sizes and colors to the main product

        // Set product type as variable
        wp_set_object_terms( $product_id, 'variable', 'product_type', false );
        // Start creating variations

        $sizes = array('small', 'medium', 'large');
        $prices = array('5', '10', '15');
        $colors = array('red', 'white', 'blue');

        for($i=0; $i<count($sizes); $i++) {
            for($j=0; $j<count($colors); $j++) {
                $parent_id = $product_id;
                $variation = array(
                    'post_title'   => 'Product #' . $parent_id . ' Variation',
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_parent'  => $parent_id,
                    'post_type'    => 'product_variation'
                );

                // The variation id
                $variation_id = wp_insert_post( $variation );
                update_post_meta( $variation_id, '_price', $prices[$i] );
                // Assign the size and color of this variation
                update_post_meta( $variation_id, 'attribute_' . $this->size_tax, $sizes[$i] );
                update_post_meta( $variation_id, 'attribute_' . $this->color_tax, $colors[$j] );

                // Update parent if variable so price sorting works and stays in sync with the cheapest child
                WC_Product_Variable::sync( $parent_id );
            }
        }
    }
}

if(isset($_POST['button'])) {
   $product = new Product;
   $product->addProduct();
}
4

1 に答える 1