From c918cbefffe04364181ba4bf531b3215f1b88efd Mon Sep 17 00:00:00 2001
From: Woo The Gravity Forms Product Addon Extension can not function properly if this additional plugin is active. Please disable it for proper functionality of the extension. The Gravity Forms Product Addon Extension can not function properly if this additional plugin is active. Please disable it for proper functionality of the extension.
+
- Gravity Forms Duplicate Prevention Active
Gravity Forms Duplicate Prevention Active
The " "Gravity Forms Product Addon Extension can not function " @@ -52,32 +91,32 @@ msgid "" "href=\"%s\">disable it for proper functionality of the extension.
" msgstr "" -#: admin/gravityforms-product-addons-admin.php:107 -#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:107 +#: admin/gravityforms-product-addons-admin.php:128 +#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:128 msgid "Gravity Forms" msgstr "" -#: admin/gravityforms-product-addons-admin.php:180 -#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:180 +#: admin/gravityforms-product-addons-admin.php:201 +#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:201 msgid "No Form ID" msgstr "" -#: admin/gravityforms-product-addons-admin.php:213 +#: admin/gravityforms-product-addons-admin.php:234 #: admin/views/html-gravityforms-addons-wc-metabox.php:410 -#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:213 +#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:234 #: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:410 msgid "Quantity Field" msgstr "" -#: admin/gravityforms-product-addons-admin.php:216 +#: admin/gravityforms-product-addons-admin.php:237 #: admin/views/html-gravityforms-addons-wc-metabox.php:413 -#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:216 +#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:237 #: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:413 msgid "A field to use to control cart item quantity." msgstr "" -#: admin/gravityforms-product-addons-admin.php:222 -#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:222 +#: admin/gravityforms-product-addons-admin.php:243 +#: dist/woocommerce-gravityforms-product-addons/admin/gravityforms-product-addons-admin.php:243 msgid "No suitable quantity fields found." msgstr "" @@ -241,17 +280,6 @@ msgstr "" msgid "Subtotal Label" msgstr "" -#: admin/views/html-gravityforms-addons-panel.php:139 -#: admin/views/html-gravityforms-addons-wc-metabox.php:220 -#: admin/views/html-gravityforms-addons-wc-metabox.php:221 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:139 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:220 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:221 -#: dist/woocommerce-gravityforms-product-addons/gravityforms-product-addons-form.php:46 -#: gravityforms-product-addons-form.php:46 -msgid "Subtotal" -msgstr "" - #: admin/views/html-gravityforms-addons-panel.php:140 #: admin/views/html-gravityforms-addons-wc-metabox.php:222 #: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:140 @@ -273,17 +301,6 @@ msgstr "" msgid "Options Label" msgstr "" -#: admin/views/html-gravityforms-addons-panel.php:153 -#: admin/views/html-gravityforms-addons-wc-metabox.php:234 -#: admin/views/html-gravityforms-addons-wc-metabox.php:235 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:153 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:234 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:235 -#: dist/woocommerce-gravityforms-product-addons/gravityforms-product-addons-form.php:47 -#: gravityforms-product-addons-form.php:47 -msgid "Options" -msgstr "" - #: admin/views/html-gravityforms-addons-panel.php:154 #: admin/views/html-gravityforms-addons-wc-metabox.php:236 #: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:154 @@ -305,17 +322,6 @@ msgstr "" msgid "Total Label" msgstr "" -#: admin/views/html-gravityforms-addons-panel.php:167 -#: admin/views/html-gravityforms-addons-wc-metabox.php:248 -#: admin/views/html-gravityforms-addons-wc-metabox.php:249 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:167 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:248 -#: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-wc-metabox.php:249 -#: dist/woocommerce-gravityforms-product-addons/gravityforms-product-addons-form.php:48 -#: gravityforms-product-addons-form.php:48 -msgid "Total" -msgstr "" - #: admin/views/html-gravityforms-addons-panel.php:168 #: admin/views/html-gravityforms-addons-wc-metabox.php:250 #: dist/woocommerce-gravityforms-product-addons/admin/views/html-gravityforms-addons-panel.php:168 @@ -550,8 +556,8 @@ msgstr "" msgid "Overwrite the price(s) completely" msgstr "" -#: dist/woocommerce-gravityforms-product-addons/gravityforms-product-addons-main.php:559 -#: gravityforms-product-addons-main.php:559 +#: dist/woocommerce-gravityforms-product-addons/gravityforms-product-addons-main.php:590 +#: gravityforms-product-addons-main.php:590 msgid "There was a problem with your submission. Please review the fields below." msgstr "" @@ -649,6 +655,214 @@ msgstr "" msgid "Failed to delete entry." msgstr "" +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:48 +#: inc/gravityforms-product-addons-merge-tags.php:48 +msgid "WooCommerce Product" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:49 +#: inc/gravityforms-product-addons-merge-tags.php:49 +msgid "" +"These merge tags are available when the form is associated with a " +"WooCommerce product. They provide information about the WooCommerce " +"product that is associated with the entry." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:53 +#: inc/gravityforms-product-addons-merge-tags.php:53 +msgid "Name" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:54 +#: inc/gravityforms-product-addons-merge-tags.php:54 +msgid "The name of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:59 +#: inc/gravityforms-product-addons-merge-tags.php:59 +msgid "SKU" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:60 +#: inc/gravityforms-product-addons-merge-tags.php:60 +msgid "The SKU of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:65 +#: inc/gravityforms-product-addons-merge-tags.php:65 +msgid "Price" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:66 +#: inc/gravityforms-product-addons-merge-tags.php:66 +msgid "The price of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:71 +#: inc/gravityforms-product-addons-merge-tags.php:71 +msgid "Regular Price" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:72 +#: inc/gravityforms-product-addons-merge-tags.php:72 +msgid "The regular price of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:77 +#: inc/gravityforms-product-addons-merge-tags.php:77 +msgid "Sale Price" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:78 +#: inc/gravityforms-product-addons-merge-tags.php:78 +msgid "The sale price of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:83 +#: inc/gravityforms-product-addons-merge-tags.php:83 +msgid "Stock Quantity" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:84 +#: inc/gravityforms-product-addons-merge-tags.php:84 +msgid "The stock quantity of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:89 +#: inc/gravityforms-product-addons-merge-tags.php:89 +msgid "Stock Status" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:90 +#: inc/gravityforms-product-addons-merge-tags.php:90 +msgid "The stock status of the product." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:95 +#: inc/gravityforms-product-addons-merge-tags.php:95 +msgid "Product URL" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:96 +#: inc/gravityforms-product-addons-merge-tags.php:96 +msgid "The URL to view the product on the website." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:103 +#: inc/gravityforms-product-addons-merge-tags.php:103 +msgid "WooCommerce Order" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:104 +#: inc/gravityforms-product-addons-merge-tags.php:104 +msgid "" +"These merge tags are available when the form is associated with a " +"WooCommerce product. They provide information about the WooCommerce order " +"that is associated with the entry." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:109 +#: inc/gravityforms-product-addons-merge-tags.php:109 +msgid "Billing Address - Formatted" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:110 +#: inc/gravityforms-product-addons-merge-tags.php:110 +msgid "" +"The billing address of the order. If the order has a user, the shipping " +"address will be used. Otherwise, the billing address will be used." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:115 +#: inc/gravityforms-product-addons-merge-tags.php:115 +msgid "Billing Email" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:116 +#: inc/gravityforms-product-addons-merge-tags.php:116 +msgid "The billing email of the order." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:121 +#: inc/gravityforms-product-addons-merge-tags.php:121 +msgid "Customer ID" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:122 +#: inc/gravityforms-product-addons-merge-tags.php:122 +msgid "" +"The ID of the customer who placed the order. If the order has a user, the " +"user ID will be used. Otherwise, the billing email will be used." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:127 +#: inc/gravityforms-product-addons-merge-tags.php:127 +msgid "Customer Email" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:128 +#: inc/gravityforms-product-addons-merge-tags.php:128 +msgid "" +"The email address of the customer who placed the order. If the order has a " +"user, the user email will be used. Otherwise, the billing email will be " +"used." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:133 +#: inc/gravityforms-product-addons-merge-tags.php:133 +msgid "Customer Display Name" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:134 +#: inc/gravityforms-product-addons-merge-tags.php:134 +msgid "" +"The display name of the customer who placed the order. If the order has a " +"user, the user display name will be used. Otherwise, the billing email will " +"be used." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:139 +#: inc/gravityforms-product-addons-merge-tags.php:139 +msgid "Order URL" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:140 +#: inc/gravityforms-product-addons-merge-tags.php:140 +msgid "The URL to view the order on the website." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:145 +#: inc/gravityforms-product-addons-merge-tags.php:145 +msgid "Order ID" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:146 +#: inc/gravityforms-product-addons-merge-tags.php:146 +msgid "The ID of the order." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:151 +#: inc/gravityforms-product-addons-merge-tags.php:151 +msgid "Order Number" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:152 +#: inc/gravityforms-product-addons-merge-tags.php:152 +msgid "The order number of the order." +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:157 +#: inc/gravityforms-product-addons-merge-tags.php:157 +msgid "Shipping Address - Formatted" +msgstr "" + +#: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-merge-tags.php:158 +#: inc/gravityforms-product-addons-merge-tags.php:158 +msgid "" +"The shipping address of the order. If the order has a user, the shipping " +"address will be used. Otherwise, the billing address will be used." +msgstr "" + #: dist/woocommerce-gravityforms-product-addons/inc/gravityforms-product-addons-order.php:63 #: inc/gravityforms-product-addons-order.php:63 msgid "View Gravity Form Entry" diff --git a/inc/gravityforms-product-addons-cart-item.php b/inc/gravityforms-product-addons-cart-item.php new file mode 100644 index 0000000..3c773e6 --- /dev/null +++ b/inc/gravityforms-product-addons-cart-item.php @@ -0,0 +1,20 @@ +cart_item = $cart_item; + $this->other_data = $other_data; + } + +} diff --git a/inc/gravityforms-product-addons-cart.php b/inc/gravityforms-product-addons-cart.php index a582924..668faa9 100644 --- a/inc/gravityforms-product-addons-cart.php +++ b/inc/gravityforms-product-addons-cart.php @@ -6,7 +6,7 @@ class WC_GFPA_Cart { public static function register() { if ( self::$instance == null ) { - self::$instance = new WC_GFPA_Cart; + self::$instance = new WC_GFPA_Cart(); } } @@ -27,19 +27,24 @@ public static function instance() { private function __construct() { // Filters for cart actions - add_filter( 'woocommerce_add_cart_item_data', array( $this, 'add_cart_item_data' ), 10, 3 ); - add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'get_cart_item_from_session' ), 10, 2 ); - add_filter( 'woocommerce_get_item_data', array( $this, 'get_item_data' ), 10, 2 ); - add_filter( 'woocommerce_add_cart_item', array( $this, 'add_cart_item' ), 10, 1 ); + add_filter( 'woocommerce_add_cart_item_data', [ $this, 'add_cart_item_data' ], 10, 3 ); + add_filter( 'woocommerce_get_cart_item_from_session', [ $this, 'get_cart_item_from_session' ], 10, 2 ); + add_filter( 'woocommerce_get_item_data', [ $this, 'get_item_data' ], 10, 2 ); + add_filter( 'woocommerce_add_cart_item', [ $this, 'add_cart_item' ], 10, 1 ); - add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'order_item_meta' ), 10, 3 ); - add_filter( 'woocommerce_add_to_cart_validation', array( $this, 'add_to_cart_validation' ), 99, 3 ); + add_action( 'woocommerce_checkout_create_order_line_item', [ $this, 'order_item_meta' ], 10, 3 ); + add_filter( 'woocommerce_add_to_cart_validation', [ $this, 'add_to_cart_validation' ], 99, 5 ); //Order Again - add_filter( 'woocommerce_order_again_cart_item_data', array( - $this, - 'on_get_order_again_cart_item_data' - ), 10, 3 ); + add_filter( + 'woocommerce_order_again_cart_item_data', + [ + $this, + 'on_get_order_again_cart_item_data', + ], + 10, + 3 + ); } /** @@ -51,11 +56,12 @@ private function __construct() { * @param $valid * @param $product_id * @param $quantity + * @param null $variation_id + * @param null $variations * * @return mixed */ - public function add_to_cart_validation( $valid, $product_id, $quantity ) { - + public function add_to_cart_validation( $valid, $product_id, $quantity, $variation_id = null, $variations = null) { $changing_pages = false; if ( ! $valid ) { return false; @@ -69,18 +75,26 @@ public function add_to_cart_validation( $valid, $product_id, $quantity ) { return false; } + // Prevent parent variable product from being added to cart. + $product = wc_get_product( $product_id ); + if ( empty( $variation_id ) && $product && $product->is_type( 'variable' ) ) { + // No variation was selected. + $valid = false; + } + if ( $gravity_form_data && is_array( $gravity_form_data ) && isset( $gravity_form_data['id'] ) && intval( $gravity_form_data['id'] ) > 0 && isset( $_POST['gform_form_id'] ) && is_numeric( $_POST['gform_form_id'] ) ) { //Gravity forms generates errors and warnings. To prevent these from conflicting with other things, we are going to disable warnings and errors. $err_level = error_reporting(); error_reporting( 0 ); + if ( ! class_exists( 'GFFormDisplay' ) ) { - require_once( GFCommon::get_base_path() . "/form_display.php" ); + require_once GFCommon::get_base_path() . '/form_display.php'; } if ( ! class_exists( 'RGFormsModel' ) ) { - require_once( GFCommon::get_base_path() . "/forms_model.php" ); + require_once GFCommon::get_base_path() . '/forms_model.php'; } $form_id = $_POST['gform_form_id']; @@ -91,8 +105,8 @@ public function add_to_cart_validation( $valid, $product_id, $quantity ) { //Remove all post_submission hooks so data does not get sent to feeds such as Zapier $this->disable_gform_after_submission_hooks( $form_id ); - GFFormDisplay::$submission = array(); - require_once( GFCommon::get_base_path() . "/form_display.php" ); + GFFormDisplay::$submission = []; + require_once GFCommon::get_base_path() . '/form_display.php'; $_POST['gform_submit'] = $_POST['gform_old_submit']; GFCommon::log_debug( __METHOD__ . "(): [woocommerce-gravityforms-product-addons] Processing Add to Cart Validation #{$form_id}." ); @@ -107,13 +121,17 @@ public function add_to_cart_validation( $valid, $product_id, $quantity ) { $this->enable_gform_after_submission_hooks( $form_id ); } - add_filter( 'gform_pre_process_' . $form_id, array( $this, 'on_gform_pre_process' ) ); + add_filter( 'gform_pre_process_' . $form_id, [ $this, 'on_gform_pre_process' ] ); add_filter( 'gform_abort_submission_with_confirmation', '__return_false', 999, 1 ); add_filter( 'gform_entry_is_spam', '__return_false', 999, 1 ); - GFFormDisplay::process_form( $form_id ); + GFForms::maybe_process_form(); - remove_filter( 'gform_pre_process_' . $form_id, array( $this, 'on_gform_pre_process' ) ); + remove_filter( 'gform_pre_process_' . $form_id, [ $this, 'on_gform_pre_process' ] ); + remove_filter( 'gform_pre_send_email', [ $this, 'suppress_form_submission_notifications' ], 50 ); + + // Prevent GF from processing the form again. + remove_action( 'wp', [ 'GFForms', 'maybe_process_form' ], 9 ); $_POST['gform_old_submit'] = $_POST['gform_submit']; unset( $_POST['gform_submit'] ); @@ -128,7 +146,6 @@ public function add_to_cart_validation( $valid, $product_id, $quantity ) { $valid = false; } - // This changing pages logic is we can determine if we should add a wc_notice for invalid submissions. You can't add a wc_notice if the form is valid, but you are changing pages. $page_to_display = intval( $submission['page_number'] ); $source_page_number = intval( $submission['source_page_number'] ); @@ -177,7 +194,7 @@ public function add_cart_item( $cart_item, $restoring_session = false ) { } if ( ! class_exists( 'RGFormsModel' ) ) { - require_once( GFCommon::get_base_path() . "/forms_model.php" ); + require_once GFCommon::get_base_path() . '/forms_model.php'; } //Gravity forms generates errors and warnings. To prevent these from conflicting with other things, we are going to disable warnings and errors. @@ -193,29 +210,27 @@ public function add_cart_item( $cart_item, $restoring_session = false ) { $lead = $cart_item['_gravity_form_lead']; - $products = array(); $total = 0; - $lead['id'] = uniqid() . time() . rand(); $products = WC_GFPA_Field_Helpers::get_product_fields( $form_meta, $lead ); - if ( ! empty( $products["products"] ) ) { - foreach ( $products["products"] as $product ) { - $price = GFCommon::to_number( $product["price"] ); - if ( is_array( rgar( $product, "options" ) ) ) { - $count = sizeof( $product["options"] ); + if ( ! empty( $products['products'] ) ) { + foreach ( $products['products'] as $product ) { + $price = GFCommon::to_number( $product['price'] ); + if ( is_array( rgar( $product, 'options' ) ) ) { + $count = sizeof( $product['options'] ); $index = 1; - foreach ( $product["options"] as $option ) { - $price += GFCommon::to_number( $option["price"] ); - $class = $index == $count ? " class='lastitem'" : ""; - $index ++; + foreach ( $product['options'] as $option ) { + $price += GFCommon::to_number( $option['price'] ); + $class = $index == $count ? " class='lastitem'" : ''; + ++ $index; } } - $subtotal = floatval( $product["quantity"] ) * $price; + $subtotal = floatval( $product['quantity'] ) * $price; $total += $subtotal; } - $total += floatval( $products["shipping"]["price"] ); + $total += floatval( $products['shipping']['price'] ); } $total = apply_filters( 'woocommerce_gforms_get_cart_item_total', $total, $cart_item ); @@ -246,10 +261,8 @@ public function add_cart_item( $cart_item, $restoring_session = false ) { $cart_item['quantity'] = $quantity; } - } } - } return $cart_item; @@ -276,11 +289,11 @@ public function add_cart_item_data( $cart_item_meta, $product_id, $variation_id error_reporting( 0 ); if ( ! class_exists( 'GFFormDisplay' ) ) { - require_once( GFCommon::get_base_path() . "/form_display.php" ); + require_once GFCommon::get_base_path() . '/form_display.php'; } if ( ! class_exists( 'RGFormsModel' ) ) { - require_once( GFCommon::get_base_path() . "/forms_model.php" ); + require_once GFCommon::get_base_path() . '/forms_model.php'; } $form_id = $gravity_form_data['id']; @@ -289,7 +302,7 @@ public function add_cart_item_data( $cart_item_meta, $product_id, $variation_id $cart_item_meta['_gravity_form_hash'] = wc_gfpa()->get_form_field_hash( $form_id ); $form_meta = RGFormsModel::get_form_meta( $form_id ); - $form_meta = gf_apply_filters( array( 'gform_pre_render', $form_id ), $form_meta ); + $form_meta = gf_apply_filters( [ 'gform_pre_render', $form_id ], $form_meta ); GFCommon::log_debug( __METHOD__ . "(): [woocommerce-gravityforms-product-addons] Processing Add to Cart #{$form_id}." ); @@ -297,12 +310,12 @@ public function add_cart_item_data( $cart_item_meta, $product_id, $variation_id GFCommon::log_debug( __METHOD__ . "(): [woocommerce-gravityforms-product-addons] Using Lead from Validation #{$form_id}." ); unset( GFFormDisplay::$submission[ $form_id ]['confirmation_message'] ); $lead = $this->lead_from_validation; - $cart_item_meta['_gravity_form_lead'] = array( - 'form_id' => $form_id, - 'source_url' => $lead['source_url'], - 'ip' => $lead['ip'], + $cart_item_meta['_gravity_form_lead'] = [ + 'form_id' => $form_id, + 'source_url' => $lead['source_url'], + 'ip' => $lead['ip'], 'original_lead_id' => $lead['id'], - ); + ]; GFCommon::log_debug( __METHOD__ . "(): [] Lead From Validation #{$form_id}." ); GFCommon::log_debug( print_r( $this->lead_from_validation, true ) ); @@ -335,7 +348,6 @@ public function add_cart_item_data( $cart_item_meta, $product_id, $variation_id $cart_item_meta['_gravity_form_lead'][ strval( $input['id'] ) ] = apply_filters( 'wcgf_gform_input_value', $cart_item_meta['_gravity_form_lead'][ strval( $input_id ) ], $product_id, $variation_id, $field, $input ); } } - } else { $cart_item_meta['_gravity_form_lead'][ strval( $field['id'] ) ] = apply_filters( 'wcgf_gform_field_value', $value, $product_id, $variation_id, $field ); } @@ -387,6 +399,8 @@ public function get_cart_item_from_session( $cart_item, $values ) { public function get_item_data( $other_data, $cart_item ) { + $cart_item_meta = new WC_GFPA_Cart_Item_Meta( $cart_item, $other_data ); + //Short circuit because subscriptions stores the metadata and automatically adds it back in. If we allow this to run, we end up with duplicate cart item metadata. if ( isset( $cart_item['subscription_initial_payment'] ) ) { return $other_data; @@ -402,20 +416,20 @@ public function get_item_data( $other_data, $cart_item ) { //Ensure GFFormDisplay exists in case developers use hooks that expect it to. if ( ! class_exists( 'GFFormDisplay' ) ) { - require_once( GFCommon::get_base_path() . "/form_display.php" ); + require_once GFCommon::get_base_path() . '/form_display.php'; } if ( ! class_exists( 'RGFormsModel' ) ) { - require_once( GFCommon::get_base_path() . "/forms_model.php" ); + require_once GFCommon::get_base_path() . '/forms_model.php'; } $form_meta = RGFormsModel::get_form_meta( $gravity_form_data['id'] ); - $form_meta = gf_apply_filters( array( 'gform_pre_render', $gravity_form_data['id'] ), $form_meta ); + $form_meta = gf_apply_filters( [ 'gform_pre_render', $gravity_form_data['id'] ], $form_meta ); if ( ! empty( $form_meta ) ) { $lead = $cart_item['_gravity_form_lead']; $lead['id'] = 0; //Set the lead ID to 0 so that the entry is not updated, and gravity forms plugins such as populate anything don't cause errors. $products = $this->get_product_fields( $form_meta, $lead ); - $valid_products = array(); + $valid_products = []; foreach ( $products['products'] as $id => $product ) { if ( $product['quantity'] ) { $valid_products[] = $id; @@ -438,12 +452,12 @@ public function get_item_data( $other_data, $cart_item ) { $arr_var = ( is_array( $value ) ) ? implode( '', $value ) : '-'; if ( $value === '0' || ( ! empty( $value ) && ! empty( $arr_var ) && $value != '[]' ) ) { - $display_value = GFCommon::get_lead_field_display( $field, $value, isset( $lead["currency"] ) ? $lead["currency"] : false, false ); + $display_value = GFCommon::get_lead_field_display( $field, $value, isset( $lead['currency'] ) ? $lead['currency'] : false, false ); $price_adjustement = false; - $display_value = apply_filters( "gform_entry_field_value", $display_value, $field, $lead, $form_meta ); + $display_value = apply_filters( 'gform_entry_field_value', $display_value, $field, $lead, $form_meta ); - $display_text = GFCommon::get_lead_field_display( $field, $value, isset( $lead["currency"] ) ? $lead["currency"] : false, apply_filters( 'woocommerce_gforms_use_label_as_value', true, $value, $field, $lead, $form_meta ) ); - $display_text = apply_filters( "woocommerce_gforms_field_display_text", $display_text, $display_value, $field, $lead, $form_meta ); + $display_text = GFCommon::get_lead_field_display( $field, $value, isset( $lead['currency'] ) ? $lead['currency'] : false, apply_filters( 'woocommerce_gforms_use_label_as_value', true, $value, $field, $lead, $form_meta ) ); + $display_text = apply_filters( 'woocommerce_gforms_field_display_text', $display_text, $display_value, $field, $lead, $form_meta ); if ( $field['type'] == 'product' ) { $prefix = ''; @@ -454,33 +468,103 @@ public function get_item_data( $other_data, $cart_item ) { } else { $hidden = false; } + + $cart_item_data = apply_filters( + 'woocommerce_gforms_get_item_data', + [ + 'name' => $prefix . $display_title, + 'display' => $display_text, + 'value' => $display_value, + 'hidden' => $hidden, + ], + $field, + $lead, + $form_meta + ); + + $other_data[] = $cart_item_data; + } else { - $display_title = GFCommon::get_label( $field ); + $inputs = $field instanceof GF_Field ? $field->get_entry_inputs() : rgar( $field, 'inputs' ); - $prefix = ''; - $hidden = $field['type'] == 'hidden' || ( isset( $field['visibility'] ) && $field['visibility'] == 'hidden' ); - $display_hidden = apply_filters( "woocommerce_gforms_field_is_hidden", $hidden, $display_value, $display_title, $field, $lead, $form_meta ); - if ( $display_hidden ) { - $prefix = $hidden ? '_' : ''; - } + if ( ! is_array( $inputs ) || empty( $inputs ) ) { - if ( ! $display_hidden && ( isset( $field->cssClass ) && strpos( $field->cssClass, 'wc-gforms-hide-from-email' ) !== false ) ) { - $prefix = '_gf_email_hidden_'; - $display_title = str_replace( '_gf_email_hidden_', '', $display_title ); - $hidden = true; - } - } + $display_title = GFCommon::get_label( $field ); + $prefix = ''; + $hidden = $field['type'] === 'hidden' || ( isset( $field['visibility'] ) && $field['visibility'] === 'hidden' ); + $display_hidden = apply_filters( 'woocommerce_gforms_field_is_hidden', $hidden, $display_value, $display_title, $field, $lead, $form_meta ); + if ( $display_hidden ) { + $prefix = $hidden ? '_' : ''; + } + + if ( ! $display_hidden && ( isset( $field->cssClass ) && strpos( $field->cssClass, 'wc-gforms-hide-from-email' ) !== false ) ) { + $prefix = '_gf_email_hidden_'; + $display_title = str_replace( '_gf_email_hidden_', '', $display_title ); + $hidden = true; + } + + $cart_item_data = apply_filters( + 'woocommerce_gforms_get_item_data', + [ + 'name' => $prefix . $display_title, + 'display' => $display_text, + 'value' => $display_value, + 'hidden' => $hidden, + ], + $field, + $lead, + $form_meta + ); + + $other_data[] = $cart_item_data; + } else { + + $field_wrapper_label = GFCommon::get_label( $field ); + $field_wrapper_prefix = ''; + $field_wrapper_hidden = $field['type'] === 'hidden' || ( isset( $field['visibility'] ) && $field['visibility'] === 'hidden' ); + + // Add an empty cart item data to the array so that the field wrapper is displayed. + $cart_item_data = apply_filters( + 'woocommerce_gforms_get_item_data', + [ + 'name' => $field_wrapper_prefix . $field_wrapper_label, + 'display' => '', + 'value' => '', + 'hidden' => $field_wrapper_hidden, + ], + $field, + $lead, + $form_meta + ); + + $other_data[] = $cart_item_data; + + // Add a space to the end of the field prefix so that the cart item data is indented. + $field_wrapper_prefix .= ' - '; - $cart_item_data = apply_filters( "woocommerce_gforms_get_item_data", array( - 'name' => $prefix . $display_title, - 'display' => $display_text, - 'value' => $display_value, - 'hidden' => $hidden - ), $field, $lead, $form_meta ); + foreach ( $inputs as $input ) { + $input_label = GFCommon::get_label( $field, $input['id'] ); + $input_value = $value[ $input['id'] ] ?? ''; - $other_data[] = $cart_item_data; + $cart_item_data = apply_filters( + 'woocommerce_gforms_get_item_data', + [ + 'name' => $field_wrapper_prefix . $input_label, + 'display' => $input_value, + 'value' => $input_value, + 'hidden' => $field_wrapper_hidden, + ], + $field, + $lead, + $form_meta + ); + + $other_data[] = $cart_item_data; + } + } + } } } } @@ -522,12 +606,14 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item //slash it so that unicode doesn't get stripped out by WP add_metadata wp_unslash $cart_item_lead = wp_slash( $cart_item['_gravity_form_lead'] ); - $item->add_meta_data( '_gravity_forms_history', array( + $item->add_meta_data( + '_gravity_forms_history', + [ '_gravity_form_hash' => $cart_item['_gravity_form_hash'], '_gravity_form_lead' => $cart_item_lead, '_gravity_form_data' => $cart_item['_gravity_form_data'], - '_gravity_form_cart_item_key' => $cart_item_key - ) + '_gravity_form_cart_item_key' => $cart_item_key, + ] ); GFCommon::log_debug( "Gravity Forms Added Order Item Gravity Forms History: Order Item ID(#{$item_id})" ); @@ -540,18 +626,21 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item //Ensure GFFormDisplay exists in case developers use hooks that expect it to. if ( ! class_exists( 'GFFormDisplay' ) ) { - require_once( GFCommon::get_base_path() . "/form_display.php" ); + require_once GFCommon::get_base_path() . '/form_display.php'; } if ( ! class_exists( 'RGFormsModel' ) ) { - require_once( GFCommon::get_base_path() . "/forms_model.php" ); + require_once GFCommon::get_base_path() . '/forms_model.php'; } $form_meta = RGFormsModel::get_form_meta( $gravity_form_data['id'] ); - $form_meta = gf_apply_filters( array( - 'gform_pre_render', - $gravity_form_data['id'] - ), $form_meta ); + $form_meta = gf_apply_filters( + [ + 'gform_pre_render', + $gravity_form_data['id'], + ], + $form_meta + ); if ( ! empty( $form_meta ) ) { $lead = $cart_item['_gravity_form_lead']; //We reset the lead id to disable caching of the gravity form value by gravity forms. @@ -559,11 +648,11 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item $lead['id'] = uniqid() . time() . rand(); $products = $this->get_product_fields( $form_meta, $lead ); - $valid_products = array(); + $valid_products = []; foreach ( $products['products'] as $id => $product ) { if ( ! isset( $product['quantity'] ) ) { - } elseif ( $product['quantity'] ) { + } else if ( $product['quantity'] ) { $valid_products[] = $id; } } @@ -595,7 +684,7 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item $files = json_decode( $dv ); if ( empty( $files ) ) { - $files = array( $dv ); + $files = [ $dv ]; } $display_value = ''; @@ -614,10 +703,10 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item if ( $field['type'] == 'address' ) { $display_value = implode( ', ', array_filter( $value ) ); } else { - $display_value = GFCommon::get_lead_field_display( $field, $value, isset( $lead["currency"] ) ? $lead["currency"] : false, apply_filters( 'woocommerce_gforms_use_label_as_value', true, $value, $field, $lead, $form_meta ) ); + $display_value = GFCommon::get_lead_field_display( $field, $value, isset( $lead['currency'] ) ? $lead['currency'] : false, apply_filters( 'woocommerce_gforms_use_label_as_value', true, $value, $field, $lead, $form_meta ) ); } - $display_value = apply_filters( "gform_entry_field_value", $display_value, $field, $lead, $form_meta ); + $display_value = apply_filters( 'gform_entry_field_value', $display_value, $field, $lead, $form_meta ); if ( strpos( $display_value, '' ) ) { @@ -640,12 +728,12 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item $display_value = strip_tags( wp_kses( $display_value, '' ) ); } - $display_text = GFCommon::get_lead_field_display( $field, $value, isset( $lead["currency"] ) ? $lead["currency"] : false, false ); - $display_value = apply_filters( "woocommerce_gforms_field_display_text", $display_value, $display_text, $field, $lead, $form_meta ); + $display_text = GFCommon::get_lead_field_display( $field, $value, isset( $lead['currency'] ) ? $lead['currency'] : false, false ); + $display_value = apply_filters( 'woocommerce_gforms_field_display_text', $display_value, $display_text, $field, $lead, $form_meta ); $prefix = ''; $hidden = $field['type'] == 'hidden' || ( isset( $field['visibility'] ) && $field['visibility'] == 'hidden' ); - $display_hidden = apply_filters( "woocommerce_gforms_field_is_hidden", $hidden, $display_value, $display_title, $field, $lead, $form_meta ); + $display_hidden = apply_filters( 'woocommerce_gforms_field_is_hidden', $hidden, $display_value, $display_title, $field, $lead, $form_meta ); if ( $display_hidden ) { $prefix = $hidden ? '_' : ''; } @@ -659,7 +747,8 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item //Set the prefix to hidden if the hidden class is present. $prefix = strpos( $field->cssClass, 'wc-gforms-hide-from-email' ) !== false ? '_' : ''; $display_title = GFCommon::get_label( $field ); - $display_value = str_replace( $display_title . ',', '', $display_value );; + $display_value = str_replace( $display_title . ',', '', $display_value ); + } if ( empty( $prefix ) && empty( $display_title ) ) { @@ -668,18 +757,18 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item $value_debug_string = $prefix . $display_title . ' - Value:' . $display_value; GFCommon::log_debug( "Gravity Forms Adding Order Item Meta:(#{$value_debug_string})" ); - $order_item_meta = array( + $order_item_meta = [ 'name' => $prefix . $display_title, - 'value' => $display_value - ); + 'value' => $display_value, + ]; - $order_item_meta = apply_filters( "woocommerce_gforms_order_item_meta", $order_item_meta, $field, $lead, $form_meta, $item_id, $cart_item ); + $order_item_meta = apply_filters( 'woocommerce_gforms_order_item_meta', $order_item_meta, $field, $lead, $form_meta, $item_id, $cart_item ); if ( $order_item_meta ) { $item->add_meta_data( $order_item_meta['name'], $order_item_meta['value'] ); GFCommon::log_debug( "Gravity Forms Added Order Item Meta:({$order_item_meta['name']} - {$order_item_meta['value']})" ); } else { - GFCommon::log_debug( "(ERROR) Gravity Forms Did Not Add Order Item Meta, It was empty after the filter" ); + GFCommon::log_debug( '(ERROR) Gravity Forms Did Not Add Order Item Meta, It was empty after the filter' ); } } catch ( Exception $e ) { $e_debug_string = $e->getMessage(); @@ -703,7 +792,7 @@ public function order_item_meta( WC_Order_Item $item, $cart_item_key, $cart_item public function on_get_order_again_cart_item_data( $data, $item, $order ) { //disable validation - remove_filter( 'woocommerce_add_to_cart_validation', array( $this, 'add_to_cart_validation' ), 99, 3 ); + remove_filter( 'woocommerce_add_to_cart_validation', [ $this, 'add_to_cart_validation' ], 99, 3 ); return $data; } @@ -740,7 +829,6 @@ private function delete_entry_legacy( $entry ) { $lead_notes_table = GFFormsModel::get_lead_notes_table_name(); $lead_detail_table = GFFormsModel::get_lead_details_table_name(); - //Delete from lead details $sql = $wpdb->prepare( "DELETE FROM $lead_detail_table WHERE lead_id=%d", $lead_id ); $wpdb->query( $sql ); @@ -760,32 +848,59 @@ private function delete_entry_legacy( $entry ) { private function disable_hooks( $form_id ) { //MUST disable notifications manually. - add_filter( 'gform_disable_notification', array( $this, 'disable_notifications' ), 999, 3 ); - - add_filter( 'gform_disable_user_notification', array( $this, 'disable_notifications' ), 999, 3 ); - add_filter( 'gform_disable_user_notification_' . $form_id, array( - $this, - 'disable_notifications' - ), 999, 3 ); - - add_filter( 'gform_disable_admin_notification' . $form_id, array( - $this, - 'disable_notifications' - ), 10, 3 ); - - - add_filter( 'gform_disable_admin_notification_' . $form_id, array( - $this, - 'disable_notifications' - ), 10, 3 ); - - - add_filter( 'gform_disable_notification_' . $form_id, array( $this, 'disable_notifications' ), 999, 3 ); + /* + add_filter( 'gform_disable_notification', [ $this, 'disable_notifications' ], 999, 3 ); + + add_filter( 'gform_disable_user_notification', [ $this, 'disable_notifications' ], 999, 3 ); + add_filter( + 'gform_disable_user_notification_' . $form_id, + [ + $this, + 'disable_notifications', + ], + 999, + 3 + ); + + add_filter( + 'gform_disable_admin_notification' . $form_id, + [ + $this, + 'disable_notifications', + ], + 10, + 3 + ); + + add_filter( + 'gform_disable_admin_notification_' . $form_id, + [ + $this, + 'disable_notifications', + ], + 10, + 3 + ); + + add_filter( 'gform_disable_notification_' . $form_id, [ $this, 'disable_notifications' ], 999, 3 ); + */ + + add_filter( 'gform_confirmation_' . $form_id, [ $this, 'disable_confirmation' ], 998, 4 ); + add_filter( 'gform_pre_send_email', [ $this, 'suppress_form_submission_notifications' ], 50, 4 ); + } - add_filter( "gform_confirmation_" . $form_id, array( $this, "disable_confirmation" ), 998, 4 ); + /** + * Suppress the 'form_submission' event type for notifications when forms are submitted by adding an item to the cart. + */ + public function suppress_form_submission_notifications( $email, $message_format, $notification, $entry ) { + if ( $notification['event'] !== 'form_submission' ) { + return $email; + } - $delete_cart_entries = isset( $gravity_form_data['keep_cart_entries'] ) && $gravity_form_data['keep_cart_entries'] == 'yes' ? false : true; + wc_gfpa()->log_debug( __METHOD__ . "(): Preventing 'form_submission' notification from sending due to the entry being created while adding an item to the cart." ); + $email['abort_email'] = true; + return $email; } private function disable_gform_after_submission_hooks( $form_id ) { @@ -818,7 +933,6 @@ private function disable_gform_after_submission_hooks( $form_id ) { unset( $wp_filter[ $tag ] ); } } - } private function enable_gform_after_submission_hooks( $form_id ) { @@ -869,10 +983,10 @@ public function on_gform_pre_process( $form ) { if ( isset( $field['type'] ) && $field['type'] == 'captcha' ) { $captcha_id = $index; - $this->removed_captcha = array( + $this->removed_captcha = [ 'index' => $index, - 'field' => $field - ); + 'field' => $field, + ]; } } @@ -884,5 +998,4 @@ public function on_gform_pre_process( $form ) { return $form; } - } diff --git a/inc/gravityforms-product-addons-entry.php b/inc/gravityforms-product-addons-entry.php index 061679a..c00da37 100644 --- a/inc/gravityforms-product-addons-entry.php +++ b/inc/gravityforms-product-addons-entry.php @@ -148,8 +148,8 @@ public function create_entries( $order_id, $data = null ) { return false; }, 99999 ); - - $user_id = $the_order->get_user_id() ? $the_order->get_user_id() : $lead_data['created_by']; + $default_created_by = $form_data['created_by'] ?? get_current_user_id(); + $user_id = $the_order->get_user_id() ? $the_order->get_user_id() : $default_created_by; if ($user_id) { $lead_data['created_by'] = $user_id; } diff --git a/inc/gravityforms-product-addons-merge-tags.php b/inc/gravityforms-product-addons-merge-tags.php new file mode 100644 index 0000000..6bc990e --- /dev/null +++ b/inc/gravityforms-product-addons-merge-tags.php @@ -0,0 +1,339 @@ + 'wcgfpa_wc_product', + 'label' => __( 'WooCommerce Product', 'wc_gf_addons' ), + 'desc' => __( 'These merge tags are available when the form is associated with a WooCommerce product. They provide information about the WooCommerce product that is associated with the entry.', 'wc_gf_addons' ), + 'tags' => array( + array( + 'tag' => '{wc_product:name}', + 'label' => __( 'Name', 'wc_gf_addons' ), + 'desc' => __( 'The name of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:sku}', + 'label' => __( 'SKU', 'wc_gf_addons' ), + 'desc' => __( 'The SKU of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:price}', + 'label' => __( 'Price', 'wc_gf_addons' ), + 'desc' => __( 'The price of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:regular_price}', + 'label' => __( 'Regular Price', 'wc_gf_addons' ), + 'desc' => __( 'The regular price of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:sale_price}', + 'label' => __( 'Sale Price', 'wc_gf_addons' ), + 'desc' => __( 'The sale price of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:stock_quantity}', + 'label' => __( 'Stock Quantity', 'wc_gf_addons' ), + 'desc' => __( 'The stock quantity of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:stock_status}', + 'label' => __( 'Stock Status', 'wc_gf_addons' ), + 'desc' => __( 'The stock status of the product.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + array( + 'tag' => '{wc_product:product_url}', + 'label' => __( 'Product URL', 'wc_gf_addons' ), + 'desc' => __( 'The URL to view the product on the website.', 'wc_gf_addons' ), + 'allowed_on' => array_merge( $notification_general_fields, array( 'field_default_value_*' )), + ), + ), + ), + array( + 'key' => 'wcgfpa_wc_order', + 'label' => __( 'WooCommerce Order', 'wc_gf_addons' ), + 'desc' => __( 'These merge tags are available when the form is associated with a WooCommerce product. They provide information about the WooCommerce order that is associated with the entry.', 'wc_gf_addons' ), + 'tags' => + array( + array( + 'tag' => '{wc_order:billing_address}', + 'label' => __( 'Billing Address - Formatted', 'wc_gf_addons' ), + 'desc' => __( 'The billing address of the order. If the order has a user, the shipping address will be used. Otherwise, the billing address will be used.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:billing_email}', + 'label' => __( 'Billing Email', 'wc_gf_addons' ), + 'desc' => __( 'The billing email of the order.', 'wc_gf_addons' ), + 'allowed_on' => $notification_fields, + ), + array( + 'tag' => '{wc_order:customer_id}', + 'label' => __( 'Customer ID', 'wc_gf_addons' ), + 'desc' => __( 'The ID of the customer who placed the order. If the order has a user, the user ID will be used. Otherwise, the billing email will be used.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:customer_email}', + 'label' => __( 'Customer Email', 'wc_gf_addons' ), + 'desc' => __( 'The email address of the customer who placed the order. If the order has a user, the user email will be used. Otherwise, the billing email will be used.', 'wc_gf_addons' ), + 'allowed_on' => $notification_fields, + ), + array( + 'tag' => '{wc_order:customer_display_name}', + 'label' => __( 'Customer Display Name', 'wc_gf_addons' ), + 'desc' => __( 'The display name of the customer who placed the order. If the order has a user, the user display name will be used. Otherwise, the billing email will be used.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:order_url}', + 'label' => __( 'Order URL', 'wc_gf_addons' ), + 'desc' => __( 'The URL to view the order on the website.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:order_id}', + 'label' => __( 'Order ID', 'wc_gf_addons' ), + 'desc' => __( 'The ID of the order.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:order_number}', + 'label' => __( 'Order Number', 'wc_gf_addons' ), + 'desc' => __( 'The order number of the order.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + array( + 'tag' => '{wc_order:shipping_address}', + 'label' => __( 'Shipping Address - Formatted', 'wc_gf_addons' ), + 'desc' => __( 'The shipping address of the order. If the order has a user, the shipping address will be used. Otherwise, the billing address will be used.', 'wc_gf_addons' ), + 'allowed_on' => $notification_general_fields, + ), + ), + ), + ); + + return apply_filters('wc_gf_addons_merge_tags', $groups); + } + + public function replace_woocommerce_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { + preg_match_all( '/{(\w+):(\w+)}/', $text, $matches, PREG_SET_ORDER ); + if ( ! empty( $matches ) ) { + foreach ( $matches as $match ) { + $full_tag = $match[0]; + $prefix = $match[1]; + $tag = $match[2]; + $result = false; + switch ( $prefix ) { + case 'wc_order': + $result = $this->process_merge_tag_wc_order( $tag, $entry, $form, $url_encode, $esc_html, $nl2br, $format ); + break; + case 'wc_product': + $result = $this->process_merge_tag_wc_product( $tag, $entry, $form, $url_encode, $esc_html, $nl2br, $format ); + break; + default: // Do nothing. + break; + } + + if ( $result !== false ) { + $text = str_replace( $full_tag, $result, $text ); + } + } + } + return $text; + } + + private function process_merge_tag_wc_order( $tag, $entry, $form, $url_encode, $esc_html, $nl2br, $format ) { + + $order_id = gform_get_meta( $entry['id'], 'woocommerce_order_number' ); + $the_order = wc_get_order( $order_id ); + + if ( empty( $the_order ) ) { + return false; + } + + $the_order_user = $the_order->get_user(); + + $result = false; + if ( $tag === 'id' || $tag === 'order_id' ) { + $result = $the_order->get_id(); + } + + if ( $tag === 'order_number' ) { + $result = $the_order->get_order_number(); + } + + if ( $tag === 'billing_address' || $tag === 'shipping_address' ) { + if ( $the_order_user ) { + $result = $the_order->get_formatted_shipping_address(); + } else { + $result = $the_order->get_formatted_billing_address(); + } + } + + if ( $tag === 'customer_email' ) { + // If the order has a user, use the user email, otherwise use the billing email. + if ( $the_order_user ) { + $result = $the_order_user->user_email; + } else { + $result = $the_order->get_billing_email(); + } + } + + if ( $tag === 'customer_display_name' ) { + // If the order has a user, use the user display name, otherwise use the billing email. + if ( $the_order_user ) { + $result = $the_order_user->display_name; + } else { + $result = $the_order->get_formatted_billing_full_name(); + } + } + + if ( $tag === 'order_url' ) { + $result = $the_order->get_view_order_url(); + } + + // See if there is a method to handle the tag. + if ( $result === false && method_exists( $the_order, "get_{$tag}" ) ) { + $result = $the_order->{"get_{$tag}"}(); + // If the result is not a simple string, see if it is stringable. + if ( ! is_string( $result ) && method_exists( $result, '__toString' ) ) { + $result = $result->__toString(); + } + } + + return $result; + } + + private function process_merge_tag_wc_product( $tag, $entry, $form, $url_encode, $esc_html, $nl2br, $format ) { + // If the entry is null, let's see if we are on the product page. + if ( empty( $entry ) ) { + $product = wc_get_product(); + if ( empty( $product ) ) { + return false; + } + + $form_id = wc_gfpa()->get_gravity_form_id( $product->get_id() ); + if ( empty( $form_id ) ) { + return false; + } + + // If we have the form, let's do extra checks to make sure this is the form attached to the product. + if ($form && $form_id !== $form['id'] ) { + return false; + } + } else { + $order_id = gform_get_meta( $entry['id'], 'woocommerce_order_number' ); + $order_item_id = gform_get_meta( $entry['id'], 'woocommerce_order_item_number' ); + + if ( ! empty( $order_id ) && ! empty( $order_item_id ) ) { + $order = wc_get_order( $order_id ); + $order_item = $order->get_item( $order_item_id ); + $product = $order_item->get_product(); + } else { + $product_id = gform_get_meta( $entry['id'], 'woocommerce_product_id' ); + if ( empty( $product_id ) ) { + return false; + } + $product = wc_get_product( $product_id ); + } + } + + if ( empty( $product ) ) { + return false; + } + + $result = false; + if ( $tag === 'name' ) { + $result = $product->get_name(); + } + + if ( $tag === 'sku' ) { + $result = $product->get_sku(); + } + + if ( $tag === 'price' ) { + $result = $product->get_price(); + } + + if ( $tag === 'regular_price' ) { + $result = $product->get_regular_price(); + } + + if ( $tag === 'sale_price' ) { + $result = $product->get_sale_price(); + } + + if ( $tag === 'stock_quantity' ) { + $result = $product->get_stock_quantity(); + } + + if ( $tag === 'stock_status' ) { + $result = $product->get_stock_status(); + } + + if ( $tag === 'product_url' ) { + $result = $product->get_permalink(); + } + + // See if there is a method to handle the tag. + if ( $result === false && method_exists( $product, "get_{$tag}" ) ) { + $result = $product->{"get_{$tag}"}(); + // If the result is not a simple string, see if it is stringable. + if ( ! is_string( $result ) && method_exists( $result, '__toString' ) ) { + $result = $result->__toString(); + } + } + + return $result; + } +} diff --git a/inc/integrations/bookings.php b/inc/integrations/bookings.php new file mode 100644 index 0000000..b9eae24 --- /dev/null +++ b/inc/integrations/bookings.php @@ -0,0 +1,76 @@ +get_id(); + if ( $product->is_type( 'variation' ) ) { + $the_product_id = $product->get_parent_id(); + } + + if ( ! wc_gfpa()->has_gravity_form( $the_product_id ) ) { + return $output; + } + + $booking_data = wc_bookings_get_posted_data( $posted, $product ); + $cost = WC_Bookings_Cost_Calculation::calculate_booking_cost( $booking_data, $product ); + + wp_send_json( + array( + 'result' => 'SUCCESS', + 'html' => $output, + 'raw_price' => (float) wc_get_price_to_display( $product, array( 'price' => $cost ) ), + ) + ); + } +}