trackingId = Settings::get( 'pinterest_tag_id' );
if ( $this->is_activated() ) {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueueScript' ] );
add_action( 'wp_head', [ $this, 'loadBaseScript' ] );
}
// Ajax adds to cart
add_action( 'wp_ajax_add_to_cart_pinterest_tag', [ $this, 'ajax_add_to_cart_data' ] );
add_action( 'wp_ajax_nopriv_add_to_cart_pinterest_tag', [ $this, 'ajax_add_to_cart_data' ] );
}
/**
* Is Pinterest Tracking Enabled.
*
* @return bool
*/
public function is_activated() {
return ! empty( $this->trackingId ) && 'enable' === Settings::get( 'pinterest_conversion_tracking' );
}
/**
* Enqueue Ajax Add to Cart Event Code.
*
* @return void
*/
public function enqueueScript() {
wp_enqueue_script( 'woo-feed-pinterest-tag,', WOO_FEED_PLUGIN_URL . 'admin/js/woo-feed-pinterest-tag.min.js', [
'jquery',
'wp-util'
], '1.0.0', true );
}
/**
* Load Base Script.
*
* @return void
*/
public function loadBaseScript() {
$email = ' ';
$current_user = wp_get_current_user();
if ( ! is_null( $current_user ) && isset( $current_user->user_email ) && ! empty( $current_user->user_email ) ) {
$email = $current_user->user_email;
}
// @ToDo Language Code check. Currently passing all to `en_US`
?>
PageView();
$this->ViewContent();
$this->AddToCart();
$this->AddToCarts();
$this->Purchase();
}
/**
* Get item info by Ids.
*
* @param array $ids
* @param string $event
*
* @return array|false
*/
private function get_content_info( $ids = [], $event = '' ) {
if ( ! empty( $ids ) ) {
$data['currency'] = get_woocommerce_currency();
if ( $event === 'checkout' ) {
$data['order_quantity'] = '1';
}
$value = 0;
foreach ( $ids as $id ) {
$product = wc_get_product( $id );
if ( ! is_object( $product ) ) {
continue;
}
$data['line_items'][]['product_id'] = $product->get_id();
$value += (int) $product->get_price();
}
$data['value'] = $value;
return $data;
}
return false;
}
/**
* Load PageView Event Script.
*
* @return void
*/
private function PageView() {
$url = $this->make_url( [], 'pagevisit' )
?>
ID );
$id = $_product->get_ID();
$ids = [ $id ];
if ( "variable" === $_product->get_type() ) {
$ids = array_merge( $ids, $_product->get_children() );
}
$data = $this->get_content_info( $ids );
$url = $this->make_url( $data, 'pagevisit' );
if ( $data ) {
?>
get_content_info( [ $product_id ], 'addtocart' );
$url = $this->make_url( $data, 'addtocart' );
if ( $data ) {
?>
cart->is_empty() ) {
$ids = [];
foreach ( WC()->cart->get_cart() as $cart_item ) {
$ids[] = $cart_item['product_id'];
}
$data = $this->get_content_info( $ids, 'addtocart' );
$url = $this->make_url( $data, 'addtocart' );
if ( $data ) {
?>
query_vars['order-received'] ) ) {
$order = wc_get_order( $wp_query->query_vars['order-received'] );
$ids = [];
foreach ( $order->get_items() as $item ) {
$ids[] = $item->get_product_id();
}
$data = $this->get_content_info( $ids, 'checkout' );
$url = $this->make_url( $data, 'checkout' );
if ( $data ) {
?>
get_content_info( [ $product_id ] );
}
wp_send_json_success( json_encode( $data ) );
}
/**
* Make noscript args
*
* @param $args
* @param $event
*
* @return string
*/
private function make_url( $args, $event ) {
$base_url = "https://ct.pinterest.com/v3/";
$base_args = [
'tid' => $this->trackingId,
'event' => $event,
'noscript' => '1',
];
$newArgs = [];
if ( ! empty( $args ) ) {
foreach ( $args as $key => $arg ) {
$newArgs[ "ed[" . $key . "]" ] = $arg;
}
}
$args = array_merge( $base_args, $newArgs );
return add_query_arg( array_filter( $args ), $base_url );
}
}