WC_Products_Tracking::track_product_updated_client_side
Track the Update button being clicked on the client side. This is needed because track_product_updated (using the edit_post is called in response to a number of other triggers.
Method of the class: WC_Products_Tracking{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Products_Tracking = new WC_Products_Tracking(); $WC_Products_Tracking->track_product_updated_client_side( $post );
- $post(WP_Post) (required)
- The post, not used.
WC_Products_Tracking::track_product_updated_client_side() WC Products Tracking::track product updated client side code WC 10.8.1
public function track_product_updated_client_side( $post ) {
$handle = 'wc-tracks-product-updated-client-side';
wp_register_script( $handle, '', array( 'jquery' ), WC_VERSION, array( 'in_footer' => true ) );
wp_enqueue_script( $handle );
wp_add_inline_script(
$handle,
"
jQuery(function($) {
if ( $( 'h1.wp-heading-inline' ).text().trim() === '" . esc_js( __( 'Edit product', 'woocommerce' ) ) . "') {
var initialStockValue = $( '#_stock' ).val();
var isBlockEditor = false;
var child_element = '#publish';
if ( $( '.block-editor' ).length !== 0 && $( '.block-editor' )[0] ) {
isBlockEditor = true;
}
if ( isBlockEditor ) {
child_element = '.editor-post-publish-button';
}
$( '#wpwrap' ).on( 'click', child_element, function() {
var description_value = '';
var tagsText = '';
var currentStockValue = $( '#_stock' ).val();
function getProductTypeOptions() {
const productTypeOptionsCheckboxes = $( 'input[type=\"checkbox\"][data-product-type-option-id]' );
const productTypeOptions = productTypeOptionsCheckboxes.map( function() {
return {
id: $( this ).data( 'product-type-option-id' ),
isEnabled: $( this ).is( ':checked' ),
};
} ).get();
return productTypeOptions;
}
function getProductTypeOptionsString( productTypeOptions ) {
return productTypeOptions
.filter( productTypeOption => productTypeOption.isEnabled )
.map( productTypeOption => productTypeOption.id )
.join( ', ' );
}
const productTypeOptions = getProductTypeOptions();
const productTypeOptionsString = getProductTypeOptionsString( productTypeOptions );
if ( ! isBlockEditor ) {
tagsText = $( '[name=\"tax_input[product_tag]\"]' ).val();
if ( $( '#content' ).is( ':visible' ) ) {
description_value = $( '#content' ).val();
} else if ( typeof tinymce === 'object' && tinymce.get( 'content' ) ) {
description_value = tinymce.get( 'content' ).getContent();
}
} else {
description_value = $( '.block-editor-rich-text__editable' ).text();
}
// We can't just check the number of '.woocommerce_attribute' elements because
// there might be empty ones, which get stripped out when saved. So, we'll check
// whether the name and values have been filled out.
var numberOfAttributes = $( '.woocommerce_attribute' ).filter( function () {
var attributeElement = $( this );
var attributeName = attributeElement.find( 'input.attribute_name' ).val();
var attributeValues = attributeElement.find( 'textarea[name^=\"attribute_values\"]' ).val();
return attributeName !== '' && attributeValues !== '';
} ).length;
var properties = {
attributes: numberOfAttributes,
categories: $( '[name=\"tax_input[product_cat][]\"]:checked' ).length,
cross_sells: $( '#crosssell_ids option' ).length ? 'Yes' : 'No',
description: description_value.trim() !== '' ? 'Yes' : 'No',
enable_reviews: $( '#comment_status' ).is( ':checked' ) ? 'Yes' : 'No',
is_virtual: $( '#_virtual' ).is( ':checked' ) ? 'Yes' : 'No',
is_block_editor: isBlockEditor,
is_downloadable: $( '#_downloadable' ).is( ':checked' ) ? 'Yes' : 'No',
manage_stock: $( '#_manage_stock' ).is( ':checked' ) ? 'Yes' : 'No',
menu_order: parseInt( $( '#menu_order' ).val(), 10 ) !== 0 ? 'Yes' : 'No',
product_gallery: $( '#product_images_container .product_images > li' ).length,
product_image: $( '#_thumbnail_id' ).val() > 0 ? 'Yes' : 'No',
product_type: $( '#product-type' ).val(),
product_type_options_string: productTypeOptionsString,
purchase_note: $( '#_purchase_note' ).val().length ? 'yes' : 'no',
sale_price: $( '#_sale_price' ).val() ? 'yes' : 'no',
short_description: $( '#excerpt' ).val().length ? 'yes' : 'no',
stock_quantity_update: ( initialStockValue != currentStockValue ) ? 'Yes' : 'No',
tags: tagsText.length > 0 ? tagsText.split( ',' ).length : 0,
upsells: $( '#upsell_ids option' ).length ? 'Yes' : 'No',
weight: $( '#_weight' ).val() ? 'Yes' : 'No',
};
if ( window.wcTracks && window.wcTracks.recordEvent ) {
window.wcTracks.recordEvent( 'product_update', properties );
}
} );
}
});
"
);
}