首页 文章

WooCommerce没有添加任何属性,变化也消失了

提问于
浏览
0

当我执行下面的代码时,在按下按钮后,将向WooCommerce Products添加一个产品,其中包含属性'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();
}

1 回答

  • 0

    如果您的属性'is_taxonomy' => '1',那么您必须插入分类法slug而不是基于文本的值 . 尝试'is_taxonomy' => '0'作为快速检查 .

相关问题