-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-product-bundles-top-add-to-cart-button.php
134 lines (110 loc) · 3.55 KB
/
woocommerce-product-bundles-top-add-to-cart-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
* Plugin Name: WooCommerce Product Bundles - Top Add to Cart Button
* Plugin URI: http://woocommerce.com/products/product-bundles/
* Description: Adds an add-to-cart button section at the top of Product Bundle pages.
* Version: 1.0.3
* Author: SomewhereWarm
* Author URI: http://somewherewarm.gr/
*
* Text Domain: woocommerce-product-bundles-top-add-to-cart-button
* Domain Path: /languages/
*
* Requires at least: 4.1
* Tested up to: 4.9
*
* WC requires at least: 3.0
* WC tested up to: 3.5
*
* Copyright: © 2017 SomewhereWarm SMPC.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_PB_Top_Add_To_Cart {
/**
* Version.
* @var string
*/
public static $version = '1.0.3';
/**
* Required PB version.
* @var string
*/
public static $req_pb_version = '5.5';
/**
* Plugin URL.
*/
public static function plugin_url() {
return plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) );
}
/**
* Plugin path.
*/
public static function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Entry point.
*/
public static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'load_plugin' ) );
}
/**
* Lights on.
*/
public static function load_plugin() {
if ( ! function_exists( 'WC_PB' ) || version_compare( WC_PB()->version, self::$req_pb_version ) < 0 ) {
add_action( 'admin_notices', array( __CLASS__, 'version_notice' ) );
return false;
}
// Localize plugin.
add_action( 'init', array( __CLASS__, 'localize_plugin' ) );
// Hook extra add-to-cart section.
add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'top_add_to_cart' ), 25 );
// Enqueue scripts and styles.
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'script' ) );
}
/**
* Load textdomain.
*
* @return void
*/
public static function localize_plugin() {
load_plugin_textdomain( 'woocommerce-product-bundles-top-add-to-cart-button', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* PB version check notice.
*/
public static function version_notice() {
echo '<div class="error"><p>' . sprintf( __( '<strong>WooCommerce Product Bundles – Top Add To Cart Button</strong> requires Product Bundles <strong>%s</strong> or higher.', 'woocommerce-product-bundles-top-add-to-cart-button' ), self::$req_pb_version ) . '</p></div>';
}
/**
* Show extra add-to-cart button section.
*/
public static function top_add_to_cart() {
global $product;
if ( is_a( $product, 'WC_Product_Bundle' ) ) {
$elements = apply_filters( 'woocommerce_bundles_top_add_to_cart_elements', array( 'error', 'availability', 'button' ), $product );
$elements_html = '';
foreach ( $elements as $el ) {
$elements_html .= '<div class="bundle_' . $el . '"' . ( 'error' === $el ? ' style="display:none"' : '' ) . '></div>';
}
if ( ! empty( $elements_html ) ) {
echo '<form method="post" class="cart bundle_form bundle_form_top"><div class="bundle_wrap">' . $elements_html . '</div></form>';
}
}
}
/**
* Script.
*/
public static function script() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'wc-pb-top-add-to-cart-button', self::plugin_url() . '/assets/js/wc-pb-top-add-to-cart-button' . $suffix . '.js', array( 'wc-add-to-cart-bundle' ), self::$version, true );
wp_enqueue_script( 'wc-pb-top-add-to-cart-button' );
}
}
WC_PB_Top_Add_To_Cart::init();