mmwd-remove-add-to-cart-for-woocommerce - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: mmwd-remove-add-to-cart-for-woocommerce fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of mmwd-remove-add-to-cart-for-woocommerce.php

  1. <?php
  2. /*
  3. Plugin Name: MMWD Remove Add To Cart for WooCommerce
  4. Plugin URI:  https://mcgregormedia.co.uk
  5. Description: Removes all Add to Cart buttons throughout a WooCommerce website without affecting anything else hooked into the Add to Cart actions.
  6. Version:     1.4.16
  7. Author:      McGregor Media Web Design
  8. Author URI:  https://mcgregormedia.co.uk
  9. Text Domain: mmwd-ratc
  10. WC requires at least: 3.0
  11. WC tested up to: 4.7
  12. License: GNU General Public License v3.0
  13. License URI: http://www.gnu.org/licenses/gpl-3.0.html
  14.  
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation, either version 3 of the License, or
  18. (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program.  If not, see http://www.gnu.org/licenses/gpl-3.0.html.
  27. */
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. if ( ! defined( 'ABSPATH' ) ) {
  36.        
  37.         exit; // Came here directly? Vamoose.
  38.        
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /**
  50.  * Loads translation files
  51.  * 
  52.  * @since 1.2.0                                      Added function
  53.  */
  54.  
  55. function mmwd_remove_atc_load_textdomain() {
  56.        
  57.         load_plugin_textdomain( 'mmwd-ratc', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
  58.        
  59. }
  60. add_action( 'plugins_loaded', 'mmwd_remove_atc_load_textdomain' );
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. /**
  68.  * Adds option on activation to check if newly activated. If true, runs WooCOmmerce check after register_activation_hook redirection
  69.  * 
  70.  * @since 1.2.0                                      Added function
  71.  */
  72.  
  73. function mmwd_remove_atc_activate(){
  74.        
  75.     add_option( 'mmwd_remove_atc_activated', 'mmwd-ratc' );
  76.        
  77. }
  78. register_activation_hook( __FILE__, 'mmwd_remove_atc_activate' );
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /**
  87.  * Checks whether WooCommerce is active and deactivates plugin with admin notice if not
  88.  * 
  89.  * @since 1.2.0                                      Added function
  90.  */
  91.  
  92. function mmwd_remove_atc_load_plugin(){
  93.  
  94.     if ( is_admin() && get_option( 'mmwd_remove_atc_activated' ) == 'mmwd-ratc' ) {
  95.                
  96.         delete_option( 'mmwd_remove_atc_activated' ); // remove option we set on activation
  97.  
  98.         if ( !class_exists( 'WooCommerce' ) ) { // check WooCommerce is active
  99.                        
  100.             add_action( 'admin_notices', 'mmwd_remove_atc_admin_notice' ); // if not display admin notice
  101.  
  102.             deactivate_plugins( plugin_basename( __FILE__ ) ); // deactivate plugin
  103.  
  104.             if ( isset( $_GET['activate'] ) ) {
  105.                                
  106.                 unset( $_GET['activate'] );
  107.                                
  108.             }
  109.                        
  110.         }
  111.                
  112.     }
  113.        
  114. }
  115. add_action( 'admin_init', 'mmwd_remove_atc_load_plugin' );
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /**
  125.  * Display an error message if WooCommerce is not activated
  126.  * 
  127.  * @return string                            The formatted HTML
  128.  * 
  129.  * @since 1.2.0                                      Added function
  130.  */
  131.  
  132. function mmwd_remove_atc_admin_notice (){
  133.        
  134.     ?>
  135.     <div class="notice notice-error"><p><?php _e( 'MMWD Remove Add To Cart for WooCommerce requires WooCommerce to run. Please install and activate WooCommerce.', 'mmwd-ratc' ) ?></p></div>
  136.     <?php
  137.        
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /**
  149.  * Adds settings section in WooCommerce > Settings > Products 
  150.  * 
  151.  * @param array $sections                 The settings sections array
  152.  * 
  153.  * @return array $sections        The updated settings sections array
  154.  *
  155.  * @since 1.0.0                                      Added function
  156.  */
  157.  
  158. function mmwd_add_remove_atc_settings_section( $sections ) {
  159.        
  160.         $sections['mmwd_remove_atc_section'] = __( 'Remove Add to Cart', 'mmwd-ratc' );
  161.        
  162.         return $sections;
  163.        
  164. }
  165. add_filter( 'woocommerce_get_sections_products', 'mmwd_add_remove_atc_settings_section' );
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /**
  174.  * Displays settings section for Remove Add to Cart in WooCommerce > Settings > Products 
  175.  * 
  176.  * @param array $settings                                  The array of settings
  177.  * @param string $current_section                         The current setting id
  178.  * 
  179.  * @return array $mmwd_remove_atc                   The updated array of settings
  180.  * @return array $settings                                  The standard array of settings
  181.  *
  182.  * @since 1.2.0                                                              Added note to reflect theme issues
  183.  * @since 1.1.0                                                              Added 'Remove prices' option
  184.  * @since 1.0.0                                                              Added function
  185.  */
  186.  
  187. function mmwd_display_remove_atc_settings( $settings, $current_section ){
  188.        
  189.         // Check the current section is what we want
  190.         if ( $current_section == 'mmwd_remove_atc_section' ) {
  191.                
  192.                 $mmwd_remove_atc = array();
  193.                
  194.                 // Title
  195.                 $mmwd_remove_atc[] = array(
  196.                         'id' => 'mmwd_remove_atc_title',
  197.                         'name' => __( 'Remove Add to Cart buttons', 'mmwd-ratc' ),
  198.                         'type' => 'title',
  199.                         'desc' => __( 'Remove all Add to Cart buttons without affecting anything else hooked into the Add to Cart actions. Removing prices may not work with themes that do not use the standard WooCommerce hooks.', 'mmwd-ratc' )
  200.                 );
  201.  
  202.                 // Checkbox
  203.                 $mmwd_remove_atc[] = array(
  204.                         'id'       => 'mmwd_remove_atc',
  205.                         'name'     => __( 'Remove Add to Cart buttons', 'mmwd-ratc' ),
  206.                         'type'     => 'checkbox'
  207.                 );
  208.  
  209.                 // Checkbox
  210.                 $mmwd_remove_atc[] = array(
  211.                         'id'       => 'mmwd_remove_price',
  212.                         'name'     => __( 'Remove prices', 'mmwd-ratc' ),
  213.                         'type'     => 'checkbox'
  214.                 );
  215.  
  216.                 $mmwd_remove_atc[] = array(
  217.                         'type' => 'sectionend',
  218.                         'id' => 'mmwd_remove_atc_end'
  219.                 );
  220.                
  221.                 return $mmwd_remove_atc;
  222.        
  223.         // If not, return the standard settings
  224.         }else{ 
  225.                
  226.                 return $settings;
  227.                
  228.         }
  229. }
  230. add_filter( 'woocommerce_get_settings_products', 'mmwd_display_remove_atc_settings', 10, 2 );
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238. /**
  239.  * Adds the filter to remove the Add to Cart buttons
  240.  *
  241.  * @since 1.0.1              Added function
  242.  */
  243.  
  244. function mmwd_remove_atc_add_filter(){
  245.        
  246.         if( get_option( 'mmwd_remove_atc' ) && get_option( 'mmwd_remove_atc' ) === 'yes' ){
  247.        
  248.                 return false;
  249.        
  250.         }else{
  251.                
  252.                 return true;
  253.                
  254.         }
  255.        
  256. }
  257. add_filter( 'out_of_stock', 'mmwd_remove_atc_add_filter' );
  258.  
  259.  
  260.  
  261.  
  262.  
  263. /**
  264.  *  Removes the Add to Cart buttons from variable products
  265.  *  
  266.  *  @since 1.4.6
  267.  */
  268.  
  269. function mmwd_remove_atc_variable_product() {
  270.        
  271.         if( get_option( 'mmwd_remove_atc' ) && get_option( 'mmwd_remove_atc' ) === 'yes' ){
  272.  
  273.                 remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
  274.        
  275.         }
  276.        
  277. }
  278. add_action( 'woocommerce_single_product_summary', 'mmwd_remove_atc_variable_product', 1, 0 );
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /**
  286.  * Removes prices on single products
  287.  *
  288.  * @since 1.2.0              Removed erroneous remove_actions
  289.  * @since 1.1.0              Added function
  290.  */
  291.  
  292. function mmwd_remove_price_remove_actions(){
  293.  
  294.         if( get_option( 'mmwd_remove_price' ) && get_option( 'mmwd_remove_price' ) === 'yes' ){
  295.                
  296.                 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
  297.                 remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
  298.                
  299.         }
  300.  
  301. }
  302. add_action( 'init', 'mmwd_remove_price_remove_actions' );
  303.  
  304.  
  305.  
  306.  
  307. /**
  308.  *  Removes prices on variable products
  309.  *  
  310.  *  @param int $price                     The original price
  311.  *  @param object $product         The product object 
  312.  *  
  313.  *  @return string $price          The updated price
  314.  *  
  315.  *  @since 1.4.7                            FIXED: rearranged hook order to honour mmwd_remove_price option
  316.  *  @since 1.4.6
  317.  */
  318.  
  319. function mmwd_remove_price_variable_product( $price, $product ) {
  320.  
  321.         $price = '';
  322.         return $price;
  323.        
  324. }
  325.  
  326.  
  327.  
  328. /**
  329.  *  Conditionally adds filters to fire mmwd_remove_price_variable_product()
  330.  *  
  331.  *  @since 1.4.7
  332.  */
  333. function mmwd_remove_price_variable_product_init() {
  334.        
  335.         if ( ( get_option( 'mmwd_remove_price' ) && get_option( 'mmwd_remove_price' ) === 'yes' ) ){
  336.                
  337.                 add_filter( 'woocommerce_variable_sale_price_html', 'mmwd_remove_price_variable_product', 10, 2 );
  338.                 add_filter( 'woocommerce_variable_price_html', 'mmwd_remove_price_variable_product', 10, 2 );
  339.                 add_filter( 'woocommerce_get_price_html', 'mmwd_remove_price_variable_product', 10, 2 );
  340.                
  341.         }
  342.  
  343. }
  344. add_action( 'init', 'mmwd_remove_price_variable_product_init' );
File Description
  • mmwd-remove-add-to-cart-for-woocommerce
  • PHP Code
  • 03 Feb-2021
  • 7.58 Kb
You can Share it: