Woocommerce Product Samples Plugin Fix – Solved

Artificial Grass Direct has been using wordpress from day one and I have built this website myself with no help at all. As we supply free samples it is important for us to offer this service to our expanding customer base.

We have been using a plugin call Woocommerce Sample. Its been a great pluging and over the years it has developed errors and not working properly.

We have managed to alter the coding and confirm that it is now working and tested to WordPress 5.0.3 and Woocommerce 3.5.3 as you can see in our store.

Here is the coding that needs to be changed or see below for edited plugin file download.

Find this code in /wp-content/plugins/woocommerce-sample/woocommerce-sample.php

      function filter_session($cart_content, $value, $key){
      	      if ($value['sample']){
      	      	      $cart_content['sample'] = true;
      	      	      $cart_content['unique_key'] = $value['unique_key'];
      	      	      //$cart_content['data']->price = 0;
					  $product_id = $cart_content['product_id'];
					  $sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
					  $sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
					  if ($sample_price_mode === 'custom'){
					  	$cart_content['data']->price = $sample_price;
					  }else if ($sample_price_mode === 'free'){
					  	$cart_content['data']->price = 0;
					  }else{
					  	//default
					  }
      	      }
      	      return $cart_content;
      }
      
      function get_item_data($item_data, $cart_item){
      	      global $cart_item_key;
      	      return $item_data;
      }

      function add_sample_to_cart_item_data ($cart_item_data, $product_id, $variation_id){
      	      if (get_post_meta($product_id, 'sample_enamble') && $_REQUEST['sample']){
					$cart_item_data['sample'] = true;
					$cart_item_data['unique_key'] = md5($product_id . 'sample');
      	      }
      	      return $cart_item_data;
      }

	function add_sample_to_cart_item ($cart_item, $cart_item_key){
		if ($cart_item['sample'] === true){
			$cart_item['data']->price = 0;
		}
		return $cart_item;
	}

Replace with:

  function filter_session($cart_content, $value, $key){
            if ($value['sample']){
                    $cart_content['sample'] = true;
                    $cart_content['unique_key'] = $value['unique_key'];
                    //$cart_content['data']->set_price( '0' );
                  $product_id = $cart_content['product_id'];
                  $sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
                  $sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
                  if ($sample_price_mode === 'custom'){
                    $cart_content['data']->set_price( $sample_price );
                  }else if ($sample_price_mode === 'free'){
                    $cart_content['data']->set_price( '0' );
                  }else{
                    //default
                  }
            }
            return $cart_content;
  }

  function get_item_data($item_data, $cart_item){
            global $cart_item_key;
            return $item_data;
  }

  function add_sample_to_cart_item_data ($cart_item_data, $product_id, $variation_id){
            if (get_post_meta($product_id, 'sample_enamble') && $_REQUEST['sample']){
                $cart_item_data['sample'] = true;
                $cart_item_data['unique_key'] = md5($product_id . 'sample');
            }
            return $cart_item_data;
  }
function add_sample_to_cart_item ($cart_item, $cart_item_key){
    if ($cart_item['sample'] === true){
        $cart_item['data']->set_price( '0' );
    }
    return $cart_item;
}

Otherwise here is the edited file for you to download

I hope this reaches and helps others with this issue. Please share to help others by sharing this post.

Many Thanks

Colin – Artificial Grass Direct.

Spread the word

Leave a Reply