wordpress WooCommerce没有添加属性和变化消失

vojdkbi0  于 2022-11-22  发布在  WordPress
关注(0)|答案(1)|浏览(166)

当我执行下面的代码时,在我按下按钮后,WooCommerce产品中会添加一个带有'pa_size'和'pa_color'属性变量的产品。当我预览产品时,一切正常。但是,当我编辑并保存该产品时,没有属性存在。最重要的是,当我再次保存该产品时,变量会消失。
我已经在网上搜索了至少三天,所以如果有人知道一个适当的解决方案,我将不胜感激。
编码:

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();
}
ivqmmu1c

ivqmmu1c1#

如果您的属性*'is_taxonomy' =〉'1',则您必须插入taxonomy slug,而不是基于文本的值。请尝试'is_taxonomy' =〉'0'*作为快速检查。

相关问题