Come reperire i dati di un prodotto utilizzando le funzioni standard di Woocommerce?

// Get $product object from product ID
 
$product = wc_get_product( $product_id );

// Get Product ID
 
$product->get_id(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly")
 
// Get Product General Info
 
$product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );
 
// Get Product Prices
 
$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();
 
// Get Product Tax, Shipping & Stock
 
$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();
 
// Get Product Dimensions
 
$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();
 
// Get Linked Products
 
$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();
 
// Get Product Variations
 
$product->get_attributes();
$product->get_default_attributes();
 
// Get Product Taxonomies
 
$product->get_categories();
$product->get_category_ids();
$product->get_tag_ids();
 
// Get Product Downloads
 
$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();
 
// Get Product Images
 
$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();
 
// Get Product Reviews
 
$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();

Come reperire i dati di un ordine usando funzioni standard di Woocommerce?

Elendo delle funzione di Woocommerce per reperire i dati di un ordine.

// Get an instance of the WC_Order object (same as before)
$order = wc_get_order( $order_id );

// Get Order ID
$order->get_id();
 
// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
 
// Get Order Items
$order->get_items();
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item();
$order->get_item_count();
$order->get_item_subtotal();
$order->get_item_tax();
$order->get_item_total();
$order->get_downloadable_items();
 
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
 
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
 
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
 
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
 
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
 
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
 
// Get Order Status
$order->get_status();
 
// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

FacetWp: Add a cutom search engine to search facet

//Add a custom Search Engine to search facet
add_filter('facetwp_facet_search_engines', function( $engines ) {
	$engines['custom_search_engine'] = 'Custom Search Engine';
	return $engines;
}, 10, 2);
	


//Modify the query search args for the custom search engine
function silmax_search_query_args( $search_args, $params ) {
	if($params['facet']['search_engine'] == 'custom_search_engine'){
		$search_args = [
			'posts_per_page' => -1,
			'fields' => 'ids',
		];
		return $search_args;
	}
}
add_filter( 'facetwp_search_query_args', 'silmax_search_query_args', 10, 2 );

Order Facetwp fselect by data-value with jQuery

(function($) {
  $(document).on('facetwp-loaded', function(){
    var $sorted_items,
    getSorted = function(selector, attrName) {
         return $(
	   $(selector).toArray().sort(function(a, b){		
	     // var aVal = parseInt(a.getAttribute(attrName));
            // var  bVal = parseInt(b.getAttribute(attrName));
									
	     var aVal = parseFloat($(a).data(attrName));
	     var bVal = parseFloat($(b).data(attrName));
									
	     return aVal - bVal;
	   })
        );
     };
				
     $('.facetwp-facet-slug_facet .fs-options').html(getSorted('.facetwp-facet-slug_facet .fs-options .fs-option', 'value'));
})(jQuery);

Stampare la categoria prodotto di woocommerce come classe nel body quando visiti un prodotto singolo

Con questa funzione puoi stampare tra le classi del body la categoria del prodotto singolo di woocommerce. Per funzionare basta copiare e incollare la funziona nel file functions.php

function sg_wooc_custom_taxonomy_in_body_class( $classes ){
  if( is_singular( 'product' ) ) {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'term-' . $custom_term->slug;
      }
    }
  }
  return $classes;
}

add_filter( 'body_class', 'sg_wooc_custom_taxonomy_in_body_class' );

Stampare la categoria come classe nel body quando visiti un post singolo

Una funzione semplice che aiuta a stampare tra le classi del body la categoria del post singolo. Per funzionare basta copiare e incollare nel file functions.php

function sg_add_category_to_single($classes) {
    if (is_single() ) {
      global $post;
      foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
      }
    }
    // return the $classes array
    return $classes;
}

add_filter('body_class','sg_add_category_to_single');

Deregistrare i custom post type di un Tema di WordPress

Spesso quando si lavora con dei temi pre-confezionati per sviluppare un nuovo sito web basato su wordpress, ci troviamo tanti custom post type inutili per il nostro progetto. Per deregistrarli potete usare questo piccolo codice che va inserito nel file functions.php del tema figlio.


function studio42_unregister_cpt() {
    global $wp_post_types;
    foreach( array( 'nome-singolo-post-type-1', 'nome-singolo-post-type-2', 'nome-singolo-post-type-3' ) as $post_type ) {
        if ( isset( $wp_post_types[ $post_type ] ) ) {
            unset( $wp_post_types[ $post_type ] );
        }
    }
}
add_action( 'init', 'studio42_unregister_cpt', 20 );

Woocommerce aggiungere campo Codice Fiscale con validazione formale

Un metodo per inserire nella pagina di checkout il campo obbligatorio del Codice Fiscale con un controllo puramente formale via regexp del codice fiscale inserito.

function wooc_field_cf($fields) {

$fields['billing']['billing_cf'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-last'),
'clear' => false,
'default' => (get_current_user_id() > 0) ? get_user_meta(get_current_user_id(), 'billing_cf', true) : ''
);

return $fields;

}

add_filter('woocommerce_checkout_fields', 'wooc_field_cf');

function wooc_checkout_field_process() {
// Check if set, if its not set add an error.

if ( $_POST['billing_cf'] && preg_match('/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i', $_POST['billing_cf'])) {
//vai avanti
}elseif(!$_POST['billing_cf'] && $_POST['billing_cf'] == '' ){
wc_add_notice( __( 'Codice Fiscale รจ un campo obbligatorio.'), 'error' );
}elseif(!preg_match('/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/i', $_POST['billing_cf'])){
wc_add_notice( __( 'Codice Fiscale non valido.'), 'error' );
}

}

add_action('woocommerce_checkout_process', 'wooc_checkout_field_process');