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-jetengine-relations.md
Created March 20, 2024 20:14 — forked from juanlistab/export-jetengine-relations.md
Export JetEngine Relations with WP All Import

How to Export JetEngine Relations

This function will use the default "jet_rel_default" table in JetEngine to look for the post_id of the parent and return its relations from the "child_object_id" column. If you're using a separate table for your relations, you can change the $table_name value.

Instructions:

  • Add a New Field in WP All Export (see https://d.pr/i/OW9dze).
  • Set a name for the column and use "ID" for the field to export (see https://d.pr/i/v5Mq98).
  • Modify the function as needed, paste it in the Function Editor and paste the name of the function in the "Export the value returned by a PHP function" field (see https://d.pr/i/2c6c07).
  • The function will return a list of IDs separated with pipes by default, but if you want to export the titles instead, you need to change the $mode in "function my_export_je_relation ($post_id, $mode = 'id')" to "title".
@trey8611
trey8611 / import-jetengine-relations.md
Created March 20, 2024 20:13 — forked from juanlistab/import-jetengine-relations.md
Import Data to JetEngine Relations

How to Import JetEngine Relations

This snippet will work for One to One, One to Many and Many to Many relationships:

Instructions:

  • Create a "_temp_child" temporary field to store the values of the relations you want to import (see https://d.pr/i/cIooGf). It will accept multiple values separated with commas, and you can use either the ID or the Title.
  • Edit the snippet using the instructions below and paste it in the Function Editor inside WP All Import.
  • The "jet_rel_default" table is the default table used for relations in JetEngine. If you're using a separate table, you'll need to set that table's name instead.
  • You'll need to specify the relationship ID in 'rel_id’; you can find it here (see https://d.pr/i/eSVZ32).
@trey8611
trey8611 / remove-image-sizes-from-url.md
Last active April 17, 2024 17:24
Remove image sizes from image URLs in content

Remove -000x000.ext from image URLs in content by passing the content to a custom PHP function:

[my_fix_images({content[1]})]

Code (save in the Function Editor at All Import › Settings):

function my_fix_img( $img = '' ) {
@trey8611
trey8611 / remove-duplicats-xpath.md
Created July 15, 2022 20:03
[Remove Duplicates via XPath 1.0]

If you have a variations XPath like this:

{variations/variant[*]}
<variations>
  <variant>
 V1
@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 December 22, 2021 20:24
[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 ) ) {