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