co-cart / co-cart

🛒 CoCart makes it easy to decouple your WooCommerce store via a customizable REST API that takes the pain out of developing – allowing you to build fast and flexible headless stores.

Home Page:https://cocartapi.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snap Pixel compatibility error

cha0ran opened this issue · comments

The Snap Pixel plugin causes the add car interface to return data exceptions because it does html code output on the woocommerce_add_to_car hook, which is a really bad piece of code. Is there any way to fix it?

public function snap_pixel_code_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data)
{
    if (!$this->snap_pixel_woocommerce_exists() || $this->addtocart !== 'checked') {
        return;
    }

    $term_list = wp_get_post_terms($product_id, 'product_cat', array(
        'fields' => 'ids',
    ));

    $cat_id = (int)$term_list[0];
    $category = get_term($cat_id, 'product_cat');
    $product_category = $category->name;
    $_product = wc_get_product($product_id);
    $product_price = $_product->get_price();
    $product_currency = get_woocommerce_currency(); ?>
    <!-- ADD_CART Snap Pixel Event -->
    <script>
        setTimeout(function () {
            snaptr('track', 'ADD_CART', {
                'currency': '<?php echo esc_js($product_currency); ?>',
                'price': <?php echo esc_js($product_price); ?>,
                'item_category': '<?php echo esc_js($product_category); ?>',
                'item_ids': [
                    '<?php echo esc_js($product_id); ?>'
                ]
            });
        }, 1500);
    </script>
    <!-- /ADD_CART Snap Pixel Event -->
    <?php
}

OMG😡

I don't see how it's adding data to the cart. It makes no sense to have JS trigger there. If this is the official Snap chat plugin in partner with WooCommerce then WooCommerce should help the developers improve this.

If you have any further information please post here otherwise for the time being I'm closing this issue.

  1. request /wp-json/cocart/v2/cart/add-item
  2. When executed, the woocommerce_add_to_cart hook is triggered.
// cart-rest-api-for-woocommerce/includes/api/class-cocart-add-item-controller.php line:260
$item_key = $controller->get_cart_instance()->add_to_cart( $product_id, $quantity, $variation_id, $variation, $item_data );

At this point the snap plugin code on the hook is executed, because the javascript script in the code is not in the <?php ? > tag, which is equivalent to echo this code directly at this point, resulting in this extra code before the interface returns the information.

The final interface returns the following.

			<!-- ADD_CART Snap Pixel Event -->
			<script>
				setTimeout(function() {
					snaptr('track', 'ADD_CART', {
						'currency': 'AUD',
						'price': 10,
						'item_category': 'Accessories',
						'item_ids': [
							'64'
						]
					});
				}, 1500);
			</script>
			<!-- /ADD_CART Snap Pixel Event -->
			{"cart_hash":"1cfc56d71a78c0bce144a3913b2b5762","cart_key":"12312331","currency":{"currency_code":"AUD","currency_symbol":"$","currency_minor_unit":2,"currency_decimal_separator":".","currency_thousand_separator":"","currency_prefix":"$ ","currency_suffix":""},"customer":{"billing_address":{"billing_first_name":"","billing_last_name":"","billing_company":"","billing_country":"CN","billing_address_1":"","billing_address_2":"","billing_city":"","billing_state":"CN2","billing_postcode":"","billing_phone":"","billing_email":"","kl_newsletter_checkbox":""},"shipping_address":{"shipping_first_name":"","shipping_last_name":"","shipping_company":"","shipping_country":"CN","shipping_address_1":"","shipping_address_2":"","shipping_city":"","shipping_state":"CN2","shipping_postcode":""}},"items":[{"item_key":"e2c420d928d4bf8ce0ff2ec19b371514","id":71,"name":"Polo","title":"Polo","price":"20.00","quantity":{"value":1,"min_purchase":1,"max_purchase":-1},"tax_data":{"subtotal":{"1":0},"total":{"1":0}},"totals":{"subtotal":20,"subtotal_tax":0,"total":20,"tax":0},"slug":"polo","meta":{"product_type":"simple","sku":"woo-polo","dimensions":{"length":"","width":"","height":"","unit":"cm"},"weight":0,"variation":[]},"backorders":"","cart_item_data":[],"featured_image":"https:\/\/local.shop\/wp-content\/uploads\/2021\/05\/polo-2-247x296.jpg"},{"item_key":"76dd3d5611f824c2835c4ebea72bd5f9","id":205,"name":"Beanie - Gray","title":"Beanie","price":"10.00","quantity":{"value":6,"min_purchase":1,"max_purchase":-1},"tax_data":{"subtotal":{"1":0},"total":{"1":0}},"totals":{"subtotal":60,"subtotal_tax":0,"total":60,"tax":0},"slug":"beanie","meta":{"product_type":"variation","sku":"woo-beanie","dimensions":{"length":"","width":"","height":"","unit":"cm"},"weight":0,"variation":{"Color":"Gray"}},"backorders":"","cart_item_data":[],"featured_image":"https:\/\/local.shop\/wp-content\/uploads\/2021\/05\/beanie-2-247x296.jpg"}],"item_count":7,"items_weight":0,"coupons":[],"needs_payment":true,"needs_shipping":true,"shipping":{"total_packages":1,"show_package_details":false,"has_calculated_shipping":false,"packages":{"default":{"package_name":"","rates":{"free_shipping:2":{"key":"free_shipping:2","method_id":"free_shipping","instance_id":2,"label":"\u514d\u8d39\u914d\u9001","cost":"0.00","html":"\u514d\u8d39\u914d\u9001","taxes":[],"chosen_method":true}},"package_details":"","index":0,"chosen_method":"free_shipping:2","formatted_destination":"Beijing \/ \u5317\u4eac"}}},"fees":[],"taxes":[],"totals":{"subtotal":"8000","subtotal_tax":"0","fee_total":"0","fee_tax":"0","discount_total":"0","discount_tax":"0","shipping_total":"0","shipping_tax":"0","total":"8000","total_tax":"0"},"removed_items":[],"cross_sells":[],"notices":{"success":["&ldquo;Beanie&rdquo; has been added to your cart."]}}

@seb86
Hi, sorry to bother you again on this issue, I gave the problem in the code in my last reply and hope to get help. Maybe I should have asked snap for help more, but they are very slow to respond and I can only continue to ask for help here.

I have tried to remove the hook but without success and now have to disable the snap plugin, but I still hope they will work together.