woocommerce / woocommerce-product-tables-feature-plugin

Implements new data-stores and moves product data into custom tables, with a new, normalised data structure.
GNU General Public License v3.0
192 stars 32 forks source link

First time add new product then feature image not showing. #139

Closed parmarhardip closed 5 years ago

parmarhardip commented 5 years ago

I have checked after Migration the product data When I create a new product and set the featured image and save the product but feature image is not set on the first time.

parmarhardip commented 5 years ago

I have checked this plugin and I have solved and patch the feature image issue but I have found another issue and issue was the whenever I create a new product and set feature image at that time and just publish the product but feature image has not updated.

I have troubleshot the issue and I have found the Woocommerce is first time save product meta using save( $post_id, $post ) { method.

Currently,

$errors = $product->set_props(
            array(
                'sku'                => isset( $_POST['_sku'] ) ? wc_clean( wp_unslash( $_POST['_sku'] ) ) : null,
                'purchase_note'      => wp_kses_post( wp_unslash( $_POST['_purchase_note'] ) ),
                'downloadable'       => isset( $_POST['_downloadable'] ),
                'virtual'            => isset( $_POST['_virtual'] ),
                'featured'           => isset( $_POST['_featured'] ),
                'catalog_visibility' => wc_clean( wp_unslash( $_POST['_visibility'] ) ),
                'tax_status'         => isset( $_POST['_tax_status'] ) ? wc_clean( wp_unslash( $_POST['_tax_status'] ) ) : null,
                'tax_class'          => isset( $_POST['_tax_class'] ) ? wc_clean( wp_unslash( $_POST['_tax_class'] ) ) : null,
                'weight'             => wc_clean( wp_unslash( $_POST['_weight'] ) ),
                'length'             => wc_clean( wp_unslash( $_POST['_length'] ) ),
                'width'              => wc_clean( wp_unslash( $_POST['_width'] ) ),
                'height'             => wc_clean( wp_unslash( $_POST['_height'] ) ),
                'shipping_class_id'  => absint( wp_unslash( $_POST['product_shipping_class'] ) ),
                'sold_individually'  => ! empty( $_POST['_sold_individually'] ),
                'upsell_ids'         => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['upsell_ids'] ) ) : array(),
                'cross_sell_ids'     => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['crosssell_ids'] ) ) : array(),
                'regular_price'      => wc_clean( wp_unslash( $_POST['_regular_price'] ) ),
                'sale_price'         => wc_clean( wp_unslash( $_POST['_sale_price'] ) ),
                'date_on_sale_from'  => wc_clean( wp_unslash( $_POST['_sale_price_dates_from'] ) ),
                'date_on_sale_to'    => wc_clean( wp_unslash( $_POST['_sale_price_dates_to'] ) ),
                'manage_stock'       => ! empty( $_POST['_manage_stock'] ),
                'backorders'         => isset( $_POST['_backorders'] ) ? wc_clean( wp_unslash( $_POST['_backorders'] ) ) : null,
                'stock_status'       => wc_clean( wp_unslash( $_POST['_stock_status'] ) ),
                'stock_quantity'     => $stock,
                'low_stock_amount'   => wc_stock_amount( wp_unslash( $_POST['_low_stock_amount'] ) ),
                'download_limit'     => '' === $_POST['_download_limit'] ? '' : absint( wp_unslash( $_POST['_download_limit'] ) ),
                'download_expiry'    => '' === $_POST['_download_expiry'] ? '' : absint( wp_unslash( $_POST['_download_expiry'] ) ),
                'downloads'          => self::prepare_downloads(
                    isset( $_POST['_wc_file_names'] ) ? wp_unslash( $_POST['_wc_file_names'] ) : array(),
                    isset( $_POST['_wc_file_urls'] ) ? wp_unslash( $_POST['_wc_file_urls'] ) : array(),
                    isset( $_POST['_wc_file_hashes'] ) ? wp_unslash( $_POST['_wc_file_hashes'] ) : array()
                ),
                'product_url'         => esc_url_raw( wp_unslash( $_POST['_product_url'] ) ),
                'button_text'         => wc_clean( wp_unslash( $_POST['_button_text'] ) ),
                'children'            => 'grouped' === $product_type ? self::prepare_children() : null,
                'reviews_allowed'     => ! empty( $_POST['comment_status'] ) && 'open' === $_POST['comment_status'],
                'attributes'          => $attributes,
                'default_attributes'  => self::prepare_set_attributes( $attributes, 'default_attribute_' ),
            )
        );

