2019-02-12 21:38:20 +00:00
|
|
|
/* global installed_woo_plugins */
|
2019-02-12 23:19:42 +00:00
|
|
|
( function( $, installed_woo_plugins ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
$( function() {
|
2019-02-12 23:19:42 +00:00
|
|
|
if ( 'undefined' === typeof installed_woo_plugins ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:34:25 +00:00
|
|
|
var marketplaceSuggestionsApiData = [
|
2019-02-12 21:38:20 +00:00
|
|
|
{
|
|
|
|
slug: 'products-empty-header',
|
|
|
|
context: 'products-list-empty-header',
|
2019-02-13 22:16:39 +00:00
|
|
|
title: 'Ready to start selling something awesome?',
|
|
|
|
copy: 'Create your first product, import your product data, or browse extensions'
|
2019-02-12 21:38:20 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
slug: 'products-empty-memberships',
|
|
|
|
context: 'products-list-empty-body',
|
2019-02-13 22:16:39 +00:00
|
|
|
title: 'Selling something else?',
|
|
|
|
copy: 'Extensions allow you to sell other types of products including bookings, subscriptions, or memberships',
|
|
|
|
'button-text': 'Browse extensions',
|
|
|
|
url: 'https://woocommerce.com/product-category/woocommerce-extensions/product-extensions/',
|
2019-02-12 21:38:20 +00:00
|
|
|
},
|
2019-02-13 22:16:39 +00:00
|
|
|
// {
|
|
|
|
// slug: 'products-empty-addons',
|
|
|
|
// context: 'products-list-empty-body',
|
|
|
|
// 'show-if-installed': [
|
|
|
|
// 'woocommerce-subscriptions',
|
|
|
|
// 'woocommerce-memberships'
|
|
|
|
// ],
|
|
|
|
// content: '<div class="marketplace-card">' +
|
|
|
|
// '<h2>Product Add-Ons</h2>' +
|
|
|
|
// '<p>Offer add-ons like gift wrapping, special messages or other special options for your products.</p>' +
|
|
|
|
// '<a class="button" href="https://woocommerce.com/products/product-add-ons/">From $149</a>' +
|
|
|
|
// '</div>'
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// slug: 'products-empty-product-bundles',
|
|
|
|
// context: 'products-list-empty-body',
|
|
|
|
// 'hide-if-installed': 'woocommerce-product-bundles',
|
|
|
|
// content: '<div class="marketplace-card">' +
|
|
|
|
// '<h2>Product Bundles</h2>' +
|
|
|
|
// '<p>Offer customizable bundles and assembled products</p>' +
|
|
|
|
// '<a class="button" href="https://woocommerce.com/products/product-bundles/">From $49</a>' +
|
|
|
|
// '</div>'
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// slug: 'products-empty-composite-products',
|
|
|
|
// context: 'products-list-empty-body',
|
|
|
|
// content: '<div class="marketplace-card">' +
|
|
|
|
// '<h2>Composite Products</h2>' +
|
|
|
|
// '<p>Create and offer product kits with configurable components</p>' +
|
|
|
|
// '<a class="button" href=https://woocommerce.com/products/composite-products/">From $79</a>' +
|
|
|
|
// '</div>'
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// slug: 'products-empty-more',
|
|
|
|
// context: 'products-list-empty-body',
|
|
|
|
// content: '<div class="marketplace-card"><h2>More Extensions</h2></div>'
|
|
|
|
// },
|
2019-02-12 21:38:20 +00:00
|
|
|
{
|
|
|
|
slug: 'products-list-enhancements-category',
|
|
|
|
context: 'products-list-inline',
|
2019-02-13 21:53:25 +00:00
|
|
|
title: 'Looking to optimize your product pages?',
|
|
|
|
'button-text': 'Explore enhancements',
|
|
|
|
url: 'https://woocommerce.com/product-category/woocommerce-extensions/product-extensions/',
|
2019-02-13 00:34:25 +00:00
|
|
|
}
|
2019-02-12 21:38:20 +00:00
|
|
|
];
|
|
|
|
|
2019-02-13 22:16:39 +00:00
|
|
|
function renderLinkoutButton( url, buttonText ) {
|
|
|
|
var linkoutButton = document.createElement( 'a' );
|
|
|
|
|
|
|
|
linkoutButton.classList.add( 'button' );
|
|
|
|
linkoutButton.setAttribute( 'href', url );
|
|
|
|
linkoutButton.textContent = buttonText;
|
|
|
|
|
|
|
|
return linkoutButton;
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:53:25 +00:00
|
|
|
function renderTableBanner( title, url, buttonText ) {
|
|
|
|
if ( ! title || ! url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
buttonText = 'Go';
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = document.createElement( 'tr' );
|
|
|
|
row.classList.add( 'marketplace-list-banner' );
|
|
|
|
|
|
|
|
var titleColumn = document.createElement( 'td' );
|
|
|
|
titleColumn.setAttribute( 'colspan', 5 );
|
|
|
|
titleColumn.classList.add( 'marketplace-list-title' );
|
|
|
|
var titleHeading = document.createElement( 'h2' );
|
|
|
|
titleColumn.append( titleHeading );
|
|
|
|
titleHeading.textContent = title;
|
|
|
|
|
|
|
|
row.appendChild( titleColumn );
|
|
|
|
|
|
|
|
var linkoutColumn = document.createElement( 'td' );
|
|
|
|
linkoutColumn.setAttribute( 'colspan', 4 );
|
|
|
|
linkoutColumn.classList.add( 'marketplace-list-linkout' );
|
2019-02-13 22:16:39 +00:00
|
|
|
var linkoutButton = renderLinkoutButton( url, buttonText );
|
2019-02-13 21:53:25 +00:00
|
|
|
linkoutColumn.append( linkoutButton );
|
|
|
|
|
|
|
|
row.appendChild( linkoutColumn );
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:16:39 +00:00
|
|
|
function renderListItem( title, copy, url, buttonText ) {
|
|
|
|
if ( ! title || ! url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
buttonText = 'Go';
|
|
|
|
}
|
|
|
|
|
|
|
|
var container = document.createElement( 'div' );
|
|
|
|
container.classList.add( 'marketplace-list-container' );
|
|
|
|
|
|
|
|
var titleHeading = document.createElement( 'h4' );
|
|
|
|
titleHeading.textContent = title;
|
|
|
|
container.appendChild( titleHeading );
|
|
|
|
|
|
|
|
var body = document.createElement( 'p' );
|
|
|
|
body.textContent = copy;
|
|
|
|
container.appendChild( body );
|
|
|
|
|
|
|
|
var linkoutButton = renderLinkoutButton( url, buttonText );
|
|
|
|
container.appendChild( linkoutButton );
|
|
|
|
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2019-02-13 01:35:56 +00:00
|
|
|
var visibleSuggestions = [];
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
function getRelevantPromotions( displayContext ) {
|
|
|
|
// select based on display context
|
2019-02-13 00:34:25 +00:00
|
|
|
var promos = _.filter( marketplaceSuggestionsApiData, function( promo ) {
|
|
|
|
return ( displayContext === promo.context );
|
|
|
|
} );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
|
|
// hide promos for things the user already has installed
|
2019-02-13 00:34:25 +00:00
|
|
|
promos = _.filter( promos, function( promo ) {
|
|
|
|
return ! _.contains( installed_woo_plugins, promo['hide-if-installed'] );
|
|
|
|
} );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
2019-02-12 22:33:37 +00:00
|
|
|
// hide promos that are not applicable based on user's installed extensions
|
2019-02-13 00:34:25 +00:00
|
|
|
promos = _.filter( promos, function( promo ) {
|
2019-02-12 22:33:37 +00:00
|
|
|
if ( ! promo['show-if-installed'] ) {
|
|
|
|
// this promotion is relevant to all
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the user has any of the prerequisites, show the promo
|
|
|
|
return ( _.intersection( installed_woo_plugins, promo['show-if-installed'] ).length > 0 );
|
|
|
|
} );
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
return promos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all suggestions containers, rendering promos
|
|
|
|
$( '.marketplace-suggestions-container' ).each( function() {
|
|
|
|
// determine the context / placement we're populating
|
2019-02-13 00:34:25 +00:00
|
|
|
var context = this.dataset.marketplaceSuggestionsContext;
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
|
|
// find promotions that target this context
|
2019-02-13 00:34:25 +00:00
|
|
|
var promos = getRelevantPromotions( context );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
|
|
// render the promo content
|
|
|
|
for ( var i in promos ) {
|
2019-02-13 22:16:39 +00:00
|
|
|
var content = renderListItem(
|
|
|
|
promos[ i ].title,
|
|
|
|
promos[ i ].copy,
|
|
|
|
promos[ i ].url,
|
|
|
|
promos[ i ]['button-text']
|
|
|
|
);
|
|
|
|
$( this ).append( content );
|
2019-02-13 01:35:56 +00:00
|
|
|
visibleSuggestions.push( promos[i].context );
|
2019-02-12 21:38:20 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// render inline promos in products list
|
|
|
|
$( '.wp-admin.admin-bar.edit-php.post-type-product table.wp-list-table.posts tbody').first().each( function() {
|
2019-02-13 00:34:25 +00:00
|
|
|
var context = 'products-list-inline';
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
|
|
// find promotions that target this context
|
2019-02-13 00:34:25 +00:00
|
|
|
var promos = getRelevantPromotions( context );
|
2019-02-13 21:53:25 +00:00
|
|
|
if ( ! promos || ! promos.length ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-12 21:38:20 +00:00
|
|
|
|
2019-02-13 21:53:25 +00:00
|
|
|
// render first promo
|
|
|
|
var content = renderTableBanner(
|
|
|
|
promos[ 0 ].title,
|
|
|
|
promos[ 0 ].url,
|
|
|
|
promos[ 0 ]['button-text']
|
|
|
|
);
|
2019-02-12 21:38:20 +00:00
|
|
|
|
2019-02-13 21:53:25 +00:00
|
|
|
if ( content ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
// where should we put it in the list?
|
2019-02-13 00:34:25 +00:00
|
|
|
var rows = $( this ).children();
|
|
|
|
var minRow = 3;
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
|
|
if ( rows.length <= minRow ) {
|
|
|
|
// if small number of rows, append at end
|
|
|
|
$( this ).append( content );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// for more rows, append
|
|
|
|
$( rows[ minRow - 1 ] ).after( content );
|
|
|
|
}
|
2019-02-13 21:53:25 +00:00
|
|
|
|
2019-02-13 01:35:56 +00:00
|
|
|
visibleSuggestions.push( context );
|
2019-02-12 21:38:20 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-02-13 01:35:56 +00:00
|
|
|
// streamline layout if we're showing empty product list promos
|
|
|
|
if ( _.contains( visibleSuggestions, 'products-list-empty-body' ) ) {
|
|
|
|
$('#wpfooter').hide();
|
|
|
|
}
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
});
|
|
|
|
|
2019-02-12 23:19:42 +00:00
|
|
|
})( jQuery, installed_woo_plugins );
|