Skip to content

Instantly share code, notes, and snippets.

View trey8611's full-sized avatar
💭
listening to n*sync

Trey trey8611

💭
listening to n*sync
View GitHub Profile
@trey8611
trey8611 / chain-imports-exports-together.md
Created March 12, 2022 14:59
[Chain Cron Imports / Exports Together] Sequential Cron Imports/Exports

Cron Job Imports: http://www.wpallimport.com/documentation/recurring/cron/.

If you want to run imports or exports sequentially, you can use this workaround:

  • Set up the "trigger" cron job and "processing" cron job for the import/export that should run first.
  • Set up "processing" cron jobs for all of the imports/exports that are going to run.
  • Modify the following code by adding your own URL and import/export IDs then save the code inside the Function Editor:
function my_run_cron_after_import( $import_id ) {
@trey8611
trey8611 / import-woocommerce-rest-api-feed.md
Created February 21, 2022 19:21
[Import WooCommerce Rest API Product Feed] #wpallimport #woocommerce #restapi

This function can be saved in your child theme's functions.php file, or in a code snippets plugin ( https://wordpress.org/plugins/code-snippets/ ), then used in WP All Import to download a product feed via the WooCommerce REST API. Things that need to be/can be changed are:

  • In the function call, change "100" to the amount of product you want per-page request.
  • In the code, change the $max_pages value from 3 to however many pages you want to fetch.
  • In the code, change the $key and $sec (secret value) values to the correct ones from your WooCo API key details.

Usage (in the Download a file › From URL field in WP All Import):

[my_fetch_products("https://example.com/wp-json/wc/v3/products?per_page=100","json")]
@trey8611
trey8611 / import-variation-swatches.md
Created February 18, 2022 21:06
[WooCommerce Variation Swatches and Photos] #wpallimport #woocommerce #swatches

This is an example snippet that imports swatches added by this plugin: https://woocommerce.com/products/variation-swatches-and-photos/. This example only works with the variation attribute "Colour" and it uses the images uploaded to the variations as the swatch images. It is highly likely that you will need to modify it for your specific use case.

add_action( 'wp_all_import_variable_product_imported', 'my_set_swatches', 10, 1 );


function my_set_swatches( $id ) {
	$product = wc_get_product( $id );
	if ( ! $product ) return;
	if ( ! $product->is_type( 'variable' ) ) return;
@trey8611
trey8611 / upload-embedded-media.md
Created January 8, 2022 18:58
[Upload embedded media to WordPress Media Library] #phpexcel

This snippet uploads all images embedded in the import file to the WordPress Media Library. Hat tip to https://stackoverflow.com/a/10910199 for the code inspiration.

add_filter( 'wp_all_import_phpexcel_object', 'wpai_wp_all_import_phpexcel_object', 10, 2 );

function wpai_wp_all_import_phpexcel_object( $objPHPExcel, $xlsFilePath ) {
    $i = 0;
    foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
        if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
            ob_start();
@trey8611
trey8611 / wpai-add-custom-wooco-webhook.md
Last active July 21, 2025 12:04
[Custom Product Imported Webhook] Adds "Product Imported" as a new WooCommerce webhook #wp-all-import #woocommerce

This adds a webhook to WooCommerce that fires when a product is newly created via WP All Import. It is currently barely tested and may need adjusted for production sites. Note that the code needs to be saved in your child theme's functions.php file, or in a code snippets plugin.

Hat tip to this thread for the code inspiration: https://gist.github.com/jessepearson/66a0e72706b99c15b52dee7ce59e1d31.

if ( ! function_exists( 'wpai_add_custom_webhook_filters_and_actions' ) && ! function_exists( 'wpai_add_custom_webhook_topic' ) && ! function_exists( 'wpai_add_custom_product_imported_event' ) && ! function_exists( 'wpai_add_custom_webhook_topics_admin_menu' ) && ! function_exists( 'wpai_add_product_imported_callback' ) ) {

	function wpai_add_custom_webhook_filters_and_actions() {
		add_filter( 'woocommerce_webhook_topic_hooks', 'wpai_add_custom_webhook_topic', 30, 2 );
		add_filter( 'woocommerce_valid_webhook_events', 'wpai_add_custom_product_imported_event',
@trey8611
trey8611 / find-woocommerce-attributes-by-name.md
Last active April 17, 2024 17:24
[Find WooCommerce Attributes by Name] Search by name instead of slug only. #wpallimport #woocommerce

Find attributes by name.

function my_find_attribute_by_name( $name, $attribute ) {
    global $wpdb;
    $term_table = $wpdb->prefix . 'terms';
    $taxonomy_table = $wpdb->prefix . 'term_taxonomy';

    if ( ! empty( $name ) ) {
@trey8611
trey8611 / export-parent-sku-if-blank-sku.md
Created November 26, 2021 16:27
[Export Parent SKU if Variation SKU is blank] #wpallimport

This function should be used on a new instance (click "Add Field") of the export element containing the product ID.

function my_get_sku( $id ) {
	$prod = wc_get_product( $id );
	if ( ! $prod ) return;
	
	if ( ! empty( $prod->get_sku() ) ) {
		return $prod->get_sku();
	}
@trey8611
trey8611 / delete-empty-taxonomy-terms-on-post-delete.md
Created November 24, 2021 15:06
[Delete empty taxonomy terms on post delete] #wpallimport
// Delete empty taxonomies on post delete
function wpai_remove_taxonomies_on_delete( $post_id, $import ) {
    $taxonomy = 'product_cat'; // change this to the taxonomy name
    $terms = wp_get_object_terms( $post_id, $taxonomy );

    if ( ! empty( $terms ) ) {
        $all_terms = array();
        foreach ( $terms as $term ) {
 if ( $term->count < 2 ) {
@trey8611
trey8611 / custom-log-functions.md
Created November 19, 2021 16:28
[WP All Import] Custom log / custom logging functions with our API

This shows how to save a custom log of imported items. It can be adjusted to include any product data.

  /***********************************************/
 /***** BEGIN LOG CREATE / UPDATE / DELETE  *****/
/********************************************* */

// This creates a custom log file in the root WordPress uploads folder
// Some of this code will need to be adjusted, please read comments.
@trey8611
trey8611 / export-wpai-tax-mapping-rules.md
Created November 17, 2021 21:31
[WP All Import - Export taxonomy mapping rules]

This snippet will export a file named "import_rules_{import_id}.csv" in your WordPress uploads folder with your taxonomy mapping rules. Be sure to change 'product_cat' to the appropriate taxonomy name if you're not exporting WooCommerce Product Category rules.

function my_export_maprules( $import_id ) {
    $import = new PMXI_Import_Record();
    $import->getById( $import_id );
    if ( $import->isEmpty() ) return false;
    
    $options = maybe_unserialize( $import->options );
    $rules   = $options['tax_mapping']['product_cat'];