Skip to content

Commit ead5f0e

Browse files
committed
chore(release): deploy v0.5.0
0 parents  commit ead5f0e

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.log
2+
node_modules
3+
.DS_Store

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Introduction
2+
This is a plugin which extends the Crocoblock Jet Engine plugin for Elementor.
3+
4+
It adds support for the Dynamic Visibility functionality to check if the user
5+
has purchased a WooCommerce product by entering the product id.
6+
7+
# Usage
8+
Install the plugin and activate it.
9+
10+
You need JetEngine and WooCommerce installed as well.
11+
12+
Don't forget that Dynamic Visibility needs to be enabled in JetEngine:
13+
14+
1. In your admin panel, go to JetEngine > JetEngine to see the JetEngine dashboard
15+
2. Toggle the "Dynamic Visibility for Widgets and Section" slider.
16+
3. Hit the “Save” button.
17+
18+
Then you can find it in Advanced > Dynamic Visibility.
19+
20+
Just look under User > User has purchased product.
21+
22+
Enter the product id to check for.
23+
24+
(Check the known issues at the end of this readme before using this plugin)
25+
26+
# Changelog
27+
0.5.0 - 21 February 2022
28+
- Initial release
29+
- Supports built in wc product purchased check with single product id
30+
31+
# Inspiration
32+
This was based on a request in this support ticket:
33+
34+
- https://github.com/Crocoblock/suggestions/issues/4902
35+
36+
I used the snippet from here to do the heavy lifting:
37+
38+
- https://www.businessbloomer.com/woocommerce-check-current-user-already-purchased-product/
39+
40+
And based the plugin structure on these two great starting points:
41+
42+
- https://github.com/UraraReika/jet-engine-single-products-widget-custom-visibility-conditions/
43+
- https://github.com/MjHead/jet-engine-custom-visibility-conditions/
44+
45+
# Known Issues
46+
There are potential issues with this that I haven't investigated fully but did
47+
read about during development.
48+
49+
It may be that refunded products are not filtered out with this function:
50+
51+
- https://github.com/woocommerce/woocommerce/issues/17959#issuecomment-831418890
52+
53+
It may be that this method may seriously slow down your website:
54+
55+
- https://github.com/woocommerce/woocommerce/issues/16604
56+
57+
I followed the discussion of this and related support tickets, and it was
58+
"solved", but then a bug was found, it was rolled back, and they gave up,
59+
so I'm not sure what the current status of this is.
60+
61+
# Licence
62+
This plugin is licenced under GPL 3, and is free to use on personal and
63+
commercial projects.
64+
65+
# Author
66+
Built by Matthew Harris of runthings.dev, copyright 2022.
67+
68+
https://runthings.dev/

conditions/user-purchased.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Runthings_Woo_User_Purchased;
4+
5+
use Elementor\Controls_Manager;
6+
7+
class WooUserPurchased extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base
8+
{
9+
10+
/**
11+
* Returns condition ID
12+
*
13+
* @return string
14+
*/
15+
public function get_id()
16+
{
17+
return 'runthings-woo-user-purchased';
18+
}
19+
20+
/**
21+
* Returns condition name
22+
*
23+
* @return string
24+
*/
25+
public function get_name()
26+
{
27+
return __('User has purchased product', 'rtp-woo-user-purchased');
28+
}
29+
30+
/**
31+
* Returns group for current operator
32+
*
33+
* @return string
34+
*/
35+
public function get_group()
36+
{
37+
return 'user';
38+
}
39+
40+
/**
41+
* Check condition by passed arguments
42+
*
43+
* @param array $args
44+
*
45+
* @return bool
46+
*/
47+
public function check($args = array())
48+
{
49+
$type = !empty($args['type']) ? $args['type'] : 'show';
50+
51+
$settings = $args['condition_settings'];
52+
53+
$product_id = !empty( $settings['product_id'] ) ? $settings['product_id'] : null;
54+
55+
$is_purchased = false;
56+
if (function_exists('wc_customer_bought_product')) {
57+
$is_purchased = wc_customer_bought_product('', get_current_user_id(), $product_id);
58+
}
59+
60+
return $this->get_result($type, $is_purchased);
61+
}
62+
63+
/**
64+
* Return condition check status depends on condition type
65+
*
66+
* @param [type] $type [description]
67+
* @param [type] $result [description]
68+
* @return [type] [description]
69+
*/
70+
public function get_result($type, $result)
71+
{
72+
if ('hide' === $type) {
73+
return !$result;
74+
} else {
75+
return $result;
76+
}
77+
}
78+
79+
/**
80+
* Check if is condition available for meta fields control
81+
*
82+
* @return boolean
83+
*/
84+
public function is_for_fields()
85+
{
86+
return false;
87+
}
88+
89+
/**
90+
* Check if is condition available for meta value control
91+
*
92+
* @return boolean
93+
*/
94+
public function need_value_detect()
95+
{
96+
return false;
97+
}
98+
99+
/**
100+
* Returns custom controls for this visibity
101+
*/
102+
public function get_custom_controls()
103+
{
104+
return array(
105+
'product_id' => array(
106+
'label' => esc_html__( 'Product Id', 'rtp-woo-user-purchased' ),
107+
'type' => \Elementor\Controls_Manager::TEXT,
108+
'default' => '',
109+
'description' => __('WooCommerce product id', 'rtp-woo-user-purchased'),
110+
),
111+
);
112+
}
113+
}

index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// Silence in golden.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Plugin Name: JetEngine - Dynamic Visibility - Woocommerce - Has User Purchased Product
4+
* Plugin URI: https://github.com/rtpHarry/jet-engine-dynamic-visibility-woo-user-purchased
5+
* Description: Add custom conditions for displaying elements if the user has purchased a product
6+
* Version: 0.5.0
7+
* Author: Matthew Harris, runthings.dev
8+
* Author URI: https://runthings.dev/
9+
* Text Domain: rtp-woo-user-purchased
10+
* License: GPL-3.0+
11+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
12+
*/
13+
14+
// If this file is called directly, abort.
15+
if ( ! defined( 'WPINC' ) ) {
16+
die();
17+
}
18+
19+
// Init plugin after loading
20+
add_action( 'plugins_loaded', 'runthings_setup_woo_user_purchased' );
21+
22+
/**
23+
* Initialize plugin
24+
*/
25+
function runthings_setup_woo_user_purchased() {
26+
27+
define( 'RTP_USER_PURCHASED__FILE__', __FILE__ );
28+
define( 'RTP_USER_PURCHASED_PATH', plugin_dir_path( RTP_USER_PURCHASED__FILE__ ) );
29+
30+
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
31+
32+
require RTP_USER_PURCHASED_PATH . 'conditions/user-purchased.php';
33+
34+
$conditions_manager->register_condition( new Runthings_Woo_User_Purchased\WooUserPurchased() );
35+
36+
} );
37+
38+
}

0 commit comments

Comments
 (0)