Skip to content

Instantly share code, notes, and snippets.

@digitalchild
Last active November 7, 2015 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save digitalchild/27d1f9109e6253e109a4 to your computer and use it in GitHub Desktop.
Save digitalchild/27d1f9109e6253e109a4 to your computer and use it in GitHub Desktop.
Only display the sold by if the product owner is a vendor
<?php
// Put this in your themes functions.php
// Remove all existing actions and filters
remove_action( 'woocommerce_after_shop_loop_item', array( 'WCV_Vendor_Shop', 'template_loop_sold_by'), 9, 2);
remove_filter( 'woocommerce_get_item_data', array( 'WCV_Vendor_Cart', 'sold_by' ), 10, 2 );
remove_action( 'woocommerce_product_meta_start', array( 'WCV_Vendor_Cart', 'sold_by_meta' ), 10, 2 );
remove_filter( 'woocommerce_order_product_title', array( 'WCV_Emails', 'show_vendor_in_email' ), 10, 2 );
remove_action( 'woocommerce_add_order_item_meta', array( 'WCV_Vendor_Shop', 'add_vendor_to_order_item_meta'), 10, 2 );
remove_action( 'woocommerce_after_shop_loop_item', array( $wcvendors_pro->wcvendors_pro_store_controller, 'loop_sold_by' ), 8 );
remove_action( 'woocommerce_product_meta_start', array( $wcvendors_pro->wcvendors_pro_store_controller, 'product_sold_by' ), 8 );
remove_filter( 'woocommerce_get_item_data', array( $wcvendors_pro->wcvendors_pro_store_controller, 'cart_sold_by' ), 8, 2 );
// Hook into the product loop
add_action( 'woocommerce_after_shop_loop_item', 'custom_loop_sold_by', 9 );
function custom_loop_sold_by($product_id) {
$vendor_id = WCV_Vendors::get_vendor_from_product( $product_id );
if ( WCV_Vendors::is_vendor( $vendor_id) ) {
$shop_url = '';
$store_id = WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id );
$sold_by = apply_filters( 'wcv_loop_sold_by',
array(
'product_id' => $product_id,
'vendor_id' => $vendor_id,
'shop_url' => get_the_permalink( $store_id ),
'shop_name' => WCV_Vendors::get_vendor_sold_by( $vendor_id ),
'wrapper_start' => '<small class="wcv_sold_by_in_loop">',
'wrapper_end' => '</small><br />',
'title' => __( 'Sold by: ', 'wcvendors-pro' )
)
);
echo $sold_by[ 'wrapper_start' ];
echo $sold_by[ 'title' ] .'<a href="'.$sold_by[ 'shop_url' ].'">'. $sold_by[ 'shop_name' ] .'</a>';
echo $sold_by[ 'wrapper_end' ];
}
} //custom_loop_sold_by()
// Hook into the cart data
add_filter( 'woocommerce_get_item_data', 'custom_cart_sold_by', 10, 2 );
function custom_cart_sold_by( $values, $cart_item ) {
$vendor_id = $cart_item[ 'data' ]->post->post_author;
if ( WCV_Vendors::is_vendor( $vendor_id ) ) {
$is_vendor = WCV_Vendors::is_vendor( $vendor_id );
$store_id = $is_vendor ? WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id ) : 0;
$shop_url = ( $store_id != 0 ) ? get_the_permalink( $store_id ) : '';
$sold_by = '';
if ( $is_vendor ) {
$sold_by = sprintf( '<a href="%s" target="_TOP">%s</a>', $shop_url, WCV_Vendors::get_vendor_sold_by( $vendor_id ) );
} else {
$sold_by = get_bloginfo( 'name' );
}
$values[] = array(
'name' => apply_filters( 'wcv_cart_sold_by', __( 'Sold by', 'wcvendors-pro' ) ),
'display' => $sold_by,
);
return $values;
}
return $values;
} // custom_cart_sold_by()
// Hook into the single product meta template
add_action( 'woocommerce_product_meta_start', 'custom_product_sold_by_meta', 10, 2 );
function custom_product_sold_by_meta() {
$vendor_id = get_the_author_meta( 'ID' );
if ( WCV_Vendors::is_vendor( $vendor_id ) ) {
$shop_url = '';
$store_id = WCVendors_Pro_Vendor_Controller::get_vendor_store_id( $vendor_id );
$sold_by = array(
'vendor_id' => $vendor_id,
'shop_url' => get_the_permalink( $store_id ),
'shop_name' => WCV_Vendors::get_vendor_sold_by( $vendor_id ),
'wrapper_start' => '<small class="wcv_sold_by_in_loop">',
'wrapper_end' => '</small><br />',
'title' => __( 'Sold by: ', 'wcvendors-pro' )
);
echo $sold_by[ 'wrapper_start' ];
echo $sold_by[ 'title' ] .'<a href="'.$sold_by[ 'shop_url' ].'">'. $sold_by[ 'shop_name' ] .'</a>';
echo $sold_by[ 'wrapper_end' ];
}
} // custom_product_sold_by_meta()
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment