diff --git a/class-wc-facebookcommerce.php b/class-wc-facebookcommerce.php index 119b63133..4362137d2 100644 --- a/class-wc-facebookcommerce.php +++ b/class-wc-facebookcommerce.php @@ -63,6 +63,9 @@ class WC_Facebookcommerce extends WooCommerce\Facebook\Framework\Plugin { /** @var WooCommerce\Facebook\AJAX Ajax handler instance */ private $ajax; + /** @var WooCommerce\Facebook\Checkout */ + private $checkout; + /** @var WooCommerce\Facebook\Products\Feed product feed handler */ private $product_feed; @@ -184,6 +187,7 @@ public function init() { $this->heartbeat = new Heartbeat( WC()->queue() ); $this->heartbeat->init(); + $this->checkout = new WooCommerce\Facebook\Checkout(); $this->product_feed = new WooCommerce\Facebook\Products\Feed(); $this->products_stock_handler = new WooCommerce\Facebook\Products\Stock(); $this->products_sync_handler = new WooCommerce\Facebook\Products\Sync(); diff --git a/includes/Checkout.php b/includes/Checkout.php new file mode 100644 index 000000000..717213906 --- /dev/null +++ b/includes/Checkout.php @@ -0,0 +1,145 @@ +add_hooks(); + } + + /** + * Adds the necessary action and filter hooks. + * + * @since 3.3.0 + */ + public function add_hooks() { + // add the rewrite rule for the checkout permalink + add_action( 'init', array( $this, 'add_checkout_permalink_rewrite_rule' ) ); + + // add the query var for the checkout permalink + add_filter( 'query_vars', array( $this, 'add_checkout_permalink_query_var' ) ); + + // load the checkout permalink template + add_filter( 'template_include', array( $this, 'load_checkout_permalink_template' ) ); + + // flush rewrite rules when plugin is activated + register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules_on_activation' ) ); + + // flush rewrite rules when plugin is deactivated + register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules_on_deactivation' ) ); + } + + /** + * Adds a rewrite rule for the checkout permalink. + * + * @since 3.3.0 + */ + public function add_checkout_permalink_rewrite_rule() { + add_rewrite_rule( '^fb-checkout/?$', 'index.php?fb_checkout=1', 'top' ); + } + + /** + * Adds query vars for the checkout permalink. + * + * @since 3.3.0 + * + * @param array $vars + * @return array + */ + public function add_checkout_permalink_query_var( $vars ) { + // Add 'fb_checkout' as a query var + $vars[] = 'fb_checkout'; + + // Add 'products' as a query var + $vars[] = 'products'; + + // Add 'coupon' as a query var + $vars[] = 'coupon'; + + return $vars; + } + + /** + * Loads the checkout permalink template. + * + * @since 3.3.0 + */ + public function load_checkout_permalink_template() { + if ( get_query_var( 'fb_checkout' ) ) { + // Clear the WooCommerce cart + WC()->cart->empty_cart(); + + // Get the 'products' query parameter + $products_param = get_query_var( 'products' ); + + if ( $products_param ) { + // Split multiple products by comma + $products = explode( ',', $products_param ); + + foreach ( $products as $product ) { + // Parse each product ID and quantity + list($product_id, $quantity) = explode( ':', $product ); + + // Validate and add the product to the cart + if ( is_numeric( $product_id ) && is_numeric( $quantity ) && $quantity > 0 ) { + WC()->cart->add_to_cart( $product_id, $quantity ); + } + } + } + + // Get the 'coupon' query parameter + $coupon_code = get_query_var( 'coupon' ); + + if ( $coupon_code ) { + // Apply the coupon to the cart + WC()->cart->apply_coupon( sanitize_text_field( $coupon_code ) ); + } + + // Use a custom template file + include plugin_dir_path( __FILE__ ) . 'Templates/CheckoutTemplate.php'; + + exit; + } + } + + /** + * Flushes rewrite rules when the plugin is activated. + * + * @since 3.3.0 + */ + public function flush_rewrite_rules_on_activation() { + $this->add_checkout_permalink_rewrite_rule(); + flush_rewrite_rules(); + } + + /** + * Flushes rewrite rules when the plugin is deactivated. + * + * @since 3.3.0 + */ + public function flush_rewrite_rules_on_deactivation() { + flush_rewrite_rules(); + } +} diff --git a/includes/Templates/CheckoutTemplate.php b/includes/Templates/CheckoutTemplate.php new file mode 100644 index 000000000..e2713eb00 --- /dev/null +++ b/includes/Templates/CheckoutTemplate.php @@ -0,0 +1,28 @@ + + +