PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.1.9
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.1.9
1.4.15 1.4.14 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 All 178 releases
disco / functions / common.php

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

192 lines 5.4 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 // Ensure the file is not accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 use Disco\App\Disco;
18
19 if ( ! function_exists( 'disco_update_cart_price_by_campaigns' ) ) {
20
21 /**
22 * Run the Cart Intent campaigns before cart.
23 *
24 * @param \WC_Cart $cart Cart Object.
25 * @return \WC_Cart
26 * @throws \Exception If the campaign is not found.
27 */
28 function disco_add_cart_discount( $cart ) {
29 return ( new Disco )->get_cart_discount( $cart );
30 }
31
32 add_action( 'woocommerce_cart_calculate_fees', 'disco_add_cart_discount', 1, 1 );
33 }
34
35 // Set Cart Items discounts.
36 if ( ! function_exists( 'disco_set_cart_item_price' ) ) {
37
38 /**
39 * @return \WC_Cart
40 */
41 function disco_set_cart_item_price() {
42 // echo "<pre>";
43 // print_r( ( new Disco )->get_cart_items_discount() );
44 // echo "</pre>";
45 // die();
46
47 return ( new Disco )->get_cart_items_discount();
48 }
49
50 add_action( 'woocommerce_before_calculate_totals', 'disco_set_cart_item_price' );
51 }
52
53 /**
54 * Add a free product to the cart.
55 *
56 * @param \WC_Cart $cart Cart Object.
57 * @return \WC_Cart
58 */
59 function disco_cart_loaded_callback( $cart ) {
60 // Get the product object of the product you want to duplicate
61 // TODO: Dynamically add free items from the campaign.
62 $free_items = array(
63 'key' => '32',
64 );
65
66 foreach ( $free_items as $item_key => $item ) { //phpcs:ignore
67 $product_id = $item;
68 $quantity = 1;
69 $new_item_key = $cart->add_to_cart( $product_id, $quantity, 0, array(), array( 'free' => 'yes' ) );
70 $cart_item = $cart->get_cart_item( $new_item_key );
71 $cart_item['data']->set_price( 0 );
72 $cart->set_quantity( $new_item_key, 1 );
73 }
74
75 return $cart;
76 }
77
78 // Add the callback function to the hook
79 // add_action( 'woocommerce_cart_loaded_from_session', 'disco_cart_loaded_callback' );
80
81 if ( ! function_exists( 'disco_run_after_cart' ) ) {
82
83 /**
84 * Display Cart Item Sale Price & Regular Price.
85 *
86 * @param string $price Cart Item Price.
87 * @param array $cart_item Cart Item.
88 * @return string
89 */
90 function disco_display_regular_and_sale_price_in_cart_item( $price, $cart_item ) {
91 $product_id = $cart_item['product_id'];
92 $product = wc_get_product( $product_id );
93 $regular_price = $product->get_price();
94 $sale_price = $cart_item['data']->get_price();
95
96 if ( isset( $cart_item['free'] ) && 'yes' === $cart_item['free'] ) {
97 $cart_item['data']->set_price( 0 );
98
99 return 0;
100 }
101
102 $offers = $cart_item['data']->get_meta( '_item_offers' );
103
104 if ( $sale_price && $sale_price < $regular_price ) {
105 $price = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>';
106 }
107
108 if ( ! empty( $offers ) && isset( $offers['key'] ) ) {
109 $key = $offers['key'];
110 $bogo_qty = $offers['bogo_qty'][ $key ];
111
112 if ( ! empty( $bogo_qty ) ) {
113 $price = $bogo_qty;
114 }
115 }
116
117 return $price;
118 }
119
120 add_filter( 'woocommerce_cart_item_price', 'disco_display_regular_and_sale_price_in_cart_item', 10, 2 );
121 }
122
123 if ( ! function_exists( 'disco_display_subtotal_regular_and_sale_price_in_cart_item' ) ) {
124
125 /**
126 * Display Cart Item Subtotal Sale Price & Regular Price.
127 *
128 * @param string $subtotal Cart Item Subtotal.
129 * @param array $cart_item Cart Item.
130 * @return string
131 */
132 function disco_display_subtotal_regular_and_sale_price_in_cart_item( $subtotal, $cart_item ) {
133 $product_id = $cart_item['product_id'];
134 $quantity = $cart_item['quantity'];
135 $product = wc_get_product( $product_id );
136 $regular_price = $product->get_price() * $quantity;
137 $sale_price = $cart_item['data']->get_price() * $quantity;
138
139 if ( $sale_price && $sale_price < $regular_price ) {
140 $subtotal = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>';
141 }
142
143 return $subtotal;
144 }
145
146 add_filter( 'woocommerce_cart_item_subtotal', 'disco_display_subtotal_regular_and_sale_price_in_cart_item', 10, 2 );
147 }
148
149 if ( ! function_exists( 'disco_cart_item_quantity_notice' ) ) {
150
151 /**
152 * Display Cart Item Quantity Badge.
153 *
154 * @param string $quantity_html Cart Item Quantity HTML.
155 * @param array $cart_item Cart Item.
156 * @return string
157 */
158 function disco_cart_item_quantity_notice( $quantity_html, $cart_item ) {//phpcs:ignore
159 // Wrap the quantity in a badge element
160 $item = WC()->cart->get_cart()[ $cart_item ];
161 $offers = $item['data']->get_meta( '_item_offers' );
162
163 if ( ! empty( $offers['price'] ) ) {
164 $message = sprintf( 'Add % s get % s off', $offers['quantity'], wc_price( $offers['price'] ) );
165 $quantity_html .= sprintf( '<span title="Add 3 get 1 Free" class="quantity-badge">%s</span>', $message );
166 }
167
168 return $quantity_html;
169 }
170
171 // add_filter( 'woocommerce_cart_item_quantity', 'disco_cart_item_quantity_notice', 10, 2 );
172 // To apply this action, uncomment the above line.
173 }
174
175 if ( ! function_exists( 'disco_add_button_to_cart_item_name' ) ) {
176
177 /**
178 * Add a button to the cart item name.
179 *
180 * @param string $product_name Product Name.
181 * @return string
182 */
183 function disco_add_button_to_cart_item_name( $product_name ) {
184 $button = ( new Disco )->get_offers(); // Replace '// ' with your desired link.
185
186 return $product_name . '<br/>' . $button; // This will add the button after the product name.
187 }
188
189 // add_filter( 'woocommerce_cart_item_name', 'disco_add_button_to_cart_item_name', 10, 1 );
190 // To apply this filter, uncomment the above line.
191 }
192