Magazine Ebusiness

5 magnifiques astuces WooCommerce #2

Publié le 05 juin 2015 par Amerigeau @arnaudmerigeau
5 juin 2015

Deuxième partie pour la série des astuces d’intégration pour WooCommerce. Pour rappel, WooCommerce est basé sur WordPress.

5 Best WooCommerce snippets for WordPress, part 2

Les bouts de code doivent être placés dans le fichier functions.php de votre thème pour fonctionner.

C’est parti donc pour 5 bouts de code à utiliser sur WooCommerce :

1 – Afficher le shortcode des produits en solde

function woocommerce_sale_products( $atts ) {

    global $woocommerce_loop;

    extract(shortcode_atts(array(
        'per_page'  => '12',
        'columns'   => '4',
        'orderby' => 'date',
        'order' => 'desc'
    ), $atts));

    $woocommerce_loop['columns'] = $columns;

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => $per_page,
        'orderby' => $orderby,
        'order' => $order,
        'meta_query' => array(
            array(
                'key' => '_visibility',
                'value' => array('catalog', 'visible'),
                'compare' => 'IN'
            ),
            array(
                'key' => '_sale_price',
                'value' =>  0,
                'compare'   => '>',
                'type'      => 'NUMERIC'
            )
        )
    );
    query_posts($args);
    ob_start();
    woocommerce_get_template_part( 'loop', 'shop' );
    wp_reset_query();

    return ob_get_clean();
}

add_shortcode('sale_products', 'woocommerce_sale_products');

2 – Ajouter un champ personnalisé à la page dédition de l’adresse

// add fields to edit address page
function woo_add_edit_address_fields( $fields ) {

	$new_fields = array(
				'date_of_birth'     => array(
				'label'             => __( 'Date of birth', 'woocommerce' ),
				'required'          => false,
				'class'             => array( 'form-row' ),
			),
		);

	$fields = array_merge( $fields, $new_fields );

    return $fields;

}

add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' );

3 – Retourné les ids des produits phares

function woo_get_featured_product_ids() {
	// Load from cache
	$featured_product_ids = get_transient( 'wc_featured_products' );

	// Valid cache found
	if ( false !== $featured_product_ids )
		return $featured_product_ids;

	$featured = get_posts( array(
		'post_type'      => array( 'product', 'product_variation' ),
		'posts_per_page' => -1,
		'post_status'    => 'publish',
		'meta_query'     => array(
			array(
				'key' 		=> '_visibility',
				'value' 	=> array('catalog', 'visible'),
				'compare' 	=> 'IN'
			),
			array(
				'key' 	=> '_featured',
				'value' => 'yes'
			)
		),
		'fields' => 'id=>parent'
	) );

	$product_ids = array_keys( $featured );
	$parent_ids  = array_values( $featured );
	$featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );

	set_transient( 'wc_featured_products', $featured_product_ids );

	return $featured_product_ids;
}

4 – Décoder le nom du destinaire des e-mails Woocommerce

function woo_custom_wp_mail_from_name() {
        global $woocommerce;
        return html_entity_decode( get_option( 'woocommerce_email_from_name' ) );
}
add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );

function woo_custom_wp_mail_from() {
        global $woocommerce;
        return html_entity_decode( get_option( 'woocommerce_email_from' ) );
}
add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );

5 – Changer le « from » de l’adresse e-mail

function woo_custom_wp_mail_from() {
        global $woocommerce;
        return html_entity_decode( '[email protected]' );
}
add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );</pre>
<pre>

A suivre…

Tags:

Retour à La Une de Logo Paperblog

A propos de l’auteur


Amerigeau 144 partages Voir son profil
Voir son blog

l'auteur n'a pas encore renseigné son compte

Dossiers Paperblog