| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Template for Bulk Discounts Single Product |
| 5 |
* |
| 6 |
* @var array $args template args |
| 7 |
* |
| 8 |
* @since 1.4 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
$product = $args['product']; |
| 16 |
$product_id = $product->get_id(); |
| 17 |
?> |
| 18 |
<div class="merchant-volume-discounts"> |
| 19 |
<?php |
| 20 |
$in_cart = 'false'; |
| 21 |
$quantity = 0; |
| 22 |
if ( ! empty( $args['in_cart'] ) ) { |
| 23 |
$in_cart = 'true'; |
| 24 |
} |
| 25 |
if ( ! empty( $args['product_cart_quantity'] ) ) { |
| 26 |
$quantity = $args['product_cart_quantity']; |
| 27 |
} |
| 28 |
$i = 0; |
| 29 |
foreach ( $args['discount_tiers'] as $offer_id => $discount_tier ) : |
| 30 |
if ( isset( $discount_tier['discount_type'], $discount_tier['product_single_page']['save_label'], $discount_tier['product_single_page']['item_text'], $discount_tier['product_single_page']['total_text'] ) ) { |
| 31 |
$is_variable = $product->is_type( 'variable' ); |
| 32 |
$discount_target = $discount_tier['discount_target'] ?? 'sale'; |
| 33 |
|
| 34 |
if ( $is_variable ) { |
| 35 |
$regular_price = (float) $product->get_variation_regular_price( 'min' ); |
| 36 |
$sale_price = (float) $product->get_variation_sale_price( 'min' ); |
| 37 |
} else { |
| 38 |
$regular_price = (float) $product->get_regular_price(); |
| 39 |
$sale_price = (float) $product->get_sale_price(); |
| 40 |
} |
| 41 |
|
| 42 |
$product_price = ( $discount_target === 'regular' || empty( $sale_price ) ) ? $regular_price : $sale_price; |
| 43 |
|
| 44 |
$discount = $discount_tier['discount_type'] === 'percentage_discount' |
| 45 |
? ( $product_price * $discount_tier['discount'] ) / 100 |
| 46 |
: $discount_tier['discount']; |
| 47 |
|
| 48 |
$discount_percent = ( $discount_tier['discount'] ?? 0 ) . '%'; |
| 49 |
$discount_qty = (int) ( $discount_tier['quantity'] ?? 1 ); |
| 50 |
|
| 51 |
$discounted_price = $product_price - $discount; |
| 52 |
$total_discount = $discount_qty * $discount; |
| 53 |
$total_price = $discount_qty * $discounted_price; |
| 54 |
$clickable = ''; |
| 55 |
|
| 56 |
if ( ! $product->is_type( 'variable' ) && $quantity < $discount_qty ) { |
| 57 |
$clickable = ' clickable'; |
| 58 |
} |
| 59 |
|
| 60 |
if ( isset( $discount_tier['product_single_page']['table_title'] ) && ! empty( $discount_tier['product_single_page']['table_title'] ) ) : ?> |
| 61 |
<div class="merchant-volume-discounts-title" style="<?php |
| 62 |
echo isset( $discount_tier['product_single_page']['title_font_weight'] ) ? esc_attr( 'font-weight: ' . $discount_tier['product_single_page']['title_font_weight'] . ';' ) : ''; |
| 63 |
echo isset( $discount_tier['product_single_page']['title_font_size'] ) ? esc_attr( 'font-size: ' . $discount_tier['product_single_page']['title_font_size'] . 'px;' ) : ''; |
| 64 |
echo isset( $discount_tier['product_single_page']['title_text_color'] ) ? esc_attr( 'color: ' . $discount_tier['product_single_page']['title_text_color'] . ';' ) : ''; ?>"><?php |
| 65 |
echo esc_html( Merchant_Translator::translate( $discount_tier['product_single_page']['table_title'] ) ); ?> |
| 66 |
</div> |
| 67 |
<?php |
| 68 |
endif; |
| 69 |
|
| 70 |
$item_classes = 'merchant-volume-discounts-item' . esc_attr( $clickable ); |
| 71 |
$item_classes .= ' merchant-volume-discounts-item-' . esc_attr( $product->get_type() ); |
| 72 |
$item_classes .= ' merchant-volume-discounts-item-' . esc_attr( $i ); |
| 73 |
|
| 74 |
// Get available variations and attributes |
| 75 |
$available_variations = $is_variable ? $product->get_available_variations() : array(); |
| 76 |
$attributes = $is_variable ? $product->get_variation_attributes() : array(); |
| 77 |
?> |
| 78 |
<div |
| 79 |
class="<?php echo esc_attr( $item_classes ); ?>" |
| 80 |
title="<?php echo esc_attr__( 'Add offer to cart', 'merchant' ); ?>" |
| 81 |
data-in-cart="<?php echo esc_attr( $in_cart ); ?>" |
| 82 |
data-product-id="<?php echo esc_attr( $product_id ); ?>" |
| 83 |
data-offer-quantity="<?php echo esc_attr( $discount_qty ); ?>" |
| 84 |
data-flexible-id="<?php echo isset( $discount_tier['flexible_id'] ) ? esc_attr( $discount_tier['flexible_id'] ) : ''; ?>" |
| 85 |
data-offer-id="<?php echo esc_attr( $offer_id ); ?>" data-variations="<?php echo esc_attr( wp_json_encode( $available_variations ) ); ?>" |
| 86 |
data-discount-target="<?php echo esc_attr( $discount_target ); ?>" |
| 87 |
style="<?php |
| 88 |
// Inline CSS variables to use in CSS. Will work per item. |
| 89 |
echo isset( $discount_tier['product_single_page']['table_item_text_color'] ) ? '--merchant-item-text-color:' . esc_attr( $discount_tier['product_single_page']['table_item_text_color'] ). ';' : ''; |
| 90 |
echo isset( $discount_tier['product_single_page']['table_item_bg_color'] ) ? '--merchant-item-bg-color:' . esc_attr( $discount_tier['product_single_page']['table_item_bg_color'] ). ';' : ''; |
| 91 |
echo isset( $discount_tier['product_single_page']['table_item_border_color'] ) ? '--merchant-item-border-color:' . esc_attr( $discount_tier['product_single_page']['table_item_border_color'] ). ';' : ''; |
| 92 |
echo isset( $discount_tier['product_single_page']['table_item_text_color_hover'] ) ? '--merchant-item-text-color-hover:' . esc_attr( $discount_tier['product_single_page']['table_item_text_color_hover'] ). ';' : ''; |
| 93 |
echo isset( $discount_tier['product_single_page']['table_item_bg_color_hover'] ) ? '--merchant-item-bg-color-hover:' . esc_attr( $discount_tier['product_single_page']['table_item_bg_color_hover'] ). ';' : ''; |
| 94 |
echo isset( $discount_tier['product_single_page']['table_item_border_color_hover'] ) ? '--merchant-item-border-color-hover:' . esc_attr( $discount_tier['product_single_page']['table_item_border_color_hover'] ). ';' : ''; ?>"> |
| 95 |
<div class="merchant-volume-discounts-buy-label"> |
| 96 |
<?php |
| 97 |
/** |
| 98 |
* Previously wrong variable `{amount}` was used for this field. Correct one should be `{quantity}`. |
| 99 |
* Still keeping the wrong one `{amount}` for backward compatibility. |
| 100 |
*/ |
| 101 |
if ( isset( $discount_tier['product_single_page']['buy_text'] ) ) { |
| 102 |
echo wp_kses( |
| 103 |
str_replace( |
| 104 |
array( |
| 105 |
'{amount}', |
| 106 |
'{quantity}', |
| 107 |
'{discount}', |
| 108 |
'{percent}', |
| 109 |
), |
| 110 |
array( |
| 111 |
'<strong>' . esc_html( $discount_qty ) . '</strong>', |
| 112 |
'<strong>' . esc_html( $discount_qty ) . '</strong>', |
| 113 |
'<strong>' . wc_price( $discount ) . '</strong>', |
| 114 |
'<strong>' . esc_html( $discount_percent ) . '</strong>', |
| 115 |
), |
| 116 |
esc_html( Merchant_Translator::translate( $discount_tier['product_single_page']['buy_text'] ) ) |
| 117 |
), |
| 118 |
merchant_kses_allowed_tags( array( 'bdi' ) ) |
| 119 |
); |
| 120 |
} else { |
| 121 |
echo wp_kses( |
| 122 |
str_replace( |
| 123 |
array( |
| 124 |
'{quantity}', |
| 125 |
'{discount}', |
| 126 |
), |
| 127 |
array( |
| 128 |
'<strong class="tier-quantity">' . esc_html( $discount_qty ) . '</strong>', |
| 129 |
'<strong class="tier-discount">' . wc_price( $discount ) . '</strong>', |
| 130 |
), |
| 131 |
esc_html__( 'Buy {quantity}, get {discount} off each', 'merchant' ) |
| 132 |
), |
| 133 |
merchant_kses_allowed_tags( array( 'bdi' ) ) |
| 134 |
); |
| 135 |
} |
| 136 |
?> |
| 137 |
</div> |
| 138 |
<ul> |
| 139 |
<li> |
| 140 |
<div></div> |
| 141 |
<div class="merchant-volume-discounts-item-price-strikethrough"> |
| 142 |
<del><?php echo wp_kses( wc_price( $product_price ), merchant_kses_allowed_tags( array( 'bdi' ) ) ); ?></del> |
| 143 |
</div> |
| 144 |
</li> |
| 145 |
<li> |
| 146 |
<div class="merchant-volume-discounts-item-text"> |
| 147 |
<?php echo isset( $discount_tier['product_single_page']['item_text'] ) ? esc_html( Merchant_Translator::translate( $discount_tier['product_single_page']['item_text'] ) ) : esc_html__( 'Per item:', 'merchant' ); ?> |
| 148 |
</div> |
| 149 |
<div class="merchant-volume-discounts-item-price" data-item-price="<?php echo esc_attr( $discounted_price ); ?>"> |
| 150 |
<strong style="color: #ff0000 !important;"> |
| 151 |
<?php echo wp_kses( wc_price( $discounted_price ), merchant_kses_allowed_tags( array( 'bdi' ) ) ); ?> |
| 152 |
</strong> |
| 153 |
</div> |
| 154 |
</li> |
| 155 |
<li> |
| 156 |
<div class="merchant-volume-discounts-total-text"> |
| 157 |
<?php echo isset( $discount_tier['product_single_page']['total_text'] ) |
| 158 |
? esc_html( Merchant_Translator::translate( $discount_tier['product_single_page']['total_text'] ) ) |
| 159 |
: esc_html__( 'Total price:', 'merchant' ); ?> |
| 160 |
</div> |
| 161 |
<div class="merchant-volume-discounts-item-price-total" data-total-price="<?php echo esc_attr( $total_price ); ?>"> |
| 162 |
<strong> |
| 163 |
<?php echo wp_kses( wc_price( $total_price ), merchant_kses_allowed_tags( array( 'bdi' ) ) ); ?> |
| 164 |
</strong> |
| 165 |
</div> |
| 166 |
</li> |
| 167 |
</ul> |
| 168 |
<div class="merchant-volume-discounts-item-label"> |
| 169 |
<span class="merchant-volume-discounts-save-label" |
| 170 |
style="<?php |
| 171 |
echo isset( $discount_tier['product_single_page']['table_label_text_color'] ) ? '--merchant-label-text-color:' . esc_attr( $discount_tier['product_single_page']['table_label_text_color'] ). ';' : ''; |
| 172 |
echo isset( $discount_tier['product_single_page']['table_label_bg_color'] ) ? '--merchant-label-bg-color:' . esc_attr( $discount_tier['product_single_page']['table_label_bg_color'] ). ';' : '';?>"> |
| 173 |
<?php |
| 174 |
if ( isset( $discount_tier['product_single_page']['save_label'] ) ) { |
| 175 |
echo wp_kses( |
| 176 |
str_replace( |
| 177 |
array( |
| 178 |
'{amount}', |
| 179 |
'{percent}', |
| 180 |
), |
| 181 |
array( |
| 182 |
wc_price( $total_discount ), |
| 183 |
esc_html( $discount_percent ), |
| 184 |
), |
| 185 |
esc_html( Merchant_Translator::translate( $discount_tier['product_single_page']['save_label'] ) ) |
| 186 |
), |
| 187 |
merchant_kses_allowed_tags( array( 'bdi' ) ) |
| 188 |
); |
| 189 |
} else { |
| 190 |
echo wp_kses( |
| 191 |
str_replace( |
| 192 |
array( |
| 193 |
'{amount}', |
| 194 |
'{percent}', |
| 195 |
), |
| 196 |
array( |
| 197 |
wc_price( $total_discount ), |
| 198 |
esc_html( $discount_percent ), |
| 199 |
), |
| 200 |
esc_html__( 'Save {amount}', 'merchant' ) |
| 201 |
), |
| 202 |
merchant_kses_allowed_tags( array( 'bdi' ) ) |
| 203 |
); |
| 204 |
} ?> |
| 205 |
</span> |
| 206 |
</div> |
| 207 |
<div class="offer-form"> |
| 208 |
<?php |
| 209 |
if ( $is_variable && ! empty( $attributes ) ) : ?> |
| 210 |
<div class="variation-form"> |
| 211 |
<?php |
| 212 |
foreach ( $attributes as $attribute_name => $options ) { |
| 213 |
echo '<div class="variations variation-dropdown">'; |
| 214 |
wc_dropdown_variation_attribute_options( |
| 215 |
array( |
| 216 |
'options' => $options, |
| 217 |
'attribute' => $attribute_name, |
| 218 |
'product' => $product, |
| 219 |
'required' => true, |
| 220 |
/* Translators: 1. Attribute name */ |
| 221 |
'show_option_none' => sprintf( __( 'Select %s', 'merchant' ), wc_attribute_label( $attribute_name ) ), |
| 222 |
) |
| 223 |
); |
| 224 |
echo '</div>'; |
| 225 |
} |
| 226 |
?> |
| 227 |
</div> |
| 228 |
<?php endif; ?> |
| 229 |
|
| 230 |
<?php if ( $is_variable ) : ?> |
| 231 |
<div class="form-footer"> |
| 232 |
<div class="offer-quantity-input"> |
| 233 |
<?php |
| 234 |
woocommerce_quantity_input( array( |
| 235 |
'input_name' => 'offer-quantity', |
| 236 |
'input_value' => Merchant_Pro_Volume_Discounts::offer_dynamic_remaining_quantity( $discount_tier, $product ), |
| 237 |
'min_value' => $discount_qty, |
| 238 |
) ) ?> |
| 239 |
</div> |
| 240 |
<div class="offer-submit"> |
| 241 |
<button type="submit" class="single_add_to_cart_button button alt"> |
| 242 |
<span class="offer-submit-text"><?php esc_html_e( 'Add to cart', 'merchant' ); ?></span> |
| 243 |
</button> |
| 244 |
</div> |
| 245 |
</div> |
| 246 |
<?php endif; ?> |
| 247 |
</div> |
| 248 |
<div class="user-message"><span class="message-text"></span></div> |
| 249 |
</div> |
| 250 |
<?php |
| 251 |
} |
| 252 |
++$i ; |
| 253 |
endforeach; ?> |
| 254 |
</div> |