Woocommerce变化下拉列表为空

时间:2019-06-26 20:13:43

标签: wordpress woocommerce attributes frontend variations

我有一个使用woocommerce的独特项目。我使用强大的表单创建了一个表单,以从前端添加变量产品。与下面的代码配合良好。我创建了几个属性。如果属性不是空白,则它们将作为变体下拉选项正确显示。

我的问题是属性(例如“颜色”)为空且未提交时。仍会创建变体“颜色”(Color)属性,其值为空白,并且下拉列表为空。我一直在寻找解决该问题的方法。如果未提交变体/属性,如何不创建它。

如果自定义字段值为空,我还尝试将set_variation更改为''。但这没有用。

代码在这里找到: Auto add all product attributes when adding a new product in Woocommerce

add_action( 'save_post', 'create_product_attributes_variations', 80, 3 );
function create_product_attributes_variations( $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;

    ## -- Set product as variable and set quote option --- ##
    update_post_meta( $post_id, 'qwc_enable_quotes', 'on');
    wp_set_object_terms( $post_id, 'variable', 'product_type', false );



        ## --- The Settings for your product attributes --- ##

    $visible   = ''; // can be: '' or '1'
    $variation = '1'; // 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;



       $term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));
       $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 );

  //Save main product to get its id
    $id = $product->save();

    ///////
    $variation = new WC_Product_Variation();
    $variation->set_regular_price(5);
    $variation->set_parent_id($id);

//Set attributes requires a key/value containing
// tax and term slug
     $variation->set_attributes(array(
       ));

//Save variation, returns variation id
$variation->save();
}

0 个答案:

没有答案