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