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