amansdpr

25 WooCommerce Code Snippet

Feb 28th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.13 KB | None | 0 0
  1. <?
  2. =====================================================================================================
  3. /*----- 1 – Add Payment Type to WooCommerce Admin Email ------*/
  4. =====================================================================================================
  5.  
  6. add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
  7.  
  8. function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
  9. if ( $is_admin_email ) {
  10. echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>';
  11. }
  12. }
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. =====================================================================================================
  22. /*----- 2 – Up-sells products per page / per line ------*/
  23. =====================================================================================================
  24.  
  25. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  26. add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
  27.  
  28. if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
  29. function woocommerce_output_upsells() {
  30. woocommerce_upsell_display( 3,3 ); // Display 3 products in rows of 3
  31. }
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. =====================================================================================================
  41. /*----- 3 - Remove product categories from shop page ------*/
  42. =====================================================================================================
  43. add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  44.  
  45. function custom_pre_get_posts_query( $q ) {
  46. if ( ! $q->is_main_query() ) return;
  47. if ( ! $q->is_post_type_archive() ) return;
  48. if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {
  49. $q->set( 'tax_query', array(array(
  50. 'taxonomy' => 'product_cat',
  51. 'field' => 'slug',
  52. 'terms' => array( 'color', 'flavor', 'spices', 'vanilla' ), // Don't display products in these categories on the shop page
  53. 'operator' => 'NOT IN'
  54. )));
  55. }
  56. remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. =====================================================================================================
  66. /*----- 4 - Quickly translate any string ------*/
  67. =====================================================================================================
  68. add_filter('gettext', 'translate_text');
  69. add_filter('ngettext', 'translate_text');
  70.  
  71. function translate_text($translated) {
  72. $translated = str_ireplace('Choose and option', 'Select', $translated);
  73. return $translated;
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. =====================================================================================================
  82. /*----- 5 - Exclude a category from the WooCommerce category widget ------*/
  83. =====================================================================================================
  84. add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
  85.  
  86. function woo_product_cat_widget_args( $cat_args ) {
  87. $cat_args['exclude'] = array('16');
  88. return $cat_args;
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. =====================================================================================================
  97. /*----- 6 - Add a custom field to a product variation ------*/
  98. =====================================================================================================
  99. //Display Fields
  100. add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
  101. //JS to add fields for new variations
  102. add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
  103. //Save variation fields
  104. add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
  105.  
  106. function variable_fields( $loop, $variation_data ) {
  107. ?>
  108. <tr>
  109. <td>
  110. <div>
  111. <label></label>
  112. <input type="text" size="5" name="my_custom_field[]" value=""/>
  113. </div>
  114. </td>
  115. </tr>
  116.  
  117. <tr>
  118. <td>
  119. <div>
  120. <label></label>
  121.  
  122. </div>
  123. </td>
  124. </tr>
  125. <?php
  126. }
  127. function variable_fields_process( $post_id ) {
  128. if (isset( $_POST['variable_sku'] ) ) :
  129. $variable_sku = $_POST['variable_sku'];
  130. $variable_post_id = $_POST['variable_post_id'];
  131. $variable_custom_field = $_POST['my_custom_field'];
  132. for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  133. $variation_id = (int) $variable_post_id[$i];
  134. if ( isset( $variable_custom_field[$i] ) ) {
  135. update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
  136. }
  137. endfor;
  138. endif;
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. =====================================================================================================
  147. /*----- 7 - Replace “Out of stock” by “sold” ------*/
  148. =====================================================================================================
  149. add_filter('woocommerce_get_availability', 'availability_filter_func');
  150.  
  151. function availability_filter_func($availability) {
  152. $availability['availability'] = str_ireplace('Out of stock', 'Sold', $availability['availability']);
  153. return $availability;
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. =====================================================================================================
  162. /*----- 8 – Display “product already in cart” instead of “add to cart” button ------*/
  163. =====================================================================================================
  164. /**
  165. * Change the add to cart text on single product pages
  166. */
  167. add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
  168.  
  169. function woo_custom_cart_button_text() {
  170. global $woocommerce;
  171. foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  172. $_product = $values['data'];
  173. if( get_the_ID() == $_product->id ) {
  174. return __('Already in cart - Add Again?', 'woocommerce');
  175. }
  176. }
  177. return __('Add to cart', 'woocommerce');
  178. }
  179.  
  180. /**
  181. * Change the add to cart text on product archives
  182. */
  183. add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );
  184.  
  185. function woo_archive_custom_cart_button_text() {
  186. global $woocommerce;
  187. foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  188. $_product = $values['data'];
  189. if( get_the_ID() == $_product->id ) {
  190. return __('Already in cart', 'woocommerce');
  191. }
  192. }
  193. return __('Add to cart', 'woocommerce');
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. =====================================================================================================
  202. /*----- 9 - Hide products count in category view ------*/
  203. =====================================================================================================
  204. add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
  205.  
  206. function woo_remove_category_products_count() {
  207. return;
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. =====================================================================================================
  216. /*----- 10 - Make account checkout fields required ------*/
  217. =====================================================================================================
  218. add_filter( 'woocommerce_checkout_fields', 'woo_filter_account_checkout_fields' );
  219.  
  220. function woo_filter_account_checkout_fields( $fields ) {
  221. $fields['account']['account_username']['required'] = true;
  222. $fields['account']['account_password']['required'] = true;
  223. $fields['account']['account_password-2']['required'] = true;
  224.  
  225. return $fields;
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. =====================================================================================================
  234. /*----- 11 - Rename a product tab ------*/
  235. =====================================================================================================
  236. add_filter( 'wocommerce_product_tabs', 'woo_rename_tab', 98);
  237. function woo_rename_tab($tabs) {
  238.  
  239. $tabs['description']['title'] = 'More info';
  240.  
  241. return $tabs;
  242. }
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. =====================================================================================================
  250. /*----- 12 - List WooCommerce product Categories ------*/
  251. =====================================================================================================
  252. $args = array(
  253. 'number' => $number,
  254. 'orderby' => $orderby,
  255. 'order' => $order,
  256. 'hide_empty' => $hide_empty,
  257. 'include' => $ids
  258. );
  259.  
  260. $product_categories = get_terms( 'product_cat', $args );
  261. $count = count($product_categories);
  262. if ( $count > 0 ){
  263. echo "<ul>";
  264. foreach ( $product_categories as $product_category ) {
  265. echo '<li><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';
  266. }
  267. echo "</ul>";
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. =====================================================================================================
  276. /*----- 13 - Replace shop page title ------*/
  277. =====================================================================================================
  278. add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
  279.  
  280. function woo_shop_page_title( $page_title ) {
  281. if( 'Shop' == $page_title) {
  282. return "My new title";
  283. }
  284. }
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291. =====================================================================================================
  292. /*----- 14 - Change a widget title ------*/
  293. =====================================================================================================
  294. /* Change widget title */
  295. add_filter( 'widget_title', 'woo_widget_title', 10, 3);
  296.  
  297. function woo_widget_title( $title, $instance, $id_base ) {
  298. if( 'onsale' == $id_base) {
  299. return "My new title";
  300. }
  301. }
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. =====================================================================================================
  309. /*----- 15 - Remove WooCommerce default settings ------*/
  310. =====================================================================================================
  311. add_filter( 'woocommerce_catalog_settings', 'woo_remove_catalog_options' );
  312.  
  313. function woo_remove_catalog_options( $catalog ) {
  314. unset($catalog[23]); //Trim zeros (no)
  315. unset($catalog[22]); //2 decimals
  316. unset($catalog[21]); //decimal sep (.)
  317. unset($catalog[20]); //thousand sep (,)
  318. unset($catalog[19]); //currency position (left)
  319. unset($catalog[18]); //currency position (left)
  320. unset($catalog[5]); // ajax add to cart (no)
  321. return $catalog;
  322. }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. =====================================================================================================
  330. /*----- 16 - Change “from” email address ------*/
  331. =====================================================================================================
  332. function woo_custom_wp_mail_from() {
  333. global $woocommerce;
  334. return html_entity_decode( '[email protected]' );
  335. }
  336. add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343. =====================================================================================================
  344. /*----- 17 - Decode from name in WooCommerce email ------*/
  345. =====================================================================================================
  346. function woo_custom_wp_mail_from_name() {
  347. global $woocommerce;
  348. return html_entity_decode( get_option( 'woocommerce_email_from_name' ) );
  349. }
  350. add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );
  351.  
  352. function woo_custom_wp_mail_from() {
  353. global $woocommerce;
  354. return html_entity_decode( get_option( 'woocommerce_email_from' ) );
  355. }
  356. add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363. =====================================================================================================
  364. /*----- 18 – Return featured products ids ------*/
  365. =====================================================================================================
  366. function woo_get_featured_product_ids() {
  367. // Load from cache
  368. $featured_product_ids = get_transient( 'wc_featured_products' );
  369.  
  370. // Valid cache found
  371. if ( false !== $featured_product_ids )
  372. return $featured_product_ids;
  373.  
  374. $featured = get_posts( array(
  375. 'post_type' => array( 'product', 'product_variation' ),
  376. 'posts_per_page' => -1,
  377. 'post_status' => 'publish',
  378. 'meta_query' => array(
  379. array(
  380. 'key' => '_visibility',
  381. 'value' => array('catalog', 'visible'),
  382. 'compare' => 'IN'
  383. ),
  384. array(
  385. 'key' => '_featured',
  386. 'value' => 'yes'
  387. )
  388. ),
  389. 'fields' => 'id=>parent'
  390. ) );
  391.  
  392. $product_ids = array_keys( $featured );
  393. $parent_ids = array_values( $featured );
  394. $featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );
  395.  
  396. set_transient( 'wc_featured_products', $featured_product_ids );
  397.  
  398. return $featured_product_ids;
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406. =====================================================================================================
  407. /*----- 19 - Add custom field to edit address page ------*/
  408. =====================================================================================================
  409. // add fields to edit address page
  410. function woo_add_edit_address_fields( $fields ) {
  411.  
  412. $new_fields = array(
  413. 'date_of_birth' => array(
  414. 'label' => __( 'Date of birth', 'woocommerce' ),
  415. 'required' => false,
  416. 'class' => array( 'form-row' ),
  417. ),
  418. );
  419.  
  420. $fields = array_merge( $fields, $new_fields );
  421.  
  422. return $fields;
  423.  
  424. }
  425.  
  426. add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' );
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433. =====================================================================================================
  434. /*----- 20 – Display onsale products catalog shortcode ------*/
  435. =====================================================================================================
  436. function woocommerce_sale_products( $atts ) {
  437.  
  438. global $woocommerce_loop;
  439.  
  440. extract(shortcode_atts(array(
  441. 'per_page' => '12',
  442. 'columns' => '4',
  443. 'orderby' => 'date',
  444. 'order' => 'desc'
  445. ), $atts));
  446.  
  447. $woocommerce_loop['columns'] = $columns;
  448.  
  449. $args = array(
  450. 'post_type' => 'product',
  451. 'post_status' => 'publish',
  452. 'ignore_sticky_posts' => 1,
  453. 'posts_per_page' => $per_page,
  454. 'orderby' => $orderby,
  455. 'order' => $order,
  456. 'meta_query' => array(
  457. array(
  458. 'key' => '_visibility',
  459. 'value' => array('catalog', 'visible'),
  460. 'compare' => 'IN'
  461. ),
  462. array(
  463. 'key' => '_sale_price',
  464. 'value' => 0,
  465. 'compare' => '>',
  466. 'type' => 'NUMERIC'
  467. )
  468. )
  469. );
  470. query_posts($args);
  471. ob_start();
  472. woocommerce_get_template_part( 'loop', 'shop' );
  473. wp_reset_query();
  474.  
  475. return ob_get_clean();
  476. }
  477.  
  478. add_shortcode('sale_products', 'woocommerce_sale_products');
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485. =====================================================================================================
  486. /*----- 21 - Have onsale products ------*/
  487. =====================================================================================================
  488. function woo_have_onsale_products() {
  489.  
  490. global $woocommerce;
  491.  
  492. // Get products on sale
  493. $product_ids_on_sale = array_filter( woocommerce_get_product_ids_on_sale() );
  494.  
  495. if( !empty( $product_ids_on_sale ) ) {
  496. return true;
  497. } else {
  498. return false;
  499. }
  500.  
  501. }
  502.  
  503. // Example:
  504. if( woo_have_onsale_products() ) {
  505. echo 'have onsale products';
  506. } else {
  507. echo 'no onsale product';
  508. }
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515. =====================================================================================================
  516. /*----- 22 – Set minimum order amount ------*/
  517. =====================================================================================================
  518. add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
  519. function wc_minimum_order_amount() {
  520. global $woocommerce;
  521. $minimum = 50;
  522. if ( $woocommerce->cart->get_cart_total(); < $minimum ) {
  523. $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
  524. }
  525. }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. =====================================================================================================
  533. /*----- 23 - Order by price, date or title on shop page ------*/
  534. =====================================================================================================
  535. add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
  536.  
  537. function custom_default_catalog_orderby() {
  538. return 'date'; // Can also use title and price
  539. }
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546. =====================================================================================================
  547. /*----- 24 - Redirect add to cart button to checkout page ------*/
  548. =====================================================================================================
  549. add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
  550.  
  551. function redirect_to_checkout() {
  552. global $woocommerce;
  553. $checkout_url = $woocommerce->cart->get_checkout_url();
  554. return $checkout_url;
  555. }
  556.  
  557.  
  558.  
  559.  
  560.  
  561. =====================================================================================================
  562. /*----- 25 - Add email recipient when order completed ------*/
  563. =====================================================================================================
  564. function woo_extra_email_recipient($recipient, $object) {
  565. $recipient = $recipient . ', [email protected]';
  566. return $recipient;
  567. }
  568. add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2);
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575. =====================================================================================================
  576. /*----- ------*/
  577. =====================================================================================================
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585. =====================================================================================================
  586. /*----- ------*/
  587. =====================================================================================================
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603. ?>
Add Comment
Please, Sign In to add comment