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 / 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'];
@trey8611
trey8611 / import-woodmart-additional-variation-images.md
Last active May 3, 2024 17:45
[WoodMart Additional Variation Images]
function wpai_woodmart_gallery_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) {
	$product = wc_get_product( $post_id );
	if ( ! $product ) return;

	$variationID_first = get_post_meta( $product->get_id(), wpai_woocommerce_add_on\XmlImportWooCommerceService::FIRST_VARIATION, TRUE );
	if ( $product->is_type( 'variation' ) || ! empty( $variationID_first ) ) {
		if ( empty( $variationID_first ) ) {
			$variationID = $product->get_id();
			$parent      = $product->get_parent_id();
@trey8611
trey8611 / phpexcel-object-add-row-to-excel-file.md
Last active November 6, 2021 14:18
[PHPExcel Object - Add row to XLSX file] #wpallimport

This is an example showing how to add a row to an Excel file when it's uploaded to WP All Import. Note that this code won't work in WP All Import's Function Editor, it must be in your child theme's functions.php file or in a Code Snippets plugin.

add_filter( 'wp_all_import_phpexcel_object', 'example_add_row_to_excel_file', 10, 2 );
function example_add_row_to_excel_file( $PHPExcel, $xlsFilePath ) {
	
	// Target the first sheet (because WPAI only references the first sheet; all other sheets are ignored)
	$PHPExcel->setActiveSheetIndex(0);
	
	// Append a new row after the last row in the sheet
@trey8611
trey8611 / php-check-if-category-is-in-mapping-rules.md
Created November 6, 2021 13:55
[Set Post Status based on Category Mapping Rules] #wpallimport

Pass a category to this function to check if it exists in the taxonomy mapping rules. If it does, set post status to 'publish', else set it to 'draft'.

function my_check_cats( $cat ) {
	$import_id = wp_all_import_get_import_id();
	$import    = new PMXI_Import_Record();
	$import->getById( $import_id );
	if ( $import->isEmpty() ) return 'publish';
	
	$mapping_rules = $import->options['tax_mapping']['product_cat'];
@trey8611
trey8611 / custom-column-direct-link-export.md
Created October 29, 2021 21:00
[Custom Columns on Manage Exports Page] Adds a "Direct Link" column with a direct link to the export file.
@trey8611
trey8611 / dynamic-attachment-export-elements.md
Created October 25, 2021 17:31
[Dynamic Attachment Export Elements] Output separate attachment elements per-attachment.

This snippet finds the post with the most attachments in the database, then outputs and populates those in the export columns.

The snippet will need to be adjusted if you want it to only output attachments that are images.

function wpae_output_attachment_headers( $headers, $export_id ) { 
    global $wpdb;
    $sql = "SELECT `ID`, `post_parent`, COUNT(*)
    FROM `wp_posts`
 WHERE `post_type` = 'attachment'
@trey8611
trey8611 / import-svi-variations-gallery-images.md
Created October 19, 2021 20:48
[WP All Import SVI Variations Gallery]