Wordpress WooCommerce
——— qty field on category
https://shopplugins.com/add-a-quantity-field-to-the-woocommerce-archive-pages/
——- если страница категории продукта не видит темплейтов, своих лейаутов
Включи объявление что тема поддерживает WooCommerce
WooCommerce -> Status -> WooCommerce support: - должно быть включено, быть зелёная галочка
add_action( 'after_setup_theme', 'setup_woocommerce_support' );
function setup_woocommerce_support()
{
add_theme_support('woocommerce');
}
——— price sale price save
%
<?php
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
echo $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
?>
——— after add to cart add product
add_action('add_to_cart_redirect', 'resolve_dupes_add_to_cart_redirect');
function resolve_dupes_add_to_cart_redirect($url = false) {
// If another plugin beats us to the punch, let them have their way with the URL
if(!empty($url)) { return $url; }
// Redirect back to the original page, without the 'add-to-cart' parameter.
// We add the `get_bloginfo` part so it saves a redirect on https:// sites.
return get_bloginfo('wpurl').add_query_arg(array(), remove_query_arg('add-to-cart'));
}
——— color swatches
https://wordpress.org/plugins/variation-swatches-for-woocommerce/
https://woocommerce.com/products/variation-swatches-and-photos
——— layered navigation product filters
https://ru.wordpress.org/plugins/woocommerce-products-filter/
——— product variations info
<?php
$available_variations = $product->get_available_variations();
foreach ($available_variations as $key => $value):
$size_attribute = get_term_by( 'slug', $value['attributes']['attribute_pa_size'], 'pa_size' );
?>
<div class="product-options _<?php echo $value['attributes']['attribute_pa_size']; ?>-size">
<span class="size"><?php echo $size_attribute->name; ?></span>
<?php echo $value['price_html']; ?>
</div>
<?php endforeach; ?>
——— custom product gallery
global $post, $product;
$attachment_ids = $product->get_gallery_image_ids();
?>
<div class="product-gallery">
<?php if( $attachment_ids && has_post_thumbnail() ): ?>
<?php
foreach ( $attachment_ids as $attachment_id ):
$full_size_image = wp_get_attachment_image_src( $attachment_id, '377x694' );
?>
<div class="slide">
<img src="<?php echo $full_size_image[0]; ?>" title="<?php echo get_post_field( 'post_title', $attachment_id ); ?>" alt="<?php echo get_post_field( 'post_title', $attachment_id ); ?>">
</div>
<?php endforeach; ?>
<?php else: ?>
<div class="big-image-gallery">
<?php if ( has_post_thumbnail() ): ?>
<?php $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), '377x694' ); ?>
<img src="<?php echo $image_attributes[0]; ?>" title="<?php the_title(); ?>" alt="<?php e the_title(); ?>">
<?php else: ?>
<?php
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
——— message block add button
https://businessbloomer.com/woocommerce-remove-product-successfully-added-cart-message/
——— woocommerce how programmatically change shipping method
WC()->session->set('chosen_shipping_methods', array( 'free_shipping' ) );
function before_shipping( $checkout ) {
global $woocommerce;
$hide_shipping = 0;
$items = $woocommerce->cart->get_cart();
foreach( $items as $item => $values )
{
if( !empty( get_field( 'no_checkout', $values['data']->post->ID ) ) ){
$hide_shipping = 1;
}
}
if( $hide_shipping ){
WC()->session->set('chosen_shipping_methods', array( 'free_shipping' ) );
}
}
add_action( 'woocommerce_shipping_init', 'before_shipping', 10 );
——— after add to cart message remove message on product
add_action('woocommerce_add_to_cart','theme_add_to_cart_flag',999,2);
function theme_add_to_cart_flag($cart_item_key, $product_id){
if (!session_id())
session_start();
$_SESSION['add_to_cart-' . $product_id] = 'added';
}
add_action('woocommerce_after_variations_form','theme_add_to_cart_message');
add_action('woocommerce_after_simple_form','theme_add_to_cart_message');
function theme_add_to_cart_message(){
global $product;
if (!session_id())
session_start();
$nonce = 'add_to_cart-' . $product->id;
if (isset($_SESSION[$nonce]) && $_SESSION[$nonce] == 'added' ){
unset($_SESSION[$nonce]);
if( $custom_message = get_field( 'custom_message', 'option' ) ){
echo '<p class="custom-message addtocart-custom-message">'.$custom_message.'</p>';
}
}
}
add_filter( 'wc_add_to_cart_message_html', '__return_null()' );
+ должен быть экшен после кнопки адд ту карт или в другом месте где хочешь вывести кастомное сообщение <?php do_action( 'woocommerce_after_simple_form' ); ?>
—— currency symbol
https://www.sellwithwp.com/pricing-remove-currency-symbol/#woocommerce
// Remove all currency symbols
function sww_remove_wc_currency_symbols( $currency_symbol, $currency ) {
$currency_symbol = '';
return $currency_symbol;
}
add_filter('woocommerce_currency_symbol', 'sww_remove_wc_currency_symbols', 10, 2);
--------------
// Change the US currency symbol
function sww_change_wc_currency_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'USD': $currency_symbol = 'USD'; break;
// Can use this for any currency symbol
}
return $currency_symbol;
}
add_filter('woocommerce_currency_symbol', 'sww_change_wc_currency_symbol', 10, 2);
--------------
// Removes currency symbol from the shop price display
function sww_remove_edd_usd_currency_symbol( $output, $currency, $price ) {
$output = $price;
return $output;
}
// This will apply to USD, but the usd in this filter can be replaced with your currency symbol
add_filter( 'edd_usd_currency_filter_before', 'sww_remove_edd_usd_currency_symbol', 10, 3 );
--------------
// Changes currency symbol from the shop price display
function sww_change_edd_usd_currency_symbol( $output, $currency, $price ) {
$output = 'USD ' . $price;
return $output;
}
// This will apply to USD, but the usd in this filter can be replaced with your currency symbol
add_filter( 'edd_usd_currency_filter_before', 'sww_change_edd_usd_currency_symbol', 10, 3 );
--------------
// Changes currency symbol to USD
function sww_change_jigoshop_currency( $symbol ) {
return 'USD ';
}
add_filter( 'jigoshop_currency_symbol', 'sww_change_jigoshop_currency' );
// Removes the currency symbol from the shop
function sww_change_wpec_currency_code( $args ) {
$args['display_currency_symbol'] = false;
return $args;
}
add_filter( 'wpsc_toggle_display_currency_code', 'sww_change_wpec_currency_code' );
——— widgets
https://www.skyverge.com/blog/overriddin-woocommerce-widgets/
—— view all
add_filter('loop_shop_per_page', 'wg_view_all_products');
function wg_view_all_products(){
if($_GET['view'] === 'all'){
return '9999';
}else{
return '15';
}
}
<?php if ( is_paged() and $_GET['view'] != 'all' ) : ?>
<a href="../../?view=all"><?php _e( 'Vis alle', 'ledanafurniture' ); ?></a>
<?php elseif( !is_paged() and $_GET['view'] != 'all' ): ?>
<a href="?view=all"><?php _e( 'Vis alle', 'ledanafurniture' ); ?></a>
<?php endif; ?>
<?php if( $_GET['view'] === 'all' ): ?>
<a href="."><?php _e( 'View with pagination', 'ledanafurniture' ); ?></a>
<?php endif; ?>
——————— обвертки
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<section id="main">';
}
function my_theme_wrapper_end() {
echo '</section>';
}
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
—————————
—— custom checkout fields add checkout fields woocommerce fields
http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{
$fields['billing']['security_number'] = array(
'label' => __('Social Security Number', 'woocommerce'),
'placeholder' => _x('Last 4 Digits of Social Security Number', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
add_action( 'woocommerce_after_checkout_billing_form', 'eosmobile_checkout_field' );
function eosmobile_checkout_field( $checkout )
{
echo '<div class="date-of-birth-holder"><h4 class="date-of-birth">' . __('Date of Birth') . '</h4>';
woocommerce_form_field( 'date_of_birt_day', array(
'type' => 'select',
'required' => true,
'class' => array('date-of-birth-select day-select'),
'label' => __('Day'),
'options' => array( '' => '', '01' => '01', '02' => '02' , '03' => '03' , '04' => '04' , '05' => '05' , '06' => '06' , '07' => '07' , '08' => '08' , '09' => '09' , '10' => '10' , '11' => '11' , '12' => '12' , '13' => '13' , '14' => '14' , '15' => '15' , '16' => '16' , '17' => '17' , '18' => '19' , '20' => '20' , '21' => '21' , '22' => '22' , '23' => '23' , '24' => '24' , '25' => '25' , '26' => '26' , '27' => '27' , '28' => '28' , '29' => '29' , '30' => '30' , '31' => '31'
)
), $checkout->get_value( 'date_of_birt_day' ));
woocommerce_form_field( 'date_of_birt_month', array(
'type' => 'select',
'required' => true,
'class' => array('date-of-birth-select month-select'),
'label' => __('Month'),
'options' => array( '' => '', '01' => '01', '02' => '02' , '03' => '03' , '04' => '04' , '05' => '05' , '06' => '06' , '07' => '07' , '08' => '08' , '09' => '09' , '10' => '10' , '11' => '11' , '12' => '12'
)
), $checkout->get_value( 'date_of_birt_month' ));
woocommerce_form_field( 'date_of_birt_year', array(
'type' => 'text',
'required' => true,
'class' => array('date-of-birth-select year-select'),
'label' => __('Year'),
'placeholder' => __('Year')
), $checkout->get_value( 'date_of_birt_year' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_meta', 'eosmobile_checkout_field_update_order_meta' );
function eosmobile_checkout_field_update_order_meta( $order_id )
{
if ( ! empty( $_POST['security_number'] ) ) {
update_post_meta( $order_id, 'Social Security Number', sanitize_text_field( $_POST['security_number'] ) );
}
if ( ! empty( $_POST['date_of_birt_day'] ) ) {
update_post_meta( $order_id, 'Date of Birth Day', sanitize_text_field( $_POST['date_of_birt_day'] ) );
}
if ( ! empty( $_POST['date_of_birt_month'] ) ) {
update_post_meta( $order_id, 'Date of Birth Month', sanitize_text_field( $_POST['date_of_birt_month'] ) );
}
if ( ! empty( $_POST['date_of_birt_year'] ) ) {
update_post_meta( $order_id, 'Date of Birth Year', sanitize_text_field( $_POST['date_of_birt_year'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'eosmobile_checkout_field_display_admin_order_meta', 10, 1 );
function eosmobile_checkout_field_display_admin_order_meta($order)
{
echo '<p><strong>'.__('Social Security Number').':</strong> ' . get_post_meta( $order->id, 'Social Security Number', true ) . '</p>';
echo '<p><strong>'.__('Date of Birth').':</strong> ' . get_post_meta( $order->id, 'Date of Birth Day', true ) .'/'. get_post_meta( $order->id, 'Date of Birth Month', true ) .'/'. get_post_meta( $order->id, 'Date of Birth Year', true ) .'</p>';
}
add_action('woocommerce_checkout_process', 'eosmobile_checkout_field_process');
function eosmobile_checkout_field_process()
{
if ( ! $_POST['security_number'] ){ wc_add_notice( __( 'Please enter your Social Security Number.' ), 'error' ); }
if ( ! $_POST['date_of_birt_day'] || ! $_POST['date_of_birt_month'] || ! $_POST['date_of_birt_year'] ){ wc_add_notice( __( 'Please enter your Date of Birth.' ), 'error' ); }
}
add_filter('woocommerce_email_order_meta_keys', 'eosmobile_checkout_field_order_meta_keys');
function eosmobile_checkout_field_order_meta_keys( $keys )
{
$keys['Social Security Number'] = 'Social Security Number';
$keys['Date of Birth - Day'] = 'Date of Birth Day';
$keys['Date of Birth - Month'] = 'Date of Birth Month';
$keys['Date of Birth - Year'] = 'Date of Birth Year';
return $keys;
}
----- shopping cart
<?php global $woocommerce; ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
----- price get product price
<?php
$product = new WC_Product(get_the_ID());
?>
<?php echo $product->get_price_html(); ?>
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
return 'Was:' . str_replace( '<ins>', ' Now:<ins>', $price );
}
----- filters
img-10
img-10-medium
img-10-medium2x
img-10-small
img-10-small2x
add_filter('woocommerce_get_country_locale_base', 'custom_filter_function');
function custom_filter_function($fields){
$fields['address_1']['label'] = __('Street Address', 'base');
$fields['address_1']['placeholder'] = '';
$fields['address_2']['placeholder'] = '';
$fields['city']['label'] = __('City', 'base');
$fields['city']['placeholder'] = '';
$fields['state']['label'] = __('State', 'base');
$fields['state']['placeholder'] = '';
$fields['postcode']['label'] = __('Zip Code', 'base');
$fields['postcode']['placeholder'] = '';
return $fields;
}
add_filter('woocommerce_free_price_html', 'custom_free_price_html');
function custom_free_price_html(){
return __('free', 'base');
}
add_filter('woocommerce_checkout_fields', 'remove_checkout_fields');
function remove_checkout_fields($fields){
unset($fields['billing']['billing_phone']);
unset($fields['billing']['billing_last_name']);
unset($fields['shipping']['shipping_last_name']);
return $fields;
}
———————— woocommerce custom thank you page customize thank you page
https://nicolamustone.com/2015/01/21/customize-the-thank-you-page-in-woocommerce/
——————— page ids
// Get The Page ID You Need
get_option( 'woocommerce_shop_page_id' );
get_option( 'woocommerce_cart_page_id' );
get_option( 'woocommerce_checkout_page_id' );
get_option( 'woocommerce_pay_page_id' );
get_option( 'woocommerce_thanks_page_id' );
get_option( 'woocommerce_myaccount_page_id' );
get_option( 'woocommerce_edit_address_page_id' );
get_option( 'woocommerce_view_order_page_id' );
get_option( 'woocommerce_terms_page_id' );
// An Example
function get_shop_featured_image() {
if( is_shop() ) {
$shop = get_option( 'woocommerce_shop_page_id' );
if( has_post_thumbnail( $shop ) ) {
echo get_the_post_thumbnail( $shop );
}
}
}
// In You Templage File Place The Following
// Line To Call The WooCommerce Shop Featured Image
// Now You're Cooking With Gas - BOOM!!
<?php get_shop_featured_image(); ?>
----- menu order hierarchical product
function modify_products() {
if ( post_type_exists( 'product' ) ) {
/* Give products hierarchy (for house plans) */
global $wp_post_types, $wp_rewrite;
$wp_post_types['product']->hierarchical = true;
$args = $wp_post_types['product'];
$wp_rewrite->add_rewrite_tag("%product%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=product&name=");
add_post_type_support('product','page-attributes');
}
}
add_action( 'init', 'modify_products', 1 );