| @@ -1,5 +1,5 @@ | ||
| 1 | -<?php // phpcs:disable | |
| 1 | +<?php | |
| 2 | 2 | /** |
| 3 | 3 | * Disco |
| 4 | 4 | * |
| 5 | 5 | * @package Disco |
| @@ -18,14 +18,14 @@ | ||
| 18 | 18 | |
| 19 | 19 | if ( ! function_exists( 'disco_update_cart_price_by_campaigns' ) ) { |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Run the Cart Intent campaigns before cart. | |
| 23 | - * | |
| 24 | - * @param \WC_Cart $cart Cart Object. | |
| 22 | + * Run the Cart Intent campaigns before cart. | |
| 23 | + * | |
| 24 | + * @param \WC_Cart $cart Cart Object. | |
| 25 | 25 | * @return \WC_Cart |
| 26 | - * @throws \Exception If the campaign is not found. | |
| 27 | - */ | |
| 26 | + * @throws \Exception If the campaign is not found. | |
| 27 | + */ | |
| 28 | 28 | function disco_add_cart_discount( $cart ) { |
| 29 | 29 | return ( new Disco )->get_cart_discount( $cart ); |
| 30 | 30 | } |
| 31 | 31 | |
| @@ -51,8 +51,36 @@ | ||
| 51 | 51 | |
| 52 | 52 | // add_action( 'woocommerce_before_calculate_totals', 'disco_set_cart_item_price',1,1 ); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | +/** | |
| 56 | + * Add a free product to the cart. | |
| 57 | + * | |
| 58 | + * @param \WC_Cart $cart Cart Object. | |
| 59 | + * @return \WC_Cart | |
| 60 | + */ | |
| 61 | +function disco_cart_loaded_callback( $cart ) { | |
| 62 | + // Get the product object of the product you want to duplicate | |
| 63 | + // TODO: Dynamically add free items from the campaign. | |
| 64 | + $free_items = array( | |
| 65 | + 'key' => '32', | |
| 66 | + ); | |
| 67 | + | |
| 68 | + foreach ( $free_items as $item_key => $item ) { //phpcs:ignore | |
| 69 | + $product_id = $item; | |
| 70 | + $quantity = 1; | |
| 71 | + $new_item_key = $cart->add_to_cart( $product_id, $quantity, 0, array(), array( 'free' => 'yes' ) ); | |
| 72 | + $cart_item = $cart->get_cart_item( $new_item_key ); | |
| 73 | + $cart_item['data']->set_price( 0 ); | |
| 74 | + $cart->set_quantity( $new_item_key, 1 ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + return $cart; | |
| 78 | +} | |
| 79 | + | |
| 80 | +// Add the callback function to the hook | |
| 81 | +// add_action( 'woocommerce_cart_loaded_from_session', 'disco_cart_loaded_callback' ); | |
| 82 | + | |
| 55 | 83 | if ( ! function_exists( 'disco_run_after_cart' ) ) { |
| 56 | 84 | |
| 57 | 85 | /** |
| 58 | 86 | * Display Cart Item Sale Price & Regular Price. |
| @@ -129,15 +157,15 @@ | ||
| 129 | 157 | * @param array $cart_item Cart Item. |
| 130 | 158 | * @return string |
| 131 | 159 | */ |
| 132 | 160 | function disco_cart_item_quantity_notice( $quantity_html, $cart_item ) {//phpcs:ignore |
| 133 | - // Wrap the quantity in a badge-images element | |
| 161 | + // Wrap the quantity in a badge element | |
| 134 | 162 | $item = WC()->cart->get_cart()[ $cart_item ]; |
| 135 | 163 | $offers = $item['data']->get_meta( '_item_offers' ); |
| 136 | 164 | |
| 137 | 165 | if ( ! empty( $offers['price'] ) ) { |
| 138 | 166 | $message = sprintf( 'Add % s get % s off', $offers['quantity'], wc_price( $offers['price'] ) ); |
| 139 | - $quantity_html .= sprintf( '<span title="Add 3 get 1 Free" class="quantity-badge-images">%s</span>', $message ); | |
| 167 | + $quantity_html .= sprintf( '<span title="Add 3 get 1 Free" class="quantity-badge">%s</span>', $message ); | |
| 140 | 168 | } |
| 141 | 169 | |
| 142 | 170 | return $quantity_html; |
| 143 | 171 | } |