-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
class-url-endpoint.php
86 lines (73 loc) · 2.3 KB
/
class-url-endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace WC_Quick_Buy;
defined( 'ABSPATH' ) || exit;
use VSP\Base;
/**
* Class URL_Endpoint
*
* @package WC_Quick_Buy
* @author Varun Sridharan <varunsridharan23@gmail.com>
*/
class URL_Endpoint extends Base {
/**
* URL_Endpoint constructor.
*/
public function __construct() {
$this->register_endpoint();
/* @uses addtocart_if_quickbuy */
add_action( 'parse_request', array( $this, 'addtocart_if_quickbuy' ) );
}
/**
* Registers Quick Buy Endpoint
*/
private function register_endpoint() {
$value_types = array(
'id' => '([\d]+)',
'qty' => '([\d]+)',
);
$endpoint = Helper::option( 'url_endpoint' );
$link = ltrim( ltrim( untrailingslashit( $endpoint ), '/' ), '^' );
$no_qty = str_replace( '{qty}', '', $link );
$instance = wponion_endpoint( 'quickbuy' );
// Quick Buy URL With Qty
$instance->add_rewrite_rule( $link, $value_types );
// Quick Buy Without Qty
$instance->add_rewrite_rule( untrailingslashit( $no_qty ), $value_types );
}
/**
* Add's To Cart if Quick Buy Sent Request.
*
* @param $query
*
* @return mixed
* @throws \Exception
*/
public function addtocart_if_quickbuy( $query ) {
if ( ! isset( $query->query_vars['quickbuy_id'] ) && ! isset( $query->query_vars['quickbuy_sku'] ) && ! isset( $query->query_vars['quickbuy_slug'] ) ) {
return $query;
}
$qty = ( isset( $query->query_vars['quickbuy_qty'] ) ) ? $query->query_vars['quickbuy_qty'] : Helper::option( 'quantity' );
$id = ( isset( $query->query_vars['quickbuy_id'] ) ) ? $query->query_vars['quickbuy_id'] : false;
if ( isset( $query->query_vars['quickbuy_sku'] ) ) {
$id = wc_get_product_id_by_sku( $query->query_vars['quickbuy_sku'] );
}
if ( isset( $query->query_vars['quickbuy_slug'] ) ) {
$product = get_page_by_path( $query->query_vars['quickbuy_slug'], OBJECT, 'product' );
$id = ( isset( $product->ID ) ) ? $product->ID : false;
}
if ( false !== $id ) {
$_REQUEST['quick_buy'] = true;
do_action( 'wc_quick_buy_endpoint_add_to_cart_before' );
wc()->cart->add_to_cart( $id, $qty );
do_action( 'wc_quick_buy_endpoint_add_to_cart_after' );
wp_safe_redirect( Helper::redirect_url() );
exit;
} else {
/*global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();*/
}
return $query;
}
}