How do I set sales to zero when copying a product?

When copying a product the sales to date amount is not being reset to zero. How do I do that?

  • Vinod Dalvi

    Hi Andrew,

    Thank you for you reply.

    By default it copies all the product content as it is except product id and product slug.

    To reset the product sales to 0 of copied product try adding the following code in the functions.php file of your child theme.

    global $mp;
    remove_filter( 'admin_action_copy-product', array($mp, 'edit_products_copy_action') );
    
    add_filter( 'admin_action_copy-product', 'custom_edit_products_copy_action' );
    function custom_edit_products_copy_action() {
    
    		$action = 'copy-product';
    		if ((isset($_GET['action'])) && ($_GET['action'] == "copy-product")) {
    
    			$sendback_href = remove_query_arg( array('_wpnonce', 'mp-action', 'post', 'trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
    
    			if (isset($_GET['post']))
    				$product_id = intval($_GET['post']);
    			else
    				wp_redirect($sendback_href);
    
    			if (isset($_GET['post_type']))
    				$post_type = esc_attr($_GET['post_type']);
    			else
    				wp_redirect($sendback_href);
    
    			if ( (!isset($_GET['_wpnonce'])) || !wp_verify_nonce($_GET['_wpnonce'], "{$action}-{$post_type}_{$product_id}") )
    				wp_redirect($sendback_href);
    
    			$product = (array)get_post_to_edit( $product_id );
    			$product['ID'] = 0;	// Zero out the Product ID to force insert of new item
    			$product['post_status'] = 'draft';
    			$product['post_author'] = get_current_user_id();
    
    			$new_product_id = wp_insert_post($product);
    			if (($new_product_id) && (!is_wp_error($$new_product_id))) {
    
    				//If we have the a valid new product ID we copy the product meta...
    				$product_meta_keys = get_post_custom_keys($product_id);
    				if (!empty($product_meta_keys)) {
    					foreach ($product_meta_keys as $meta_key) {
    						if ( $meta_key != 'mp_sales_count' ) {
    						$meta_values = get_post_custom_values($meta_key, $product_id);
    
    						foreach ($meta_values as $meta_value) {
    							$meta_value = maybe_unserialize($meta_value);
    							add_post_meta($new_product_id, $meta_key, $meta_value);
    						}
    						}
    					}
    				}
    
    				// ... thne we copy the product taxonomy terms
    				$product_taxonomies = get_object_taxonomies($post_type);
    				if (!empty($product_taxonomies)) {
    					foreach ($product_taxonomies as $product_taxonomy) {
    						$product_terms = wp_get_object_terms($product_id, $product_taxonomy, array( 'orderby' => 'term_order' ));
    						if (($product_terms) && (count($product_terms))) {
    							$terms = array();
    							foreach($product_terms as $product_term)
    								$terms[] = $product_term->slug;
    						}
    						wp_set_object_terms($new_product_id, $terms, $product_taxonomy);
    					}
    				}
    			}
    		}
    		wp_redirect($sendback_href);
    		die();
    	}

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.