| @@ -20,25 +20,45 @@ | ||
| 20 | 20 | */ |
| 21 | 21 | public function __construct() { |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - public function showUpsell( $cartItem ) { | |
| 24 | + public function showUpsell( $cartItem, $cartItemKey = '' ) { | |
| 25 | 25 | if ( !$this->isCartUpsellEnabled() ) { |
| 26 | 26 | return; |
| 27 | 27 | } |
| 28 | - $upsellString = $this->formatUpsellString( $cartItem ); | |
| 28 | + $nextPriceData = $this->getNextPriceData( $cartItem ); | |
| 29 | + $upsellString = $this->formatUpsellString( $cartItem, $nextPriceData, $cartItemKey ); | |
| 29 | 30 | if ( !$upsellString ) { |
| 30 | 31 | return; |
| 31 | 32 | } |
| 33 | + // the link carries the target quantity for the cart script | |
| 34 | + $allowed = wp_kses_allowed_html( 'post' ); | |
| 35 | + $allowed['a']['data-tpt-cart-item'] = true; | |
| 36 | + $allowed['a']['data-tpt-quantity'] = true; | |
| 37 | + $current = (int) $this->getTotalProductCountInCart( $cartItem ); | |
| 38 | + $target = $current + (int) $nextPriceData['next_quantity']; | |
| 32 | 39 | ?> |
| 33 | - <div> | |
| 34 | - <small style="color: <?php | |
| 40 | + <div class="tpt-cart-upsell" style="color: <?php | |
| 35 | 41 | echo esc_attr( $this->getUpsellColor() ); |
| 36 | 42 | ?>"> |
| 37 | - <?php | |
| 38 | - echo wp_kses_post( $upsellString ); | |
| 43 | + <small><?php | |
| 44 | + echo wp_kses( $upsellString, $allowed ); | |
| 45 | + ?></small> | |
| 46 | + <?php | |
| 47 | + if ( $this->isProgressBarEnabled() && $target > 0 ) { | |
| 48 | + ?> | |
| 49 | + <div class="tpt-cart-upsell__progress" role="progressbar" aria-valuemin="0" aria-valuemax="<?php | |
| 50 | + echo esc_attr( $target ); | |
| 51 | + ?>" aria-valuenow="<?php | |
| 52 | + echo esc_attr( $current ); | |
| 53 | + ?>"> | |
| 54 | + <i style="width: <?php | |
| 55 | + echo esc_attr( max( 2, min( 100, round( $current / $target * 100 ) ) ) ); | |
| 56 | + ?>%"></i> | |
| 57 | + </div> | |
| 58 | + <?php | |
| 59 | + } | |
| 39 | 60 | ?> |
| 40 | - </small> | |
| 41 | 61 | </div> |
| 42 | 62 | <?php |
| 43 | 63 | } |
| 44 | 64 | |
| @@ -68,16 +88,31 @@ | ||
| 68 | 88 | ); |
| 69 | 89 | return $itemData; |
| 70 | 90 | } |
| 71 | 91 | |
| 72 | - protected function formatUpsellString( $cartItem ) { | |
| 73 | - $nextPriceData = $this->getNextPriceData( $cartItem ); | |
| 92 | + /** | |
| 93 | + * @param array $cartItem | |
| 94 | + * @param array|null $nextPriceData The next tier's figures, when already computed. | |
| 95 | + * @param string $cartItemKey Set for the classic cart: the required quantity becomes a link that | |
| 96 | + * moves the line to the next tier. | |
| 97 | + * | |
| 98 | + * @return string|false | |
| 99 | + */ | |
| 100 | + protected function formatUpsellString( $cartItem, $nextPriceData = null, $cartItemKey = '' ) { | |
| 101 | + if ( null === $nextPriceData ) { | |
| 102 | + $nextPriceData = $this->getNextPriceData( $cartItem ); | |
| 103 | + } | |
| 74 | 104 | if ( empty( $nextPriceData ) ) { |
| 75 | 105 | return false; |
| 76 | 106 | } |
| 77 | 107 | $template = $this->getTemplate(); |
| 108 | + $quantity = (string) $nextPriceData['next_quantity']; | |
| 109 | + if ( $cartItemKey && $this->isQuantityLinkEnabled() ) { | |
| 110 | + $target = (int) $cartItem['quantity'] + (int) $nextPriceData['next_quantity']; | |
| 111 | + $quantity = '<a href="#" class="tpt-cart-upsell__link" data-tpt-cart-item="' . esc_attr( $cartItemKey ) . '" data-tpt-quantity="' . esc_attr( $target ) . '">' . $quantity . '</a>'; | |
| 112 | + } | |
| 78 | 113 | return strtr( $template, array( |
| 79 | - '{tp_required_quantity}' => $nextPriceData['next_quantity'], | |
| 114 | + '{tp_required_quantity}' => $quantity, | |
| 80 | 115 | '{tp_next_price}' => wc_price( $nextPriceData['next_price'] ), |
| 81 | 116 | '{tp_next_discount}' => number_format( |
| 82 | 117 | $nextPriceData['next_discount'], |
| 83 | 118 | wc_get_price_decimals(), |
| @@ -168,16 +203,24 @@ | ||
| 168 | 203 | return ( !empty( $cartItem['variation_id'] ) ? $cartItem['variation_id'] : $cartItem['product_id'] ); |
| 169 | 204 | } |
| 170 | 205 | |
| 171 | 206 | protected function getTemplate() { |
| 172 | - return $this->getContainer()->getSettings()->get( 'cart_upsell_template', __( 'Buy <b>{tp_required_quantity}</b> more to get <b>{tp_next_price}</b> each', 'tier-pricing-table' ) ); | |
| 207 | + return $this->getContainer()->getSettings()->get( 'cart_upsell_template', __( 'Add <b>{tp_required_quantity}</b> more to get <b>{tp_next_price}</b> each', 'tier-pricing-table' ) ); | |
| 173 | 208 | } |
| 174 | 209 | |
| 175 | 210 | protected function getUpsellColor() { |
| 176 | - return $this->getContainer()->getSettings()->get( 'cart_upsell_color', '#3858e9' ); | |
| 211 | + return $this->getContainer()->getSettings()->get( 'cart_upsell_color', '#059669' ); | |
| 177 | 212 | } |
| 178 | 213 | |
| 179 | 214 | protected function isCartUpsellEnabled() { |
| 180 | 215 | return $this->getContainer()->getSettings()->get( 'cart_upsell_enabled', 'no' ) === 'yes'; |
| 216 | + } | |
| 217 | + | |
| 218 | + protected function isQuantityLinkEnabled() : bool { | |
| 219 | + return $this->getContainer()->getSettings()->get( 'cart_upsell_link', 'yes' ) !== 'no'; | |
| 220 | + } | |
| 221 | + | |
| 222 | + protected function isProgressBarEnabled() : bool { | |
| 223 | + return $this->getContainer()->getSettings()->get( 'cart_upsell_progress', 'no' ) === 'yes'; | |
| 181 | 224 | } |
| 182 | 225 | |
| 183 | 226 | } |