After the add 'image_id' fields is working fine,.

$errors = $product->set_props(array(
            'sku'                => isset( $_POST['_sku'] ) ? wc_clean( wp_unslash( $_POST['_sku'] ) ) : null,
            **'image_id'           => ( intval( $_POST['_thumbnail_id'] ) >= 0 ) ? intval( $_POST['_thumbnail_id'] ) : 0,**
            'purchase_note'      => wp_kses_post( wp_unslash( $_POST['_purchase_note'] ) ),
            'downloadable'       => isset( $_POST['_downloadable'] ),
            'virtual'            => isset( $_POST['_virtual'] ),
            'featured'           => isset( $_POST['_featured'] ),
            'catalog_visibility' => wc_clean( wp_unslash( $_POST['_visibility'] ) ),
            'tax_status'         => isset( $_POST['_tax_status'] ) ? wc_clean( wp_unslash( $_POST['_tax_status'] ) ) : null,
            'tax_class'          => isset( $_POST['_tax_class'] ) ? wc_clean( wp_unslash( $_POST['_tax_class'] ) ) : null,
            'weight'             => wc_clean( wp_unslash( $_POST['_weight'] ) ),
            'length'             => wc_clean( wp_unslash( $_POST['_length'] ) ),
            'width'              => wc_clean( wp_unslash( $_POST['_width'] ) ),
            'height'             => wc_clean( wp_unslash( $_POST['_height'] ) ),
            'shipping_class_id'  => absint( wp_unslash( $_POST['product_shipping_class'] ) ),
            'sold_individually'  => ! empty( $_POST['_sold_individually'] ),
            'upsell_ids'         => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['upsell_ids'] ) ) : array(),
            'cross_sell_ids'     => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['crosssell_ids'] ) ) : array(),
            'regular_price'      => wc_clean( wp_unslash( $_POST['_regular_price'] ) ),
            'sale_price'         => wc_clean( wp_unslash( $_POST['_sale_price'] ) ),
            'date_on_sale_from'  => wc_clean( wp_unslash( $_POST['_sale_price_dates_from'] ) ),
            'date_on_sale_to'    => wc_clean( wp_unslash( $_POST['_sale_price_dates_to'] ) ),
            'manage_stock'       => ! empty( $_POST['_manage_stock'] ),
            'backorders'         => isset( $_POST['_backorders'] ) ? wc_clean( wp_unslash( $_POST['_backorders'] ) ) : null,
            'stock_status'       => wc_clean( wp_unslash( $_POST['_stock_status'] ) ),
            'stock_quantity'     => $stock,
            'low_stock_amount'   => wc_stock_amount( wp_unslash( $_POST['_low_stock_amount'] ) ),
            'download_limit'     => '' === $_POST['_download_limit'] ? '' : absint( wp_unslash( $_POST['_download_limit'] ) ),
            'download_expiry'    => '' === $_POST['_download_expiry'] ? '' : absint( wp_unslash( $_POST['_download_expiry'] ) ),
            'downloads'          => self::prepare_downloads(
                isset( $_POST['_wc_file_names'] ) ? wp_unslash( $_POST['_wc_file_names'] ) : array(),
                isset( $_POST['_wc_file_urls'] ) ? wp_unslash( $_POST['_wc_file_urls'] ) : array(),
                isset( $_POST['_wc_file_hashes'] ) ? wp_unslash( $_POST['_wc_file_hashes'] ) : array()
            ),
            'product_url'        => esc_url_raw( wp_unslash( $_POST['_product_url'] ) ),
            'button_text'        => wc_clean( wp_unslash( $_POST['_button_text'] ) ),
            'children'           => 'grouped' === $product_type ? self::prepare_children() : null,
            'reviews_allowed'    => ! empty( $_POST['comment_status'] ) && 'open' === $_POST['comment_status'],
            'attributes'         => $attributes,
            'default_attributes' => self::prepare_set_attributes( $attributes, 'default_attribute_' ),
        ));

Can you please look into this in fresh WordPress and WooCommerce.

Refer the Woocomemrce product meta file: https://github.com/woocommerce/woocommerce/blob/master/includes/admin/meta-boxes/class-wc-meta-box-product-data.php