我正在建立一个使用WooCommerce和他们的愿望清单插件的大型网站。我想要做的是在边栏上添加一个小部件,在登录时显示客户的愿望清单。目前似乎没有任何可用的东西,所以我认为我要构建我自己......
在实际的wishlists插件文件中,我发现了shortcodes-init.php,我认为它可以作为创建窗口小部件的起点,因为它应该检查该人是否已登录,如果是,则显示他们的愿望清单。我当时所要做的就是创建实际的小部件。
我创建了一个新文件,我添加到wishlists插件文件夹(因为我知道我的自定义文件不会被覆盖),设置小部件并添加在短代码中找到的代码以显示愿望清单。
这就是我所拥有的:
// 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' );
我遇到的问题是,这不会查看客户是否已登录并实际中断页面。这是代码似乎是问题。
//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 );
}
有谁知道如何解决这个或其他方式来获取客户的愿望清单?
谢谢:)