PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.0.4
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.0.4
1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 1.3.44 All 176 releases
disco / functions / common.php

common.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.0.4, at functions/common.php

228 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Disco
4 *
5 * @package Disco
6 * @author Ohidul Islam <wahid0003@gmail.com>
7 * @link http://domain.tld
8 * @license GPL 2.0+
9 * @copyright 2022 WebAppick
10 */
11
12 use Disco\App\Disco;
13
14
15
16 if ( ! function_exists( 'disco_update_cart_price_by_campaigns' ) ) {
17
18 /**
19 * Run the Cart Intent campaigns before cart.
20 *
21 * @param \WC_Cart $cart Cart Object.
22 * @return \WC_Cart
23 */
24 function disco_add_cart_discount( $cart ) {
25 return ( new Disco )->get_cart_discount( $cart );
26 }
27
28 // Temporarily disabled: add_action( 'woocommerce_cart_calculate_fees', 'disco_add_cart_discount', 1, 1 );
29 // To apply a cart discount based on certain conditions, uncomment the above line.
30
31 }
32
33 // Set Cart Items discounts.
34 if ( ! function_exists( 'disco_set_cart_item_price' ) ) {
35
36 /**
37 * @return bool
38 */
39 function disco_set_cart_item_price() {
40 $disco = new Disco;
41
42 return $disco->get_cart_items_discount();
43 }
44
45 // add_action( 'woocommerce_before_calculate_totals', 'disco_set_cart_item_price' );
46 // To apply this action, uncomment the above line.
47 }
48
49 /**
50 * Add a free product to the cart.
51 *
52 * @param \WC_Cart $cart Cart Object.
53 * @return \WC_Cart
54 */
55 function disco_cart_loaded_callback( $cart ) {
56 // Get the product object of the product you want to duplicate
57 $free_items = array(
58 'key' => '32',
59 );
60
61 foreach ( $free_items as $item_key => $item ) { //phpcs:ignore
62 $product_id = $item;
63 $quantity = 1;
64 $new_item_key = $cart->add_to_cart( $product_id, $quantity, 0, array(), array( 'free' => 'yes' ) );
65 $cart_item = $cart->get_cart_item( $new_item_key );
66 $cart_item['data']->set_price( 0 );
67 $cart->set_quantity( $new_item_key, 1 );
68 }
69
70 return $cart;
71 }
72
73 // Add the callback function to the hook
74 // add_action( 'woocommerce_cart_loaded_from_session', 'disco_cart_loaded_callback' );
75
76 if ( ! function_exists( 'disco_run_after_cart' ) ) {
77
78 /**
79 * Display Cart Item Sale Price & Regular Price.
80 *
81 * @param string $price Cart Item Price.
82 * @param array $cart_item Cart Item.
83 * @return string
84 */
85 function disco_display_regular_and_sale_price_in_cart_item( $price, $cart_item ) {
86 $product_id = $cart_item['product_id'];
87 $product = wc_get_product( $product_id );
88 $regular_price = $product->get_price();
89 $sale_price = $cart_item['data']->get_price();
90
91 if ( isset( $cart_item['free'] ) && 'yes' === $cart_item['free'] ) {
92 $cart_item['data']->set_price( 0 );
93
94 return 0;
95 }
96
97 $offers = $cart_item['data']->get_meta( '_item_offers' );
98
99 if ( $sale_price && $sale_price < $regular_price ) {
100 $price = ' < del > ' . wc_price( $regular_price ) . ' < / del > < ins > ' . wc_price( $sale_price ) . ' < / ins > ';
101 }
102
103 if ( ! empty( $offers ) && isset( $offers['key'] ) ) {
104 $key = $offers['key'];
105 $bogo_qty = $offers['bogo_qty'][ $key ];
106
107 if ( ! empty( $bogo_qty ) ) {
108 $price = $bogo_qty;
109 }
110 }
111
112 return $price;
113 }
114
115 // add_filter( 'woocommerce_cart_item_price', 'disco_display_regular_and_sale_price_in_cart_item', 10, 2 );
116 // To apply this action, uncomment the above line.
117
118 }
119
120 if ( ! function_exists( 'disco_display_subtotal_regular_and_sale_price_in_cart_item' ) ) {
121
122 /**
123 * Display Cart Item Subtotal Sale Price & Regular Price.
124 *
125 * @param string $subtotal Cart Item Subtotal.
126 * @param array $cart_item Cart Item.
127 * @return string
128 */
129 function disco_display_subtotal_regular_and_sale_price_in_cart_item( $subtotal, $cart_item ) {
130 $product_id = $cart_item['product_id'];
131 $quantity = $cart_item['quantity'];
132 $product = wc_get_product( $product_id );
133 $regular_price = $product->get_price() * $quantity;
134 $sale_price = $cart_item['data']->get_price() * $quantity;
135
136 if ( $sale_price && $sale_price < $regular_price ) {
137 $subtotal = '<del>' . wc_price( $regular_price ) . '</del><ins>' . wc_price( $sale_price ) . '</ins>';
138 }
139
140 return $subtotal;
141 }
142
143 // add_filter( 'woocommerce_cart_item_subtotal', 'disco_display_subtotal_regular_and_sale_price_in_cart_item', 10, 2 );
144 // To apply this action, uncomment the above line.
145 }
146
147 if ( ! function_exists( 'disco_cart_item_quantity_notice' ) ) {
148
149 /**
150 * Display Cart Item Quantity Badge.
151 *
152 * @param string $quantity_html Cart Item Quantity HTML.
153 * @param array $cart_item Cart Item.
154 * @return string
155 */
156 function disco_cart_item_quantity_notice( $quantity_html, $cart_item ) {//phpcs:ignore
157 // Wrap the quantity in a badge element
158 $item = WC()->cart->get_cart()[ $cart_item ];
159 $offers = $item['data']->get_meta( '_item_offers' );
160
161 if ( ! empty( $offers['price'] ) ) {
162 $message = sprintf( 'Add % s get % s off', $offers['quantity'], wc_price( $offers['price'] ) );
163 $quantity_html .= sprintf( '<span title="Add 3 get 1 Free" class="quantity-badge">%s</span>', $message );
164 }
165
166 return $quantity_html;
167 }
168
169 // add_filter( 'woocommerce_cart_item_quantity', 'disco_cart_item_quantity_notice', 10, 2 );
170 // To apply this action, uncomment the above line.
171 }
172
173 if ( ! function_exists( 'disco_add_button_to_cart_item_name' ) ) {
174
175 /**
176 * Add a button to the cart item name.
177 *
178 * @param string $product_name Product Name.
179 * @return string
180 */
181 function disco_add_button_to_cart_item_name( $product_name ) {
182 $button = ( new Disco )->get_offers(); // Replace '// ' with your desired link.
183
184 return $product_name . '<br/>' . $button; // This will add the button after the product name.
185 }
186
187 // add_filter( 'woocommerce_cart_item_name', 'disco_add_button_to_cart_item_name', 10, 1 );
188 // To apply this filter, uncomment the above line.
189 }
190
191 if ( ! function_exists( 'disco_override_shipping_rates_to_free_shipping' ) ) {
192
193 /**
194 * @param array $rates Array of rates found for the package.
195 * @param array $package Array of package information.
196 * @return array
197 */
198 function disco_override_shipping_rates_to_free_shipping( $rates, $package ) {
199 $is_free_shipping = ( new Disco )->apply_free_shipping( WC()->cart );
200
201 if ( ! $is_free_shipping ) {
202 return $rates;
203 }
204
205 // Check if there are any existing rates
206 if ( ! empty( $rates ) ) {
207 foreach ( $rates as $rate_key => $rate ) {
208 // Add only the first-rate but change it to free shipping
209 if ( 'free_shipping' === $rate->method_id ) {
210 unset( $rates[ $rate_key ] );
211 } else {
212 $rate->label = 'Free Shipping';
213 $rate->cost = 0;
214 $rate->taxes = array();
215 $rate->id = 'free_shipping';
216 $rate->method_id = 'free_shipping';
217 $rate->package = $package;
218 }
219 }
220 }
221
222 return $rates;
223 }
224
225 // add_filter( 'woocommerce_package_rates', 'disco_override_shipping_rates_to_free_shipping', 100, 2 );
226 // To apply this filter, uncomment the above line.
227 }
228