Skip to content

Commit

Permalink
Merge pull request #77 from DripEmail/fix-get-cart-issue
Browse files Browse the repository at this point in the history
Fix get cart should not be called before wp_loaded handling
  • Loading branch information
diegodrip authored Jul 27, 2023
2 parents 50e6436 + 06de5ec commit 02956f4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
13 changes: 7 additions & 6 deletions devtools_wp/cypress/integration/CartInteractions/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Then('I restore it to the cart', () => {
Then('I increase the quantity in the cart', () => {
cy.visit(`http://localhost:3007/?page_id=${CartPageId}`)
cy.contains("Update cart")
cy.get('input[title="Qty"]').first().clear().type('9001')
cy.get('.quantity input.input-text').first().clear().type('9001')
cy.wrap(Mockclient.reset())
cy.get('button[name="update_cart"]').click().wait(300)
cy.contains("Cart updated.")
Expand All @@ -99,7 +99,7 @@ Then('I increase the quantity in the cart', () => {
Then('I decrease the quantity in the cart to zero', () => {
cy.visit(`http://localhost:3007/?page_id=${CartPageId}`)
cy.contains("Update cart")
cy.get('input[title="Qty"]').first().clear().type('0')
cy.get('.quantity input.input-text').first().clear().type('0')
cy.wrap(Mockclient.reset())
cy.get('button[name="update_cart"]').click().wait(300)
cy.contains("Your cart is currently empty.")
Expand Down Expand Up @@ -155,7 +155,7 @@ Then('I get sent a webhook with visitor_uuid', () => {
expect(Number(event.total_taxes)).to.eq(0)
expect(Number(event.total_fees)).to.eq(0)
expect(Number(event.total_shipping)).to.eq(0)
expect(event.currency).to.eq('GBP')
expect(event.currency).to.eq('USD')
expect(event.grand_total).to.eq('10.99')
expect(Object.keys(event.cart_data)).to.have.lengthOf(1)
validateMyFairWidget(event)
Expand All @@ -174,7 +174,8 @@ Then('I get sent a webhook with an empty cart', () => {
}
)).then(function (recordedRequests) {
cy.wrap(validateRequests(recordedRequests)).then(function (body) {
const event = validateRequestBody(body)
const event = validateRequestBody(recordedRequests[0].body.json)
// expected [] to have a length of 1 but got 0
expect(Object.keys(event.cart_data)).to.have.lengthOf(0)
expect(event.grand_total).to.eq(0)
expect(event.cart_data).to.be.empty
Expand All @@ -193,7 +194,7 @@ Then('I get sent an updated webhook', () => {
}
)).then(function (recordedRequests) {
cy.wrap(validateRequests(recordedRequests)).then(function (body) {
const event = validateRequestBody(body)
const event = validateRequestBody(recordedRequests[0].body.json)
expect(Object.keys(event.cart_data)).to.have.lengthOf(1)
validateMyFairWidget(event, 9001)
expect(event.grand_total.toString()).to.eq(`${10.99 * 9001}`)
Expand Down Expand Up @@ -328,7 +329,7 @@ const validateRequestBody = function (body) {
expect(Number(event.total_taxes)).to.eq(0)
expect(Number(event.total_fees)).to.eq(0)
expect(Number(event.total_shipping)).to.eq(0)
expect(event.currency).to.eq('GBP')
expect(event.currency).to.eq('USD')
return event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Then("the page includes a Drip JS API call", () => {
expect(product.name).to.eq('My Fair Widget')
expect(product.price.toString()).to.eq('1099')
expect(product.product_url).to.eq('http://localhost:3007/?product=fair-widget')
expect(product.currency).to.eq('GBP')
expect(product.currency).to.eq('USD')
expect(product.categories).to.eq('my fair category')
})
})
Expand All @@ -45,7 +45,7 @@ Then("the page includes a null-price Drip JS API call", () => {
expect(product.name).to.eq('My Fair Widget Null')
expect(product.price.toString()).to.eq('0')
expect(product.product_url).to.eq('http://localhost:3007/?product=fair-widget-null')
expect(product.currency).to.eq('GBP')
expect(product.currency).to.eq('USD')
expect(product.categories).to.eq('my fair category')
})
})
2 changes: 1 addition & 1 deletion drip.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Plugin Name: Drip for WooCommerce
Plugin URI: https://github.com/DripEmail/drip-woocommerce
Description: A WordPress plugin to connect to Drip's WooCommerce integration
Version: 1.1.4
Version: 1.1.5
Author: Drip
Author URI: https://www.drip.com/
License: GPLv2
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: getdrip
Tags: ecommerce, emailmarketing, marketingautomation, emailmarketingautomation, woocommerce, drip
Requires at least: 4.6
Tested up to: 6.2.2
Stable tag: 1.1.4
Stable tag: 1.1.5
Requires PHP: 5.6
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -73,6 +73,9 @@ The philosophy behind this plugin is to do as little as possible in it, and as m
= NEXT =

* Your change here!
= 1.1.5 =

* Fix bug when calling get_cart function before WP finishes loading

= 1.1.4 =

Expand Down
19 changes: 13 additions & 6 deletions src/class-drip-woocommerce-cart-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ public static function init() {
}

/**
* Set up cart action callbacks
* Set up cart actions ensuring that actions will be registered after WordPress has fully loaded
*/
public function setup_cart_actions() {
add_action( 'woocommerce_after_cart_item_quantity_update', array( $this, 'drip_woocommerce_cart_updated' ), 10, 0 );
add_action( 'woocommerce_cart_item_removed', array( $this, 'drip_woocommerce_cart_updated' ), 10, 0 );
add_action( 'woocommerce_add_to_cart', array( $this, 'drip_woocommerce_cart_updated' ), 10, 0 );
add_action( 'woocommerce_cart_item_restored', array( $this, 'drip_woocommerce_cart_updated' ), 10, 0 );
add_action( 'woocommerce_cart_emptied', array( $this, 'drip_woocommerce_cart_updated' ), 10, 0 ); // no way to get this from the UI without plugins.
add_action('wp_loaded', array($this, 'register_cart_actions'));
}

/**
* Register cart action callbacks
*/
public function register_cart_actions() {
add_action('woocommerce_after_cart_item_quantity_update', array($this, 'drip_woocommerce_cart_updated'), 10, 0);
add_action('woocommerce_cart_item_removed', array($this, 'drip_woocommerce_cart_updated'), 10, 0);
add_action('woocommerce_add_to_cart', array($this, 'drip_woocommerce_cart_updated'), 10, 0);
add_action('woocommerce_cart_item_restored', array($this, 'drip_woocommerce_cart_updated'), 10, 0);
add_action('woocommerce_cart_emptied', array($this, 'drip_woocommerce_cart_updated'), 10, 0);
}

/**
Expand Down

0 comments on commit 02956f4

Please sign in to comment.