helgatheviki

disable-cart-page-for-woocommerce.php

Jan 31st, 2022 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.00 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     Plugin Name: Disable cart page for WooCommerce
  5.     Plugin URI: https://code4life.it/shop/plugins/disable-cart-page-for-woocommerce/
  6.     Description: Disable cart page and redirect to checkout for each purchase.
  7.     Author: Code4Life
  8.     Author URI: https://code4life.it/
  9.     Version: 1.3.0-beta
  10.     Text Domain: disable-cart-page-for-woocommerce
  11.     Domain Path: /i18n/
  12.     License: GPLv3
  13.     License URI: http://www.gnu.org/licenses/gpl-3.0.html
  14.  
  15.     WC requires at least: 3.0
  16.     WC tested up to: 6.1
  17.  */
  18.  
  19. // Exit if accessed directly
  20. if ( ! defined( 'ABSPATH' ) ) { exit; }
  21.  
  22. // Add language support to internationalize plugin
  23. add_action( 'init', 'wcdcp_textdomain' );
  24. function wcdcp_textdomain() {
  25.     load_plugin_textdomain( 'disable-cart-page-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/' );
  26. }
  27.  
  28. // Add link to configuration page into plugin
  29. add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wcdcp_action_links' );
  30. function wcdcp_action_links( $links ) {
  31.     return array_merge( array(
  32.         'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=wcdcp' ) . '">' . __( 'Settings', 'disable-cart-page-for-woocommerce' ) . '</a>'
  33.     ), $links );
  34. }
  35.  
  36. function wcdcp_fields() {
  37.     $fields = array(
  38.         'section_title-enable' => array(
  39.             'name'     => __( 'Disable cart page', 'disable-cart-page-for-woocommerce' ),
  40.             'type'     => 'title',
  41.             'desc'     => __( 'Enabling this option will delete the cart page and for each purchase customers will be redirected directly to the checkout. They will also be able to buy one type of product at a time.', 'disable-cart-page-for-woocommerce' )
  42.         ),
  43.         'wcdcp_enable' => array(
  44.             'name' => __( 'Enable', 'disable-cart-page-for-woocommerce' ),
  45.             'type' => 'checkbox',
  46.             'desc' => __( 'Disable cart page and redirect directly to checkout', 'disable-cart-page-for-woocommerce' ),
  47.             'id'   => 'wcdcp_enable'
  48.         ),
  49.         'section_end-enable' => array(
  50.              'type' => 'sectionend'
  51.         )
  52.     );
  53.  
  54.     return apply_filters( 'wcdcp_fields', $fields );
  55. }
  56.  
  57. // Add settings tab to WooCommerce options
  58. add_filter( 'woocommerce_settings_tabs_array', 'wcdcp_settings_tab', 50 );
  59. function wcdcp_settings_tab( $tabs ) {
  60.     $tabs['wcdcp'] = __( 'Disable cart page', 'disable-cart-page-for-woocommerce' );
  61.    
  62.     return $tabs;
  63. }
  64.  
  65. // Add settings to the new tab
  66. add_action( 'woocommerce_settings_tabs_wcdcp', 'wcdcp_settings' );
  67. function wcdcp_settings() {
  68.     woocommerce_admin_fields( wcdcp_fields() );
  69. }
  70.  
  71. // Save settings
  72. add_action( 'woocommerce_update_options_wcdcp', 'wcdcp_update_settings' );
  73. function wcdcp_update_settings() {
  74.     woocommerce_update_options( wcdcp_fields() );
  75. }
  76.  
  77. /*** IF PLUGIN IS ENABLED ***/
  78. if ( get_option( 'wcdcp_enable' ) == 'yes' ) {
  79.  
  80.     // Remove cart button from mini-cart
  81.     remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
  82.  
  83.     // Add checks and notices
  84.     add_action( 'admin_notices', 'wcdcp_admin_notices' );
  85.     function wcdcp_admin_notices() {
  86.         if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  87.             ?><div class="notice notice-error"><p><?php _e( 'Warning! To use Disable cart page for WooCommerce it need WooCommerce is installed and active.', 'disable-cart-page-for-woocommerce' ); ?></p></div><?php
  88.         }
  89.     }
  90.  
  91.     // Force WooCommerce to redirect after product added to cart
  92.     add_filter( 'pre_option_woocommerce_cart_redirect_after_add', 'wcdcp_cart_redirect_after_add' );
  93.     function wcdcp_cart_redirect_after_add( $pre_option ) {
  94.         return 'yes';
  95.     }
  96.    
  97.     add_filter( 'woocommerce_product_settings', 'wcdcp_product_settings' );
  98.     function wcdcp_product_settings( $fields ) {
  99.         foreach ( $fields as $key => $field ) {
  100.             if ( $field['id'] === 'woocommerce_cart_redirect_after_add' ) {
  101.                 $fields[$key]['custom_attributes'] = array(
  102.                     'disabled' => true
  103.                 );
  104.             }
  105.         }
  106.         return $fields;
  107.     }
  108.  
  109.     // Empty cart when product is added to cart, so we can't have multiple products in cart
  110.     add_filter( 'woocommerce_add_cart_item_data', 'wcdcp_cart_item_data' );
  111.     function wcdcp_cart_item_data( $cart_item_data ) {
  112.         wc_empty_cart();
  113.         return $cart_item_data;
  114.     };
  115.  
  116.     // When add a product to cart, redirect to checkout
  117.     add_filter( 'woocommerce_add_to_cart_redirect', 'wcdcp_add_to_cart_redirect' );
  118.     function wcdcp_add_to_cart_redirect() {
  119.             return wc_get_checkout_url();
  120.     }
  121.  
  122.     // Remove added to cart message
  123.     add_filter( 'wc_add_to_cart_message_html', '__return_null' );
  124.  
  125.     // If someone reaches the cart page, redirect to checkout permanently
  126.     add_action( 'template_redirect', 'wcdcp_template_redirect' );
  127.     function wcdcp_template_redirect() {
  128.         if ( ! is_cart() ) { return; }
  129.         if ( WC()->cart->get_cart_contents_count() == 0 ) {
  130.             wp_redirect( apply_filters( 'wcdcp_redirect', wc_get_page_permalink( 'shop' ) ) );
  131.             exit;
  132.         }
  133.  
  134.         // Redirect to checkout page
  135.         wp_redirect( wc_get_checkout_url(), '301' );
  136.         exit;
  137.     }
  138.  
  139.     // Change add to cart button text ( in loop )
  140.     add_filter( 'woocommerce_product_add_to_cart_text', 'wcdcp_loop_add_to_cart_text' );
  141.     function wcdcp_loop_add_to_cart_text() {
  142.         return __( 'Buy now', 'disable-cart-page-for-woocommerce' );
  143.     }
  144.  
  145.     // Change add to cart button text ( in product page )
  146.     add_filter( 'woocommerce_product_single_add_to_cart_text', 'wcdcp_product_single_add_to_cart_text' );
  147.     function wcdcp_product_single_add_to_cart_text() {
  148.         return __( 'Buy now', 'disable-cart-page-for-woocommerce' );
  149.     }
  150.  
  151.     // Clear cart if there are errors
  152.     add_action( 'woocommerce_cart_has_errors', 'wcdcp_clear_cart' );
  153.     function wcdcp_clear_cart() {
  154.         wc_empty_cart();
  155.     }
  156.  
  157. }
Add Comment
Please, Sign In to add comment