Sekcia krátkych ale užitočných snippetov pre Woocomerce a WordPress.
Upraví zobrazovanie dostupnosti pre všetky produkty ktoré začínajú kódom XXX
/**
* Change text if product sku start XXX
*/
add_filter( 'woocommerce_get_availability', 'filter_wc_get_availability', 10, 2);
function filter_wc_get_availability( $availability, $product ) {
// In Stock
if ( $product->is_on_backorder() ){
$product_sku = $product->get_sku();
$kod = substr($product_sku,0,3);
if ( $kod =='XXX' ) {
// $stock_quantity = $product->get_stock_quantity();
// if ( $stock_quantity<=0 ) {
$availability['availability'] = 'Tu sa zobrazí nastavená dostupnosť';
// }
}
}
return $availability;
}
Zobrazí ID produktov a EAN alebo čokoľvek čo nastavíme iba vybranému používateľovi alebo administrátorovi:
// zobrazi mne ID produktov
add_action( 'woocommerce_single_product_summary', 'zobraz_id_produktu');
function zobraz_id_produktu(){
// if (current_user_can( 'administrator' ) ){
global $current_user;
if ( $current_user->user_login == 'Meno používateľa' ){
global $product;
echo ' ID produktu: ' . $product->get_ID();
echo ' <br> EAN: ' . get_post_meta( $product->get_ID(), 'ceske_sluzby_hodnota_ean', true );
}
// }
}
Vytvorenie vlastnej kolónky pri produktoch
//pridanie polia produktu
if ( ! function_exists( 'create_custom_field' ) ) :
function create_custom_field() {
$args = array(
'id' => 'custom_field',
'label' => __( 'Názov' ),
'class' => 'cfwc-custom-field',
'desc_tip' => true,
'description' => __( 'Názov'),
);
woocommerce_wp_text_input( $args );
}
endif;
add_action( 'woocommerce_product_options_inventory_product_data', 'create_custom_field' );
////ulozenie hodnoty
if ( ! function_exists( 'save_custom_field' ) ) :
function save_custom_field( $post_id ) {
$product = wc_get_product( $post_id );
$title = isset( $_POST['custom_field'] ) ? $_POST['custom_field'] : '';
$product->update_meta_data( 'custom_field', sanitize_text_field( $title ) );
$product->save();
}
endif;
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
Odstráni vybrané polia z administrácie produktu a z karty špecifikácie na frontende na stránke produktu.
function remove_tab($tabs){
if(!current_user_can('administrator') ){ // not enable function if user is admin
//unset($tabs['general']); // it is to remove general tab
unset($tabs['inventory']); // it is to remove inventory tab
//unset($tabs['advanced']); // it is to remove advanced tab
//unset($tabs['linked_product']); // it is to remove linked_product tab
//unset($tabs['attribute']); // it is to remove attribute tab
//unset($tabs['variations']); // it is to remove variations tab
}
return($tabs);
}
add_filter('woocommerce_product_data_tabs', 'remove_tab', 10, 1);
// add_filter( 'wc_product_enable_dimensions_display', '__return_false' ); // remove
specification
add_filter( 'woocommerce_product_get_weight' , '__return_false' ); // remove only weight from spcification
Skrytie druhej platby ak je dostupná doprava zdarma
// function pppcreative_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {
// // Only unset rates if free_shipping is available
// if ( isset( $rates['free_shipping:2'] ) ) { // zvolte podla hodnoty value v inpute doprava zdarma
// unset( $rates['flat_rate:1'] ); // zvolte podla hodnoty value v inpute ktorý chcete skryt
// }
// return $rates;
// }
Na stránke sa pracuje…