PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.9
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.9
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.2.9, at functions/common.php

194 lines 5.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 // 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 * Set Cart Items discounts.
40 *
41 * @param \WC_Cart $cart Cart Object.
42 */
43 function disco_set_cart_item_price( $cart ) {
44 // echo "<pre>";
45 // print_r( ( new Disco )->get_cart_items_discount($cart) );
46 // echo "</pre>";
47 // die();
48
49 // return ( new Disco )->get_cart_items_discount();
50 }
51
52 // add_action( 'woocommerce_before_calculate_totals', 'disco_set_cart_item_price',1,1 );
53 }
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
83 if ( ! function_exists( 'disco_run_after_cart' ) ) {
84
85 /**
86 * Display Cart Item Sale Price & Regular Price.
87 *
88 * @param string $price Cart Item Price.
89 * @param array $cart_item Cart Item.
90 * @return string
91 */
92 function disco_display_regular_and_sale_price_in_cart_item( $price, $cart_item ) {
93 $product_id = $cart_item['product_id'];
94 $product = wc_get_product( $product_id );
95 $regular_price = $product->get_price();
96 $sale_price = $cart_item['data']->get_price();
97
98 if ( isset( $cart_item['free'] ) && 'yes' === $cart_item['free'] ) {
99 $cart_item['data']->set_price( 0 );
100
101 return 0;
102 }
103
104 $offers = $cart_item['data']->get_meta( '_item_offers' );
105
106 if ( $sale_price && $sale_price < $regular_price ) {
107 $price = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>';
108 }
109
110 if ( ! empty( $offers ) && isset( $offers['key'] ) ) {
111 $key = $offers['key'];
112 $bogo_qty = $offers['bogo_qty'][ $key ];
113
114 if ( ! empty( $bogo_qty ) ) {
115 $price = $bogo_qty;
116 }
117 }
118
119 return $price;
120 }
121
122 // add_filter( 'woocommerce_cart_item_price', 'disco_display_regular_and_sale_price_in_cart_item', 10, 2 );
123 }
124
125 if ( ! function_exists( 'disco_display_subtotal_regular_and_sale_price_in_cart_item' ) ) {
126
127 /**
128 * Display Cart Item Subtotal Sale Price & Regular Price.
129 *
130 * @param string $subtotal Cart Item Subtotal.
131 * @param array $cart_item Cart Item.
132 * @return string
133 */
134 function disco_display_subtotal_regular_and_sale_price_in_cart_item( $subtotal, $cart_item ) {
135 $product_id = $cart_item['product_id'];
136 $quantity = $cart_item['quantity'];
137 $product = wc_get_product( $product_id );
138 $regular_price = $product->get_price() * $quantity;
139 $sale_price = $cart_item['data']->get_price() * $quantity;
140
141 if ( $sale_price && $sale_price < $regular_price ) {
142 $subtotal = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>';
143 }
144
145 return $subtotal;
146 }
147
148 // add_filter( 'woocommerce_cart_item_subtotal', 'disco_display_subtotal_regular_and_sale_price_in_cart_item', 10, 2 );
149 }
150
151 if ( ! function_exists( 'disco_cart_item_quantity_notice' ) ) {
152
153 /**
154 * Display Cart Item Quantity Badge.
155 *
156 * @param string $quantity_html Cart Item Quantity HTML.
157 * @param array $cart_item Cart Item.
158 * @return string
159 */
160 function disco_cart_item_quantity_notice( $quantity_html, $cart_item ) {//phpcs:ignore
161 // Wrap the quantity in a badge element
162 $item = WC()->cart->get_cart()[ $cart_item ];
163 $offers = $item['data']->get_meta( '_item_offers' );
164
165 if ( ! empty( $offers['price'] ) ) {
166 $message = sprintf( 'Add % s get % s off', $offers['quantity'], wc_price( $offers['price'] ) );
167 $quantity_html .= sprintf( '<span title="Add 3 get 1 Free" class="quantity-badge">%s</span>', $message );
168 }
169
170 return $quantity_html;
171 }
172
173 // add_filter( 'woocommerce_cart_item_quantity', 'disco_cart_item_quantity_notice', 10, 2 );
174 // To apply this action, uncomment the above line.
175 }
176
177 if ( ! function_exists( 'disco_add_button_to_cart_item_name' ) ) {
178
179 /**
180 * Add a button to the cart item name.
181 *
182 * @param string $product_name Product Name.
183 * @return string
184 */
185 function disco_add_button_to_cart_item_name( $product_name ) {
186 $button = ( new Disco )->get_offers(); // Replace '// ' with your desired link.
187
188 return $product_name . '<br/>' . $button; // This will add the button after the product name.
189 }
190
191 // add_filter( 'woocommerce_cart_item_name', 'disco_add_button_to_cart_item_name', 10, 1 );
192 // To apply this filter, uncomment the above line.
193 }
194