lundi 2 novembre 2015

Creating a wishlist widget

I am building quite a large website using WooCommerce and their wishlists addon. What I'd like to do is add a widget to the sidebar that shows the customers wishlists when they are logged in. There doesn't seem to be anything available at the moment so I thought I'd build it myself....

In the actual wishlists plugin files I found shortcodes-init.php which I thought would be good as a starting point to create the widget, as it should check if the person is logged in and if so, display their wishlists. All I then had to do was create the actual widget.

I created a new file that I added to the wishlists plugin folder (because I know my custom file won't get overwritten), set up the widget and added the code found in the shortcodes to display wishlists.

This is what I have:

// Creating the widget 
class wl_widget extends WP_Widget {

    function __construct() {

        parent::__construct(
        // Base ID of your widget
        'wl_widget', 

        // Widget name will appear in UI
        __('Boards Widget', 'wl_widget_domain'), 

        // Widget description
        array( 'description' => __( 'Widget to add show customers boards', 'wl_widget_domain' ), ) );

    }

    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {

        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );

        // before and after widget arguments are defined by themes
        echo $args['before_widget'];

        if ( $title )
            echo $before_title . $title . $after_title; 

        //We need to force WooCommerce to set the session cookie
        if ( !is_admin() && !WC_Wishlist_Compatibility::WC()->session->has_session() ) {
            WC_Wishlist_Compatibility::WC()->session->set_customer_session_cookie( true );
        }


        ob_start();

        if (is_user_logged_in() || (WC_Wishlists_Settings::get_setting('wc_wishlist_guest_enabled', 'enabled') == 'enabled')) {
            //woocommerce_wishlists_get_template('my-lists.php');
            echo "loggedin";
        } else {
            //woocommerce_wishlists_get_template('guest-disabled.php');
            echo "loggedout";
        }

        return ob_get_clean();


        echo $args['after_widget'];

    }

    // Widget Backend 
    public function form( $instance ) {

        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'New title', 'wl_widget_domain' );
        }

        // Widget admin form
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>

        <p>
        This widget automatically displays boards when the person is logged in so there is no further settings needed.
        </p>

        <?php 
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {

    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }

} // Class btn_widget ends here

// Register and load the widget
function wl_load_widget() {
    register_widget( 'wl_widget' );
}
add_action( 'widgets_init', 'wl_load_widget' );

The problem I have is that this does not see if the customer is logged in or not and actually breaks the page. This is the code that seems to be the problem.

//We need to force WooCommerce to set the session cookie
        if ( !is_admin() && !WC_Wishlist_Compatibility::WC()->session->has_session() ) {
            WC_Wishlist_Compatibility::WC()->session->set_customer_session_cookie( true );
        }

Does anyone know how to fix this or another way to get the customers wishlists?

Thanks :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire