Skip to content

Woocommerce integration createAddToCartEvent causes fatal error #88

@Acentrix-Jordan

Description

@Acentrix-Jordan

The current site is a single-page checkout that utilises a custom Elementor Widget.

The site has worked perfectly fine until we added the Meta Pixel plugin.

Stack Trace:

Stack trace:
#0 [redacted]/public_html/wp-content/plugins/official-facebook-pixel/integration/class-facebookwordpresswoocommerce.php(427): FacebookPixelPlugin\Integration\FacebookWordpressWooCommerce::getProductId()
#1 [internal function]: FacebookPixelPlugin\Integration\FacebookWordpressWooCommerce::createAddToCartEvent()
#2 [redacted]/public_html/wp-content/plugins/official-facebook-pixel/core/class-servereventfactory.php(307): call_user_func_array()
#3 [redacted]/public_html/wp-content/plugins/official-facebook-pixel/integration/class-facebookwordpresswoocommerce.php(350): FacebookPixelPlugin\Core\ServerEventFactory::safe_create_event()
#4[redacted]/wp-includes/class-wp-hook.php(326): FacebookPixelPlugin\Integration\FacebookWordpressWooCommerce::trackAddToCartEvent()
#5 [redacted]/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#6 [redacted]/public_html/wp-includes/plugin.php(517): WP_Hook->do_action()
#7 /[redacted]/public_html/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1292): do_action()
#8 [redacted]/public_html/wp-content/plugins/single-checkout-addon-prod/custom-woocommerce/endpoints.php(70): WC_Cart->add_to_cart()
#9 [redacted]/public_html/wp-includes/class-wp-hook.php(324): scw_add_to_card_action_callback()
#10 [redacted]/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#11 [redacted]/public_html/wp-includes/plugin.php(517): WP_Hook->do_action()
#12 [redacted]/public_html/wp-admin/admin-ajax.php(207): do_action()
#13 {main}
  thrown in[redacted]/public_html/wp-content/plugins/official-facebook-pixel/integration/class-facebookwordpresswoocommerce.php on line 646

Method causing issue

 public static function createAddToCartEvent( $cart_item_key, $product_id, $quantity ) {
    $event_data                 = self::getPIIFromSession();
    $event_data['content_type'] = 'product';
    $event_data['currency']     = \get_woocommerce_currency();

    $cart_item = self::getCartItem( $cart_item_key ); // <--- Why does this not contain the product id

    if ( ! empty( $cart_item_key ) ) { // <-- Why only test if not empty here when variable has been passed to a prior method
      $event_data['content_ids'] = array(
        self::getProductId( // <-- this could be omitted / replaced by formatting utility, if getCartItem contains the productId
            $cart_item['data']  // <-- For some reason Null is being passed
        ),
      );

    $event_data['value'] = self::getAddToCartValue(
        $cart_item,
        $quantity
    );
    }

    return $event_data;
  }

Code implemented to prevent site having a fatal error:

  public static function createAddToCartEvent(
        $cart_item_key,
        $product_id,
        $quantity
    ) {
        $event_data                 = self::getPIIFromSession();
        $event_data['content_type'] = 'product';
        $event_data['currency']     = \get_woocommerce_currency();

        $cart_item = self::getCartItem( $cart_item_key );

        if ( ! empty( $cart_item_key ) ) { 
                 // Exhaustive search O(n), our site can have max 1 product in cart so becomes O(1)
                 // Better implementation could use binary search to get O(log n)
		$found = false;

                 // Get Cart Items
		$cart_items = WC()->cart->get_cart();
		
                 // Search for the cart item that has key
		foreach($cart_items as $_cart_item) {
			if($_cart_item['key'] === $cart_item_key) {
				$found = $_cart_item;
                                 break;
			}
		}
			
		$data = isset($found['sku'])
			? $found['sku'] . '_' . $found['product_id'] 
			: self::FB_ID_PREFIX . $found['product_id'];
			
            $event_data['content_ids'] = array(
                $data
            );
			
            $event_data['value']       = self::getAddToCartValue(
                $cart_item,
                $quantity
            );
        }

        return $event_data;
    }

Get Product ID Method:

private static function getProductId( $product ) {
   // Check if methods exist prior to trying to access 
    $woo_id = $product->get_id(); // <-- This was causing fatal error 

    return $product->get_sku()
      ? $product->get_sku() . '_' . $woo_id
      : self::FB_ID_PREFIX . $woo_id;
  }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions