| 1 |
<?php |
| 2 |
namespace WPFunnels\Widgets\Bricks; |
| 3 |
|
| 4 |
use WPFunnels\Wpfnl_functions; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Offer Button Element for Bricks Builder |
| 12 |
* |
| 13 |
* @since 3.0.0 |
| 14 |
*/ |
| 15 |
class OfferButton extends \Bricks\Element { |
| 16 |
|
| 17 |
public $category = 'wpfunnels'; |
| 18 |
public $name = 'wpfnl-offer-button'; |
| 19 |
public $icon = 'ti-mouse'; |
| 20 |
public $css_selector = '.wpfnl-offerbtn-wrapper'; |
| 21 |
public $scripts = ['wpfnlOffer']; |
| 22 |
|
| 23 |
/** |
| 24 |
* Get element label |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_label() { |
| 29 |
return esc_html__( 'Offer Button', 'wpfnl' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Set control groups |
| 34 |
*/ |
| 35 |
public function set_control_groups() { |
| 36 |
$this->control_groups['settings'] = [ |
| 37 |
'title' => esc_html__( 'Settings', 'wpfnl' ), |
| 38 |
'tab' => 'content', |
| 39 |
]; |
| 40 |
|
| 41 |
$this->control_groups['style'] = [ |
| 42 |
'title' => esc_html__( 'Style', 'wpfnl' ), |
| 43 |
'tab' => 'content', |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Set element controls |
| 49 |
*/ |
| 50 |
public function set_controls() { |
| 51 |
// Settings |
| 52 |
$this->controls['offerAction'] = [ |
| 53 |
'tab' => 'content', |
| 54 |
'group' => 'settings', |
| 55 |
'label' => esc_html__( 'Button Action', 'wpfnl' ), |
| 56 |
'type' => 'select', |
| 57 |
'options' => [ |
| 58 |
'accept' => esc_html__( 'Accept Offer', 'wpfnl' ), |
| 59 |
'reject' => esc_html__( 'Reject Offer', 'wpfnl' ), |
| 60 |
], |
| 61 |
'inline' => true, |
| 62 |
'default' => 'accept', |
| 63 |
]; |
| 64 |
|
| 65 |
$this->controls['buttonText'] = [ |
| 66 |
'tab' => 'content', |
| 67 |
'group' => 'settings', |
| 68 |
'label' => esc_html__( 'Button Text', 'wpfnl' ), |
| 69 |
'type' => 'text', |
| 70 |
'default' => esc_html__( 'Yes, Add to My Order!', 'wpfnl' ), |
| 71 |
]; |
| 72 |
|
| 73 |
$this->controls['showProductPrice'] = [ |
| 74 |
'tab' => 'content', |
| 75 |
'group' => 'settings', |
| 76 |
'label' => esc_html__( 'Show Product Price', 'wpfnl' ), |
| 77 |
'type' => 'select', |
| 78 |
'options' => [ |
| 79 |
'no' => esc_html__( 'No', 'wpfnl' ), |
| 80 |
'yes' => esc_html__( 'Yes', 'wpfnl' ), |
| 81 |
], |
| 82 |
'inline' => true, |
| 83 |
'default' => 'no', |
| 84 |
'required' => [ 'offerAction', '=', 'accept' ], |
| 85 |
]; |
| 86 |
|
| 87 |
$this->controls['buttonAlign'] = [ |
| 88 |
'tab' => 'content', |
| 89 |
'group' => 'settings', |
| 90 |
'label' => esc_html__( 'Button Alignment', 'wpfnl' ), |
| 91 |
'type' => 'select', |
| 92 |
'options' => [ |
| 93 |
'left' => esc_html__( 'Left', 'wpfnl' ), |
| 94 |
'center' => esc_html__( 'Center', 'wpfnl' ), |
| 95 |
'right' => esc_html__( 'Right', 'wpfnl' ), |
| 96 |
], |
| 97 |
'inline' => true, |
| 98 |
'default' => 'left', |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Render element |
| 104 |
*/ |
| 105 |
public function render() { |
| 106 |
$settings = $this->settings; |
| 107 |
|
| 108 |
$offer_action = isset( $settings['offerAction'] ) ? $settings['offerAction'] : 'accept'; |
| 109 |
$button_text = isset( $settings['buttonText'] ) ? $settings['buttonText'] : __( 'Yes, Add to My Order!', 'wpfnl' ); |
| 110 |
$show_price = isset( $settings['showProductPrice'] ) ? $settings['showProductPrice'] : 'no'; |
| 111 |
$button_align = isset( $settings['buttonAlign'] ) ? $settings['buttonAlign'] : 'left'; |
| 112 |
|
| 113 |
// Get offer button type from settings or URL |
| 114 |
$offer_button_type = isset( $settings['offerType'] ) ? $settings['offerType'] : 'upsell'; |
| 115 |
|
| 116 |
$this->set_attribute( '_root', 'class', 'wp-block-wpfnl-offer-btn-' . esc_attr( $button_align ) ); |
| 117 |
|
| 118 |
// Get product data |
| 119 |
$response = \WPFunnels\Wpfnl_functions::get_product_data_for_widget( get_the_ID() ); |
| 120 |
$offer_product = isset($response['offer_product']) && $response['offer_product'] ? $response['offer_product'] : ''; |
| 121 |
$get_product_type = isset($response['get_product_type']) && $response['get_product_type'] ? $response['get_product_type'] : ''; |
| 122 |
|
| 123 |
echo "<div {$this->render_attributes( '_root' )}>"; |
| 124 |
?> |
| 125 |
<div class="wpfnl-offerbtn-wrapper" id="wpfnl-offerbtn-wrapper"> |
| 126 |
<?php |
| 127 |
if( ('variable' === $get_product_type || 'variable-subscription' === $get_product_type) && 'accept' === $offer_action ) { |
| 128 |
echo '<div class="has-variation-product">'; |
| 129 |
echo '<div class="wpfnl-product-variation">'; |
| 130 |
if( 'yes' === $show_price ){ |
| 131 |
echo '<span class="offer-btn-loader"></span>'; |
| 132 |
} |
| 133 |
$post_id = get_the_ID(); |
| 134 |
echo do_shortcode( '[wpf_variable_offer post_id="'.$post_id.'"]' ); |
| 135 |
echo '</div>'; |
| 136 |
} |
| 137 |
?> |
| 138 |
|
| 139 |
<div class="wpfnl-offerbtn-and-price-wrapper"> |
| 140 |
<?php if ( $show_price === 'yes' && $offer_action === 'accept' ) : ?> |
| 141 |
<span class="wpfnl-offer-product-price" id="wpfnl-offer-product-price"> |
| 142 |
<?php |
| 143 |
if( ( 'variable' !== $get_product_type && 'variable-subscription' !== $get_product_type ) && 'accept' === $offer_action ){ |
| 144 |
$step_id = get_the_ID(); |
| 145 |
$step_type = get_post_meta( $step_id, '_step_type', true ); |
| 146 |
$products = get_post_meta( $step_id, '_wpfnl_' . $step_type . '_products', true ); |
| 147 |
|
| 148 |
if ( ! empty( $products ) && is_array( $products ) ) { |
| 149 |
$product_id = isset( $products[0]['id'] ) ? $products[0]['id'] : 0; |
| 150 |
if ( $product_id ) { |
| 151 |
$product = wc_get_product( $product_id ); |
| 152 |
if ( $product ) { |
| 153 |
echo $product->get_price_html(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
?> |
| 159 |
</span> |
| 160 |
<?php endif; ?> |
| 161 |
|
| 162 |
<a href="#" |
| 163 |
class="wpfunnels-bricks-widget wpfunnels_offer_button btn" |
| 164 |
id="wpfunnels_<?php echo esc_attr( $offer_button_type ); ?>_<?php echo esc_attr( $offer_action ); ?>" |
| 165 |
data-offertype="<?php echo esc_attr( $offer_button_type ); ?>"> |
| 166 |
<?php echo esc_html( $button_text ); ?> |
| 167 |
</a> |
| 168 |
</div> |
| 169 |
|
| 170 |
<?php |
| 171 |
if( ( 'variable' === $get_product_type || 'variable-subscription' === $get_product_type ) && 'accept' === $offer_action ) { |
| 172 |
echo '</div>'; |
| 173 |
} |
| 174 |
?> |
| 175 |
</div> |
| 176 |
<?php |
| 177 |
echo '</div>'; |
| 178 |
|
| 179 |
// Fire after offer button hook |
| 180 |
if ( $offer_action !== 'reject' && \WPFunnels\Wpfnl_functions::is_wc_active() ) { |
| 181 |
do_action( 'wpfunnels/after_offer_button' ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|