WooCommerce_Sample

The WooCommerce Sample WooCommerce Sample class.

Defined (1)

The class is defined in the following location(s).

/woocommerce-sample.php  
  1. class WooCommerce_Sample { 
  2. /** 
  3. * Gets things started by adding an action to initialize this plugin once 
  4. * WooCommerce is known to be active and initialized 
  5. */ 
  6. public function __construct() { 
  7. add_action('woocommerce_init', array(&$this, 'init')); 
  8.  
  9. /** 
  10. * to add the necessary actions for the plugin 
  11. */ 
  12. public function init() { 
  13. // backend stuff 
  14. add_action('woocommerce_product_write_panel_tabs', array($this, 'product_write_panel_tab')); 
  15. add_action('woocommerce_product_write_panels', array($this, 'product_write_panel')); 
  16. add_action('woocommerce_process_product_meta', array($this, 'product_save_data'), 10, 2); 
  17. // frontend stuff 
  18. add_action('woocommerce_after_add_to_cart_form', array($this, 'product_sample_button'));  
  19. add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); 
  20. //add_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ); 
  21. //do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ); 
  22. // Prevent add to cart 
  23. add_filter('woocommerce_add_to_cart_validation', array( $this, 'add_to_cart_validation' ), 40, 4 ); 
  24. add_filter('woocommerce_add_cart_item_data', array( $this, 'add_sample_to_cart_item_data' ), 10, 3 ); 
  25. add_filter('woocommerce_add_cart_item', array( $this, 'add_sample_to_cart_item' ), 10, 2 ); 
  26. add_filter('woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 ); 
  27. add_filter('woocommerce_get_cart_item_from_session', array( $this, 'filter_session'), 10, 3); 
  28. add_filter('woocommerce_in_cart_product_title', array( $this, 'cart_title'), 10, 3); 
  29. add_filter('woocommerce_cart_widget_product_title', array( $this, 'cart_widget_product_title'), 10, 2); 
  30. add_filter('woocommerce_cart_item_quantity', array( $this, 'cart_item_quantity'), 10, 2); 
  31.  
  32. add_filter('woocommerce_shipping_free_shipping_is_available', array( $this, 'enable_free_shipping'), 40, 1); 
  33. add_filter('woocommerce_available_shipping_methods', array( $this, 'free_shipping_filter'), 10, 1); 
  34.  
  35. // filter for Minimum/Maximum plugin override overriding 
  36. if (in_array('woocommerce-min-max-quantities/min-max-quantities.php', apply_filters('active_plugins', get_option('active_plugins')))) { 
  37. add_filter('wc_min_max_quantity_minimum_allowed_quantity', array($this, 'minimum_quantity'), 10, 4 ); 
  38. add_filter('wc_min_max_quantity_maximum_allowed_quantity', array($this, 'maximum_quantity'), 10, 4 ); 
  39. add_filter('wc_min_max_quantity_group_of_quantity', array($this, 'group_of_quantity'), 10, 4 );  
  40.  
  41. // filter for Minimum/Maximum plugin overriding 
  42. function minimum_quantity($minimum_quantity, $checking_id, $cart_item_key, $values) { 
  43. if ($values['sample']) 
  44. $minimum_quantity = 1; 
  45. return $minimum_quantity; 
  46.  
  47. function maximum_quantity($maximum_quantity, $checking_id, $cart_item_key, $values) { 
  48. if ($values['sample']) 
  49. $maximum_quantity = 1; 
  50. return $maximum_quantity; 
  51.  
  52. function group_of_quantity($group_of_quantity, $checking_id, $cart_item_key, $values) { 
  53. if ($values['sample']) 
  54. $group_of_quantity = 1; 
  55. return $group_of_quantity; 
  56. // end filter for Mimimum/Maximum plugin overriding 
  57.  
  58. function enable_free_shipping($is_available) { 
  59. global $woocommerce; 
  60. if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { 
  61. $check = true; 
  62.  
  63. foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) { 
  64. if ($cart_item['sample']) { 
  65. $sample_shipping_mode = get_post_meta($cart_item['product_id'], 'sample_shipping_mode', true); 
  66. if ($sample_shipping_mode !== 'free') { 
  67. $check = false; 
  68. break;  
  69. }else{ 
  70. // sample is setted - we go on to check all other items in cart 
  71.  
  72. }else{ 
  73. $check = false; 
  74. break; 
  75.  
  76. if ($check === true) { 
  77. return true; 
  78. }else{ 
  79. return $is_available; 
  80. }else{ 
  81. return $is_available; 
  82.  
  83. function free_shipping_filter( $available_methods ) 
  84. if ( isset( $available_methods['free_shipping'] ) ) : 
  85. // Get Free Shipping array into a new array 
  86. $freeshipping = array(); 
  87. $freeshipping = $available_methods['free_shipping']; 
  88.  
  89. // Empty the $available_methods array 
  90. unset( $available_methods ); 
  91.  
  92. // Add Free Shipping back into $avaialble_methods 
  93. $available_methods = array(); 
  94. $available_methods['free_shipping'] = $freeshipping; 
  95. endif; 
  96. return $available_methods; 
  97.  
  98. function cart_item_quantity ($product_quantity, $cart_item_key) { 
  99. global $woocommerce; 
  100. if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { 
  101. $cart_items = $woocommerce->cart->get_cart(); 
  102. $cart_item =$cart_items[$cart_item_key]; 
  103. if ($cart_item['sample']) { 
  104. $product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key ); 
  105. }  
  106. return $product_quantity;  
  107.  
  108. function cart_title($title, $values, $cart_item_key) { 
  109. if ($values['sample']) { 
  110. $title .= ' [' . __('Sample', 'woosample') . '] '; 
  111. return $title; 
  112.  
  113. function cart_widget_product_title($title, $cart_item) { 
  114. if (is_array($cart_item) && $cart_item['sample']) { 
  115. $title .= ' [' . __('Sample', 'woosample') . '] '; 
  116. return $title; 
  117.  
  118. function filter_session($cart_content, $value, $key) { 
  119. if ($value['sample']) { 
  120. $cart_content['sample'] = true; 
  121. $cart_content['unique_key'] = $value['unique_key']; 
  122. //$cart_content['data']->price = 0; 
  123. $product_id = $cart_content['product_id']; 
  124. $sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default'; 
  125. $sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0; 
  126. if ($sample_price_mode === 'custom') { 
  127. $cart_content['data']->price = $sample_price; 
  128. }else if ($sample_price_mode === 'free') { 
  129. $cart_content['data']->price = 0; 
  130. }else{ 
  131. //default 
  132. return $cart_content; 
  133.  
  134. function get_item_data($item_data, $cart_item) { 
  135. global $cart_item_key; 
  136. //if ($cart_item['sample']) { 
  137. // error_log('SAMPLE TRUE'); 
  138. //}else{ 
  139. // error_log('SAMPLE FALSE'); 
  140. //} 
  141. return $item_data; 
  142.  
  143. function add_sample_to_cart_item_data ($cart_item_data, $product_id, $variation_id) { 
  144. if (get_post_meta($product_id, 'sample_enamble') && $_REQUEST['sample']) { 
  145. $cart_item_data['sample'] = true; 
  146. $cart_item_data['unique_key'] = md5($product_id . 'sample'); 
  147. return $cart_item_data; 
  148.  
  149. function add_sample_to_cart_item ($cart_item, $cart_item_key) { 
  150. if ($cart_item['sample'] === true) { 
  151. $cart_item['data']->price = 0; 
  152. return $cart_item; 
  153.  
  154. /** 
  155. * add_to_cart_validation function. 
  156. * @access public 
  157. * @param mixed $pass 
  158. * @param mixed $product_id 
  159. * @param mixed $quantity 
  160. * @return void 
  161. */ 
  162. function add_to_cart_validation( $pass, $product_id, $quantity, $variation_id = 0 ) { 
  163. global $woocommerce; 
  164.  
  165. // se ci sono articoli nel carrello eseguiamo i controlli altrimenti se il carrello vuoto aggiungiamo l'elemento senza controlli ulteriori 
  166. if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { 
  167. $is_sample = empty($_REQUEST['sample']) ? false : true; 
  168. // eseguiamo una validazione specifica solo se l'articolo aggiunto un campione 
  169. if ($is_sample) { 
  170. // l'articolo richiesto un "campione" controlliamo che non sia gi stato inserito nel carrello 
  171. $cart_items = $woocommerce->cart->get_cart(); 
  172. $unique_key = md5($product_id . 'sample'); 
  173.  
  174. foreach ($cart_items as $cart_id_key => $cart_item) { 
  175. if ($cart_item['unique_key'] == $unique_key) { 
  176. $woocommerce->add_error( __( 'A sample of the same product is already present into your cart', 'woosample' ) ); 
  177. return false; 
  178. if ($cart_item['product_id'] == $product_id) { 
  179. $woocommerce->add_error( __( 'You have already added this product on your cart, you can\'t add a sample of the same item', 'woosample' ) ); 
  180. return false; 
  181. // passiamo il valore impostato di default; 
  182. return $pass; 
  183. /** 
  184. * creates the tab for the administrator, where administered product sample. 
  185. */ 
  186. public function product_write_panel_tab() { 
  187. echo "<li><a class='added_sample' href=\"#sample_tab\">" . __('Sample', 'woosample') . "</a></li>"; 
  188.  
  189. /** 
  190. * build the panel for the administrator. 
  191. */ 
  192. public function product_write_panel() { 
  193. global $post; 
  194. $sample_enable = get_post_meta($post->ID, 'sample_enamble', true) ? get_post_meta($post->ID, 'sample_enamble', true) : false; 
  195. $sample_shipping_mode = get_post_meta($post->ID, 'sample_shipping_mode', true) ? get_post_meta($post->ID, 'sample_shipping_mode', true) : 'default'; 
  196. $sample_shipping = get_post_meta($post->ID, 'sample_shiping', true) ? get_post_meta($post->ID, 'sample_shipping', true) : 0; 
  197. $sample_price_mode = get_post_meta($post->ID, 'sample_price_mode', true) ? get_post_meta($post->ID, 'sample_price_mode', true) : 'default'; 
  198. $sample_price = get_post_meta($post->ID, 'sample_price', true) ? get_post_meta($post->ID, 'sample_price', true) : 0; 
  199. ?> 
  200. <div id="sample_tab" class="panel woocommerce_options_panel"> 
  201. <p class="form-field sample_enamble_field "> 
  202. <label for="sample_enamble"><?php _e('Enable sample', 'woosample');?></label> 
  203. <input type="checkbox" class="checkbox" name="sample_enamble" id="sample_enamble" value="yes" <?php echo $sample_enable ? 'checked="checked"' : ''; ?>> <span class="description"><?php _e('Enable or disable sample option for this item.', 'woosample'); ?></span> 
  204. </p> 
  205. <legend><?php _e('Sample Shipping', 'woosample'); ?></legend> 
  206. <div class="options_group"> 
  207. <input class="radio" id="sample_shipping_default" type="radio" value="default" name="sample_shipping_mode" <?php echo $sample_shipping_mode == 'default' ? 'checked="checked"' : ''; ?>> 
  208. <label class="radio" for="sample_shipping_default"><?php _e('use default product shipping methods', 'woosample'); ?></label> 
  209. </div> 
  210. <div class="options_group"> 
  211. <input class="radio" id="sample_shipping_free" type="radio" value="free" name="sample_shipping_mode" <?php echo $sample_shipping_mode == 'free' ? 'checked="checked"' : ''; ?>> 
  212. <label class="radio" for="sample_shipping_free"><?php _e('free shipping for sample', 'woosample'); ?></label> 
  213. </div> 
  214. <!-- 
  215. <div class="options_group"> 
  216. <input class="radio" id="sample_shipping_custom" type="radio" value="custom" name="sample_shipping_mode" <?php echo $sample_shipping_mode == 'custom' ? 'checked="checked"' : ''; ?>> 
  217. <label class="radio" for="sample_shipping_custom"><?php _e('custom fee shipping', 'woosample'); ?></label> 
  218. <p class="form-field sample_shipping_field clear"> 
  219. <label for="sample_shipping"><?php _e('set shipping fee', 'woosample'); ?></label> 
  220. <input type="number" class="wc_input_price short" name="sample_shipping" id="sample_shipping" value="<?php echo $sample_shipping; ?>" step="any" min="0"> 
  221. </p> 
  222. </div> 
  223. --> 
  224. <legend><?php _e('Sample price', 'woosample'); ?></legend> 
  225. <div class="options_group"> 
  226. <input class="radio" id="sample_price_default" type="radio" value="default" name="sample_price_mode" <?php echo $sample_price_mode == 'default' ? 'checked="checked"' : ''; ?>> 
  227. <label class="radio" for="sample_price_default"><?php _e('product default price', 'woosample'); ?></label> 
  228. </div> 
  229. <div class="options_group"> 
  230. <input class="radio" id="sample_price_free" type="radio" value="free" name="sample_price_mode" <?php echo $sample_price_mode == 'free' ? 'checked="checked"' : ''; ?>> 
  231. <label class="radio" for="sample_price_free"><?php _e('free', 'woosample'); ?></label> 
  232. </div> 
  233. <div class="options_group"> 
  234. <input class="radio" id="sample_price_custom" type="radio" value="custom" name="sample_price_mode" <?php echo $sample_price_mode == 'custom' ? 'checked="checked"' : ''; ?>> 
  235. <label class="radio" for="sample_price_custom"><?php _e('custom price', 'woosample'); ?></label> 
  236. <p class="form-field sample_price_field clear"> 
  237. <label for="sample_price"><?php _e('set sample price', 'woosample'); ?></label> 
  238. <input type="number" class="wc_input_price short" name="sample_price" id="sample_price" value="<?php echo $sample_price; ?>" step="any" min="0"> 
  239. </p> 
  240. </div> 
  241. </div> 
  242. <?php 
  243.  
  244. /** 
  245. * build form to the administrator. 
  246. */ 
  247.  
  248.  
  249. /** 
  250. * updating the database post. 
  251. */ 
  252. public function product_save_data($post_id, $post) { 
  253.  
  254. $sample_enamble = $_POST['sample_enamble']; 
  255. if (empty($sample_enamble)) { 
  256. delete_post_meta($post_id, 'sample_enamble'); 
  257. }else{ 
  258. update_post_meta($post_id, 'sample_enamble', true); 
  259.  
  260. $sample_price_mode = $_POST['sample_price_mode']; 
  261. update_post_meta($post_id, 'sample_price_mode', $sample_price_mode); 
  262. $sample_price = $_POST['sample_price']; 
  263. update_post_meta($post_id, 'sample_price', $sample_price); 
  264. $sample_shipping_mode = $_POST['sample_shipping_mode']; 
  265. update_post_meta($post_id, 'sample_shipping_mode', $sample_shipping_mode); 
  266. $sample_shipping = $_POST['sample_shipping']; 
  267. update_post_meta($post_id, 'sample_shipping', $sample_shipping); 
  268. //$videos = $_POST['_tab_sample']; 
  269. //$length = count($videos); 
  270. //foreach($videos as $key=>$video) { 
  271. // if(!empty($video)) update_post_meta($post_id, 'wo_di_video_product'.$key, stripslashes($video)); 
  272. // else delete_post_meta($post_id, 'wo_di_video_product'.$key); 
  273. //} 
  274.  
  275.  
  276. public function product_sample_button() { 
  277. global $post, $product; 
  278. $is_sample = get_post_meta($post->ID, 'sample_enamble'); 
  279. if ($is_sample) { 
  280. ?> 
  281. <?php do_action('woocommerce_before_add_sample_to_cart_form'); ?> 
  282. <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart sample" method="post" enctype='multipart/form-data'> 
  283. <?php do_action('woocommerce_before_add_sample_to_cart_button'); ?> 
  284. <div class="single_variation_wrap" style="">  
  285. <button type="submit" class="single_add_to_cart_button button alt single_add_sample_to_cart_button"><?php echo __( 'Add Sample to cart', 'woosample' ); ?></button> 
  286. <input type="hidden" name="sample" id="sample" value="true"/> 
  287. </div> 
  288. <?php do_action('woocommerce_after_add_sample_to_cart_button'); ?> 
  289. </form> 
  290. <?php do_action('woocommerce_after_add_sample_to_cart_form'); ?> 
  291. <?php 
  292.  
  293. function enqueue_scripts() { 
  294. global $pagenow, $wp_scripts; 
  295. $plugin_url = untrailingslashit(plugin_dir_url(__FILE__)); 
  296. if ( ! is_admin() ) { 
  297. wp_enqueue_script('woocommerce-sample', $plugin_url . '/js/woocommerce-sample.js', array('jquery'), '1.0', true); 
  298. /** 
  299. if (is_admin() && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' || 'edit-tags.php')) { 
  300. // for admin enqueue 
  301. */ 
  302.  
  303.  
  304. }//end of the class