/** * Creates a duplicate product of this product's data * * @author Jonathan Davis * @since 1.0 * @version 1.2 * * @return void **/ public function duplicate() { $original = $this->id; $this->load_data(); // Load everything $this->id = ''; $this->name = $this->name . ' ' . __('copy', 'Shopp'); $slug = sanitize_title_with_dashes($this->name); $this->slug = wp_unique_post_slug($slug, $this->id, $this->status, ShoppProduct::posttype(), 0); $this->created = ''; $this->modified = ''; $this->status = 'draft'; // Set duplicated product to draft status $this->save(); // Copy prices foreach ($this->prices as $price) { $Price = new ShoppPrice(); $Price->copydata($price); $Price->product = $this->id; $Price->save(); // Copy Price record meta entries $meta = array('donation', 'recurring', 'membership', 'dimensions'); $priceline['settings'] = array(); $settings = array(); foreach ($meta as $name) { if (isset($price->{$name})) { $settings[$name] = $price->{$name}; } } shopp_set_meta($Price->id, 'price', 'settings', $settings); shopp_set_meta($Price->id, 'price', 'options', $price->options); } // Copy taxonomy assignments $terms = array(); $taxonomies = get_object_taxonomies(self::$posttype); $assignments = wp_get_object_terms($original, $taxonomies, array('fields' => 'all_with_object_id')); foreach ($assignments as $term) { // Map WP taxonomy data to object meta if (!isset($term->term_id) || empty($term->term_id)) { continue; } // Skip invalid entries if (!isset($term->taxonomy) || empty($term->taxonomy)) { continue; } // Skip invalid entries if (!isset($terms[$term->taxonomy])) { $terms[$term->taxonomy] = array(); } $terms[$term->taxonomy][] = (int) $term->term_id; } foreach ($terms as $taxonomy => $termlist) { wp_set_object_terms($this->id, $termlist, $taxonomy); } $metadata = array('specs', 'images', 'settings', 'meta'); foreach ($metadata as $metaset) { if (!is_array($this->{$metaset})) { continue; } foreach ($this->{$metaset} as $metaobjects) { if (!is_array($metaobjects)) { $metaobjects = array($metaobjects); } foreach ($metaobjects as $meta) { $ObjectClass = get_class($meta); $Meta = new $ObjectClass(); $Meta->copydata($meta); $Meta->parent = $this->id; $Meta->save(); } } } // Duplicate summary (primarily for summary settings data) $Summary = new ProductSummary($original); $Summary->product = $this->id; $Summary->sold = $Summary->grossed = $Summary->stock = 0; $Summary->save(); // Re-summarize product pricing $this->load_data(array('prices', 'summary')); // Duplicate (WP) post meta data foreach (get_post_custom($original) as $key => $values) { foreach ((array) $values as $value) { add_post_meta($this->id, $key, $value); } } }