Skip to content

Instantly share code, notes, and snippets.

@waylay
Created September 25, 2017 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylay/33194b2d89baf562d5465f307da3fc7a to your computer and use it in GitHub Desktop.
Save waylay/33194b2d89baf562d5465f307da3fc7a to your computer and use it in GitHub Desktop.
Map Magento attachment file to WooCommerce product file
<?php
//SQL
$mydb = new wpdb('root','root','magento','localhost');
$rows = $mydb->get_results("
select p.entity_id, p.sku, f.title, f.uploaded_file
from pml_uni_fileuploader f
inner join pml_catalog_product_entity p
on find_in_set(p.entity_id, f.product_ids)
order by p.entity_id
");
echo count($rows);
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->entity_id." - ".$obj->sku." - ".$obj->title." - ".str_replace('custom/upload/','',$obj->uploaded_file)."</li>";
$product_id = wc_get_product_id_by_sku($obj->sku);
$file_url = 'http://site.dev/wp-content/uploads/woocommerce_uploads/'.str_replace('custom/upload/','',$obj->uploaded_file);
$woo_files[$product_id][md5( $file_url )] = array(
'name' => $obj->title,
'file' => $file_url
);
update_post_meta( $product_id, '_downloadable', 'yes');
update_post_meta( $product_id, '_downloadable_files', $woo_files[$product_id]);
update_post_meta( $product_id, '_download_limit', '-1');
update_post_meta( $product_id, '_download_expiry', '-1');
echo "Product: ".$product_id ." updated!";
endforeach;
echo "</ul>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment