| 1 |
<?php // phpcs:disable |
| 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 |
if ( ! function_exists( 'disco_run_after_cart' ) ) { |
| 56 |
|
| 57 |
/** |
| 58 |
* Display Cart Item Sale Price & Regular Price. |
| 59 |
* |
| 60 |
* @param string $price Cart Item Price. |
| 61 |
* @param array $cart_item Cart Item. |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
function disco_display_regular_and_sale_price_in_cart_item( $price, $cart_item ) { |
| 65 |
$product_id = $cart_item['product_id']; |
| 66 |
$product = wc_get_product( $product_id ); |
| 67 |
$regular_price = $product->get_price(); |
| 68 |
$sale_price = $cart_item['data']->get_price(); |
| 69 |
|
| 70 |
if ( isset( $cart_item['free'] ) && 'yes' === $cart_item['free'] ) { |
| 71 |
$cart_item['data']->set_price( 0 ); |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
$offers = $cart_item['data']->get_meta( '_item_offers' ); |
| 77 |
|
| 78 |
if ( $sale_price && $sale_price < $regular_price ) { |
| 79 |
$price = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>'; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! empty( $offers ) && isset( $offers['key'] ) ) { |
| 83 |
$key = $offers['key']; |
| 84 |
$bogo_qty = $offers['bogo_qty'][ $key ]; |
| 85 |
|
| 86 |
if ( ! empty( $bogo_qty ) ) { |
| 87 |
$price = $bogo_qty; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $price; |
| 92 |
} |
| 93 |
|
| 94 |
// add_filter( 'woocommerce_cart_item_price', 'disco_display_regular_and_sale_price_in_cart_item', 10, 2 ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! function_exists( 'disco_display_subtotal_regular_and_sale_price_in_cart_item' ) ) { |
| 98 |
|
| 99 |
/** |
| 100 |
* Display Cart Item Subtotal Sale Price & Regular Price. |
| 101 |
* |
| 102 |
* @param string $subtotal Cart Item Subtotal. |
| 103 |
* @param array $cart_item Cart Item. |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
function disco_display_subtotal_regular_and_sale_price_in_cart_item( $subtotal, $cart_item ) { |
| 107 |
$product_id = $cart_item['product_id']; |
| 108 |
$quantity = $cart_item['quantity']; |
| 109 |
$product = wc_get_product( $product_id ); |
| 110 |
$regular_price = $product->get_price() * $quantity; |
| 111 |
$sale_price = $cart_item['data']->get_price() * $quantity; |
| 112 |
|
| 113 |
if ( $sale_price && $sale_price < $regular_price ) { |
| 114 |
$subtotal = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>'; |
| 115 |
} |
| 116 |
|
| 117 |
return $subtotal; |
| 118 |
} |
| 119 |
|
| 120 |
// add_filter( 'woocommerce_cart_item_subtotal', 'disco_display_subtotal_regular_and_sale_price_in_cart_item', 10, 2 ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! function_exists( 'disco_cart_item_quantity_notice' ) ) { |
| 124 |
|
| 125 |
/** |
| 126 |
* Display Cart Item Quantity Badge. |
| 127 |
* |
| 128 |
* @param string $quantity_html Cart Item Quantity HTML. |
| 129 |
* @param array $cart_item Cart Item. |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
function disco_cart_item_quantity_notice( $quantity_html, $cart_item ) {//phpcs:ignore |
| 133 |
// Wrap the quantity in a badge-images element |
| 134 |
$item = WC()->cart->get_cart()[ $cart_item ]; |
| 135 |
$offers = $item['data']->get_meta( '_item_offers' ); |
| 136 |
|
| 137 |
if ( ! empty( $offers['price'] ) ) { |
| 138 |
$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 ); |
| 140 |
} |
| 141 |
|
| 142 |
return $quantity_html; |
| 143 |
} |
| 144 |
|
| 145 |
// add_filter( 'woocommerce_cart_item_quantity', 'disco_cart_item_quantity_notice', 10, 2 ); |
| 146 |
// To apply this action, uncomment the above line. |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! function_exists( 'disco_add_button_to_cart_item_name' ) ) { |
| 150 |
|
| 151 |
/** |
| 152 |
* Add a button to the cart item name. |
| 153 |
* |
| 154 |
* @param string $product_name Product Name. |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
function disco_add_button_to_cart_item_name( $product_name ) { |
| 158 |
$button = ( new Disco )->get_offers(); // Replace '// ' with your desired link. |
| 159 |
|
| 160 |
return $product_name . '<br/>' . $button; // This will add the button after the product name. |
| 161 |
} |
| 162 |
|
| 163 |
// add_filter( 'woocommerce_cart_item_name', 'disco_add_button_to_cart_item_name', 10, 1 ); |
| 164 |
// To apply this filter, uncomment the above line. |
| 165 |
} |
| 166 |
|