| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Disco |
| 4 |
* @subpackage \App\Intents |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Disco\App\Intents; |
| 8 |
|
| 9 |
use Disco\App\Utility\Conditions; |
| 10 |
use Disco\App\Utility\Helper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Cart |
| 14 |
* |
| 15 |
* @package Disco |
| 16 |
* @subpackage \App\Intents |
| 17 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 18 |
* @link https://webappick.com |
| 19 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 20 |
* @category Intention |
| 21 |
*/ |
| 22 |
class ProductIntent extends Intent { |
| 23 |
|
| 24 |
/** |
| 25 |
* Intent constructor. |
| 26 |
* |
| 27 |
* @param \Disco\App\Utility\Config $campaign Campaign Config. |
| 28 |
* @param \Disco\App\Utility\Settings|array $settings Global Settings. |
| 29 |
*/ |
| 30 |
public function __construct( $campaign, $settings ) { |
| 31 |
parent::__construct( $campaign, $settings ); |
| 32 |
|
| 33 |
$this->campaign = $campaign; |
| 34 |
$this->settings = $settings; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Apply Product intention. |
| 39 |
* Product Discount Configuration. |
| 40 |
* |
| 41 |
* @param array|string $items Product Price. |
| 42 |
* @param \WC_Product $product Product info. |
| 43 |
* @return float|bool $discounted_price Discounted Price. |
| 44 |
*/ |
| 45 |
public function get_discounts( $items, $product ) {//phpcs:ignore |
| 46 |
$price = (float) $items; |
| 47 |
$discounted_amount = 0; |
| 48 |
|
| 49 |
// Filter the product. |
| 50 |
$info = array( 'product' => $product ); |
| 51 |
|
| 52 |
if ( ! ( new Conditions( $this->campaign, $info ) )->is_passed() ) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! Helper::is_in_valid_date( $this->campaign ) ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $this->campaign->product_is_applicable( $product->get_id() ) ) { |
| 61 |
$rules = $this->campaign->get_discount_rules(); |
| 62 |
|
| 63 |
if ( ! empty( $rules ) && isset( $rules[0] ) ) { |
| 64 |
$rule = $rules[0]; |
| 65 |
$discounted_amount = $this->calculate_discount( $price, $rule->discount_type, ( $rule->discount_value ? abs( $rule->discount_value ) : 0 ) ); |
| 66 |
// $discounted_price = $this->discounted_price( $price, $discounted_amount ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $discounted_amount; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get Product offers. |
| 75 |
* |
| 76 |
* @param array $items Cart Items. |
| 77 |
* @param \WC_Cart $cart Cart Object. |
| 78 |
* @return array $cart Cart Object. |
| 79 |
*/ |
| 80 |
public function get_offers( $items, $cart ) {//phpcs:ignore |
| 81 |
return $cart->get_cart_contents(); |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|