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