Our client requested a complex marketplace consisting of a lot of moving parts. This produced challenges which we overcame through rigorous planning and powerful code.
On this page we have listed some useful code we used to achieve the goals requested of us. We hope you find them useful. If any code is out of date or if you require clarification, don’t hesitate to leave us a message.
The code
Use the snippets that suit your needs. PHP is added to your functions.php file and SQL queries are run from your database admin panel (for example, phpMyAdmin). Best practices are constantly evolving; although every effort is made to keep these snippets current, their use may result in issues or loss.
Add a CSS class to a single product page body (PHP)
This filter adds a product-type class to the page body so you can target product types in CSS, for example .simple-product .tab1 { display: none; }.
/** Add product type body class **/
add_filter('body_class','woocommerce_body_classes');
function woocommerce_body_classes( $classes ) {
if ( is_product() ) {
global $woocommerce, $post, $product;
$product = get_product( $post->ID );
$product_type = $product->product_type;
if ( $product->product_type == 'external' ) $classes[] = 'external-product';
if ( $product->product_type == 'grouped' ) $classes[] = 'grouped-product';
if ( $product->product_type == 'simple' ) $classes[] = 'simple-product';
if ( $product->product_type == 'variable' ) $classes[] = 'variable-product';
return $classes;
}
}Remove products from the catalogue when archived (PHP)
By default, products are not removed from the catalogue when archived. This hook hides them.
/** Remove from catalog when status is archived **/
add_action( 'wcfm_after_product_archived', function( $product_id ) {
$product = wc_get_product( $product_id );
$product->set_catalog_visibility( 'hidden' );
$product->save();
});Disable banner selection for vendors (PHP)
Restricts vendors from choosing a banner type or changing the store-name position.
/** Disable store banner type selection for vendors **/
add_filter( 'wcfm_is_allow_store_banner_type', '__return_false' );
/** Disable store name position option for vendors**/
add_filter( 'wcfm_marketplace_settings_fields_visibility', function( $visibility_fields, $store_id ) {
$visibility_fields = wcfm_hide_field( 'store_name_position', $visibility_fields );
return $visibility_fields;
}, 50, 2 );Create shortcodes for ACF metadata (PHP)
This example adds a shortcode that outputs a “Start Time” label followed by the ACF field value for course_start. The shortcode is [ACF_course_start].
/** Create shortcode **/
function ACF_course_start() {
global $product;
if ( get_field('course_start') ) {
echo '<wp-p><strong>Start Time:</strong> ' . get_field('course_start') . '</wp-p>';
}
}
add_shortcode( 'ACF_course_start', 'ACF_course_start' );Limit vendor file types and upload size (PHP)
These filters limit non-administrators to uploading PNG and JPEG files up to 5 MB.
/** Limit vendors to png and jpegs **/
add_filter('upload_mimes','restict_mimes_all_but_admin');
function restict_mimes_all_but_admin($mimes) {
if (!current_user_can('administrator')) {
$mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
);
}
return $mimes;
}
/** Set the upload size limit for all but admin **/
function upload_size_limit( $size ) {
if ( ! current_user_can( 'administrator' ) ) {
// Current: 5 MB.
$size = 1024 * 5000;
}
return $size;
}
add_filter( 'upload_size_limit', 'upload_size_limit', 20 );Query vendor data from your database (SQL)
Run this against your database to retrieve a list of vendors. Replace DB_NAME and PREFIX with your database name and table prefix.
SELECT ID, display_name, user_login, user_email
FROM (SELECT * FROM `DB_NAME`.`PREFIX_usermeta`) T1
INNER JOIN (SELECT * FROM `DB_NAME`.`PREFIX_users`) T2 ON T1.`user_ID` = T2.`ID`
WHERE meta_key LIKE '%wcfm_vendor%'
GROUP BY user_ID