Skip to content

Instantly share code, notes, and snippets.

@ApoGouv
Last active April 5, 2019 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ApoGouv/3630246a2a23560cda05cff586876da2 to your computer and use it in GitHub Desktop.
Save ApoGouv/3630246a2a23560cda05cff586876da2 to your computer and use it in GitHub Desktop.
Woocommerce Functions for mods
<?php
/************************************************************************
* Changes the redirect URL for the Return To Shop button in the cart.
***********************************************************************/
function cds_wc_empty_cart_redirect_url() {
return 'https://www.example.gr/some/custom/url';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'cds_wc_empty_cart_redirect_url', 20 );
<?php
/************************************************************************
* Change classes of Billing field on Checkout
***********************************************************************/
function cds_custom_change_classes_wc_checkout_billing_fields( $fields ) {
$fields['billing']['billing_first_name']['class'] = array (
0 => 'form-row-first',
);
$fields['billing']['billing_last_name']['class'] = array (
0 => 'form-row-last',
);
$fields['billing']['billing_country']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
);
$fields['billing']['billing_address_1']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
);
$fields['billing']['billing_address_2']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
);
$fields['billing']['billing_city']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
);
$fields['billing']['billing_state']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
);
$fields['billing']['billing_postcode']['class'] = array (
0 => 'form-row-wide',
1 => 'address-field',
);
$fields['billing']['billing_phone']['class'] = array (
0 => 'form-row-wide',
);
$fields['billing']['billing_email']['class'] = array (
0 => 'form-row-wide',
);
highlight_string("<?php\n\$fields =\n" . var_export($fields, true) . ";\n");
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'cds_custom_change_classes_wc_checkout_billing_fields' );
/************************************************************************
* Change Order of Billing field on Checkout
* DEFAULT Fields Order
* --> BILLING <--
* billing_first_name 10
* billing_last_name 20
* billing_company 30
* billing_country 40
* billing_address_1 50
* billing_address_2 60
* billing_city 70
* billing_state 80
* billing_postcode 90
* billing_phone 100
* billing_email 110
*
* --> SHIPPING <--
* shipping_first_name 10
* shipping_last_name 20
* shipping_company 30
* shipping_country 40
* shipping_address_1 50
* shipping_address_2 60
* shipping_city 70
* shipping_state 80
* shipping_postcode 90
*
* --> ACCOUNT <--
* account_password –
*
* --> ORDER <--
* order_comments –
***********************************************************************/
function cds_wc_checkout_billing_fields_change_field_order( $checkout_fields ){
// Re-order the Billing Fields
$checkout_fields['billing']['billing_first_name']['priority'] = 10;
$checkout_fields['billing']['billing_last_name']['priority'] = 20;
$checkout_fields['billing']['billing_phone']['priority'] = 30;
$checkout_fields['billing']['billing_email']['priority'] = 40;
$checkout_fields['billing']['billing_country']['priority'] = 50;
$checkout_fields['billing']['billing_state']['priority'] = 60;
$checkout_fields['billing']['billing_address_1']['priority'] = 70;
$checkout_fields['billing']['billing_address_2']['priority'] = 80;
$checkout_fields['billing']['billing_city']['priority'] = 90;
$checkout_fields['billing']['billing_postcode']['priority'] = 100;
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields' , 'cds_wc_checkout_billing_fields_change_field_order', 10 );
/************************************************************************
* Change Order of Locale field on Checkout
* DEFAULT Locale Fields Order
* first_name 10
* last_name 20
* country 40
* address_1 50
* address_2 60
* city 70
* state 80
* postcode 90
***********************************************************************/
function cds_override_default_wc_checkout_locale_fields( $fields ) {
$fields['first_name']['priority'] = 10;
$fields['last_name']['priority'] = 20;
$fields['country']['priority'] = 50;
$fields['state']['priority'] = 60;
$fields['address_1']['priority'] = 70;
$fields['address_2']['priority'] = 80;
$fields['city']['priority'] = 90;
$fields['postcode']['priority'] = 100;
// highlight_string("<?php\n\woocommerce_default_address_fields =\n" . var_export($fields, true) . ";\n");
return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'cds_override_default_wc_checkout_locale_fields' );
<?php
/************************************************************************
* Remove Woocommerce Breadcrumbs
***********************************************************************/
remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
/************************************************************************
* Disable Woocommerce Sidebar
***********************************************************************/
function cds_disable_woocommerce_sidebar() {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
}
add_action('init', 'cds_disable_woocommerce_sidebar');
<?php
/************************************************************************
* Remove related products output
***********************************************************************/
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
/************************************************************************
* Remove Woocommerce Single Product "Additional Information" Tab
***********************************************************************/
function multi_remove_product_tabs( $tabs ) {
unset( $tabs['description'] );
unset( $tabs['additional_information'] );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'multi_remove_product_tabs', 98 );
/************************************************************************
* Custom variable product price
***********************************************************************/
function cds_custom_min_max_variable_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) ) {
$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $product );
} else {
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
$min_reg_price = current( $prices['regular_price'] );
$max_reg_price = end( $prices['regular_price'] );
if ( $min_price !== $max_price ) {
// $price = wc_format_price_range( $min_price, $max_price );
$price = sprintf( _x( '%1$s &ndash; %2$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $min_price ) ? wc_price( $min_price ) : $min_price, is_numeric( $max_price ) ? wc_price( $max_price ) : $max_price );
// Show pice of: Double Occupancy + Full Price
// $price = sprintf( _x( 'Full Price: %1$s', 'Price Max', 'woocommerce' ), is_numeric( $max_price ) ? wc_price( $max_price ) : $max_price );
} elseif ( $this->is_on_sale() && $min_reg_price === $max_reg_price ) {
// $price = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
$price = '<del>' . ( is_numeric( $min_reg_price ) ? wc_price( $min_reg_price ) : $min_reg_price ) . '</del> <ins>' . ( is_numeric( $max_reg_price ) ? wc_price( $max_reg_price ) : $max_reg_price ) . '</ins>';
} else {
$price = wc_price( $min_price );
}
}
return $price;
}
add_filter( 'woocommerce_variable_price_html', 'cds_custom_min_max_variable_price_html', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment