1

I am using Woocommerce together with the WooThemes Per Product Shipping plugin.

The problem I have is that when using the “Duplicate” a product feature in Woocommerce, the PPS entries are not copied over to the new product.

Can anyone help in what I would need to add to the following file to get this to work? https://github.com/woothemes/woocommerce/blob/master/includes/admin/class-wc-admin-duplicate-product.php

The PPS entries are stored in the following table: http://www.awesomescreenshot.com/image/471866/f815997f78d9f2505919db880ffe35b5

sk15
  • 35
  • 1
  • 4

1 Answers1

1

You can use following code,

add_action( 'woocommerce_duplicate_product', 'wdm_duplicate_pps_entries',10,2);

function wdm_duplicate_pps_entries( $new_id, $post) {
   global $wpdb;
   $id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
   if(!empty($id)) {
   $query = "Select * From " . $wpdb->prefix . "woocommerce_per_product_shipping_rule 
   Where product_id = '" . $id . "'";

   $result = $wpdb->results($query);
   $table_name =  $wpdb->prefix . "woocommerce_per_product_shipping_rule";

   foreach($result as $single_result) {

      $data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);

     $wpdb->insert($table_name,$data);
   }

   }
}
Domain
  • 11,562
  • 3
  • 23
  • 44
  • I tried that code in my theme functions.php file and I get the following error message: Fatal error: Call to undefined method wpdb::results() – sk15 Aug 10 '15 at 20:07
  • Correct method name is, get_results(). Just replace - $result = $wpdb->results($query); with -- $result = $wpdb->get_results($query); Sorry for typo mistake. – Domain Aug 11 '15 at 06:14
  • Thank you very much for your help. – sk15 Aug 11 '15 at 22:11
  • For what it's worth the action is renamed to `woocommerce_product_duplicate` now. – Magnetize Mar 23 '17 at 10:38
  • Hey WisdmLabs, I am having this same issue however the code you have provided does not work for me? If you could help that would be so appreciated! Here is my question - http://stackoverflow.com/questions/43948904/woocommerce-when-duplicating-a-product-it-does-not-copy-per-product-shipping-i – eZ_Harry May 13 '17 at 03:50