Proof of concept for testing to use CSV uploader in the CYS product creation process
This commit is contained in:
parent
dcbc4dc588
commit
1f90599361
|
@ -0,0 +1,7 @@
|
|||
ID,Type,SKU,Name,Published,Is featured?,Visibility in catalog,Short description,Description,Date sale price starts,Date sale price ends,Tax status,Tax class,In stock?,Stock,Backorders allowed?,Sold individually?,Weight (lbs),Length (in),Width (in),Height (in),Allow customer reviews?,Purchase note,Sale price,Regular price,Categories,Tags,Shipping class,Images,Download limit,Download expiry days,Parent,Grouped products,Upsells,Cross-sells,External URL,Button text,Position,Attribute 1 name,Attribute 1 value(s),Attribute 1 visible,Attribute 1 global,Attribute 2 name,Attribute 2 value(s),Attribute 2 visible,Attribute 2 global,Meta: _headstart_post,Download 1 name,Download 1 URL,Download 2 name,Download 2 URL
|
||||
69,simple,,Leather-Clad Leisure Chair,1,0,visible,,Sit back and relax in this comfy designer chair. High-grain leather and steel frame add luxury to your your leisure.,,,taxable,,1,,0,0,,,,,,,,249,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/table-wood-house-chair-floor-window.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
||||
71,simple,,Vintage Typewriter,1,0,visible,,A hit spy novel or a love letter? Anything you type using this vintage typewriter from the 20s is bound to make a mark.,,,taxable,,1,,0,0,,,,,,,,90,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/writing-typing-keyboard-technology-white-vintage.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
||||
65,simple,,3-Speed Bike,1,0,visible,,Zoom through the streets on this premium 3-speed bike. Manufactured and assembled in Germany in the 80s.,,,taxable,,1,,0,0,,,,,,,,115,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/road-sport-vintage-wheel-retro-old.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
||||
67,simple,,Black and White,1,0,visible,,"This 24"" x 30"" high-quality print just exudes summer. Hang it on the wall and forget about the world outside.",,,taxable,,1,,0,0,,,,,,,,115,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/white-black-black-and-white-photograph-monochrome-photography.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
||||
63,simple,,Hi-Fi Headphones,1,0,visible,,Experience your favorite songs in a new way with these premium hi-fi headphones.,,,taxable,,1,,0,0,,,,,,,,125,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/man-person-music-black-and-white-white-photography.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
||||
27,simple,,Retro Glass Jug (330 ml),1,0,visible,,Thick glass and a classic silhouette make this jug a must-have for any retro-inspired kitchen.,,,taxable,,1,,0,0,,,,,,,,115,,,,https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce/assets/images/pattern-placeholders/drinkware-liquid-tableware-dishware-bottle-fluid.jpg,,,,,,,,,0,,,,,,,,,1,,,,
|
|
|
@ -53,6 +53,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
'prevent_timeouts' => true, // Check memory and time usage and abort if reaching limit.
|
||||
'enclosure' => '"', // The character used to wrap text in the CSV.
|
||||
'escape' => "\0", // PHP uses '\' as the default escape character. This is not RFC-4180 compliant. This disables the escape character.
|
||||
'uploaded_file' => true,
|
||||
);
|
||||
|
||||
$this->params = wp_parse_args( $params, $default_args );
|
||||
|
@ -83,7 +84,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
|||
* Read file.
|
||||
*/
|
||||
protected function read_file() {
|
||||
if ( ! WC_Product_CSV_Importer_Controller::is_file_valid_csv( $this->file ) ) {
|
||||
if ( ! WC_Product_CSV_Importer_Controller::is_file_valid_csv( $this->file, $this->params['uploaded_file'] ) ) {
|
||||
wp_die( esc_html__( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,10 @@ namespace Automattic\WooCommerce\Blocks\AIContent;
|
|||
|
||||
use Automattic\WooCommerce\Blocks\AI\Connection;
|
||||
use WP_Error;
|
||||
|
||||
require_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';
|
||||
require_once WC_ABSPATH . 'includes/admin/importers/class-wc-product-csv-importer-controller.php';
|
||||
|
||||
/**
|
||||
* Pattern Images class.
|
||||
*
|
||||
|
@ -101,6 +105,7 @@ class UpdateProducts {
|
|||
* @return array|WP_Error An array with the dummy products that need to have their content updated by AI.
|
||||
*/
|
||||
public function fetch_dummy_products_to_update() {
|
||||
$use_csv_importer = false;
|
||||
$real_products = $this->fetch_product_ids();
|
||||
$real_products_count = count( $real_products );
|
||||
|
||||
|
@ -110,41 +115,61 @@ class UpdateProducts {
|
|||
);
|
||||
}
|
||||
|
||||
$dummy_products = $this->fetch_product_ids( 'dummy' );
|
||||
$dummy_products_count = count( $dummy_products );
|
||||
$products_to_create = max( 0, 6 - $real_products_count - $dummy_products_count );
|
||||
while ( $products_to_create > 0 ) {
|
||||
$this->create_new_product( self::DUMMY_PRODUCTS[ $products_to_create - 1 ] );
|
||||
--$products_to_create;
|
||||
}
|
||||
if ( $use_csv_importer ) {
|
||||
|
||||
// Identify dummy products that need to have their content updated.
|
||||
$dummy_products_ids = $this->fetch_product_ids( 'dummy' );
|
||||
if ( ! is_array( $dummy_products_ids ) ) {
|
||||
return new \WP_Error( 'failed_to_fetch_dummy_products', __( 'Failed to fetch dummy products.', 'woocommerce' ) );
|
||||
}
|
||||
$params = array(
|
||||
'delimiter' => ',',
|
||||
'start_pos' => 0,
|
||||
'mapping' => array(),
|
||||
'update_existing' => false,
|
||||
'character_encoding' => '',
|
||||
'lines' => 6,
|
||||
'parse' => true,
|
||||
'uploaded_file' => false,
|
||||
);
|
||||
|
||||
$dummy_products = array_map(
|
||||
function ( $product ) {
|
||||
return wc_get_product( $product->ID );
|
||||
},
|
||||
$dummy_products_ids
|
||||
);
|
||||
include_once WC_ABSPATH . 'includes/import/class-wc-product-csv-importer.php';
|
||||
|
||||
$dummy_products_to_update = [];
|
||||
foreach ( $dummy_products as $dummy_product ) {
|
||||
if ( ! $dummy_product instanceof \WC_Product ) {
|
||||
continue;
|
||||
$file = plugins_url( '/assets/cys-dummy-products.csv', WC_PLUGIN_FILE );
|
||||
$importer_class = new \WC_Product_CSV_Importer( $file, $params );
|
||||
return $importer_class->import();
|
||||
} else {
|
||||
$dummy_products = $this->fetch_product_ids( 'dummy' );
|
||||
$dummy_products_count = count( $dummy_products );
|
||||
$products_to_create = max( 0, 6 - $real_products_count - $dummy_products_count );
|
||||
while ( $products_to_create > 0 ) {
|
||||
$this->create_new_product( self::DUMMY_PRODUCTS[ $products_to_create - 1 ] );
|
||||
--$products_to_create;
|
||||
}
|
||||
|
||||
$should_update_dummy_product = $this->should_update_dummy_product( $dummy_product );
|
||||
|
||||
if ( $should_update_dummy_product ) {
|
||||
$dummy_products_to_update[] = $dummy_product;
|
||||
// Identify dummy products that need to have their content updated.
|
||||
$dummy_products_ids = $this->fetch_product_ids( 'dummy' );
|
||||
if ( ! is_array( $dummy_products_ids ) ) {
|
||||
return new \WP_Error( 'failed_to_fetch_dummy_products', __( 'Failed to fetch dummy products.', 'woocommerce' ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $dummy_products_to_update;
|
||||
$dummy_products = array_map(
|
||||
function ( $product ) {
|
||||
return wc_get_product( $product->ID );
|
||||
},
|
||||
$dummy_products_ids
|
||||
);
|
||||
|
||||
$dummy_products_to_update = [];
|
||||
foreach ( $dummy_products as $dummy_product ) {
|
||||
if ( ! $dummy_product instanceof \WC_Product ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$should_update_dummy_product = $this->should_update_dummy_product( $dummy_product );
|
||||
|
||||
if ( $should_update_dummy_product ) {
|
||||
$dummy_products_to_update[] = $dummy_product;
|
||||
}
|
||||
}
|
||||
|
||||
return $dummy_products_to_update;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue