我有一个 Woocommerce 订阅网站,所以我的大部分客户角色都是 Subscriber。现在,我想向潜在客户提供一些免费的小型试用,并向他们提供一些信用,他们将成为 Customer 角色,因为我们还没有让他们注册订阅。
问题是账户资金(插件:WooCommerce 的 WooCommerce 账户资金)显示在他们的“我的账户”区域,允许他们存入 10 美元(如果他们愿意)并绕过订阅。我怎样才能通过 Woocommerce Hook 从他们的 View 中隐藏它?
以下是与付费账户资金插件的“我的账户”相关的源代码,取自/includes/class-wc-account-funds-my-account.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Account_Funds_My_Account
*/
class WC_Account_Funds_My_Account extends WC_Query {
/**
* Constructor
*/
public function __construct() {
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'the_title', array( $this, 'change_endpoint_title' ), 11, 1 );
if ( ! is_admin() ) {
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_filter( 'woocommerce_get_breadcrumb', array( $this, 'add_breadcrumb' ), 10 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 11 );
// Inserting your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'add_menu_items' ) );
add_action( 'woocommerce_account_account-funds_endpoint', array( $this, 'endpoint_content' ) );
add_action( 'wp', array( $this, 'topup_handler' ) );
if ( function_exists( 'WC' ) && version_compare( WC()->version, '2.6', '<' ) ) {
add_action( 'woocommerce_before_my_account', array( $this, 'my_account' ) );
}
}
$this->init_query_vars();
}
/**
* Init query vars by loading options.
*
* @since 2.0.12
*/
public function init_query_vars() {
$this->query_vars = array(
'account-funds' => get_option( 'woocommerce_myaccount_account_funds_endpoint', 'account-funds' ),
);
}
/**
* Adds endpoint breadcrumb when viewing account funds.
*
* @since 2.0.12
*
* @param array $crumbs already assembled breadcrumb data
* @return array $crumbs if we're on a account funds page, then augmented breadcrumb data
*/
public function add_breadcrumb( $crumbs ) {
foreach ( $this->query_vars as $key => $query_var ) {
if ( $this->is_query( $query_var ) ) {
$crumbs[] = array( $this->get_endpoint_title( $key ) );
}
}
return $crumbs;
}
/**
* Check if the current query is for a type we want to override.
*
* @since 2.0.12
*
* @param string $query_var the string for a query to check for
* @return bool
*/
protected function is_query( $query_var ) {
global $wp;
$is_af_query = false;
if ( is_main_query() && is_page() && isset( $wp->query_vars[ $query_var ] ) ) {
$is_af_query = true;
}
return $is_af_query;
}
/**
* Get endpoint title.
*
* @since 2.0.12
*
* @param string $endpoint Endpoint name
* @return string Endpoint title
*/
public function get_endpoint_title( $endpoint ) {
$title = '';
if ( 'account-funds' === $endpoint ) {
$title = __( 'Account Funds', 'woocommerce-account-funds' );
}
return $title;
}
/**
* Changes page title on account funds page.
*
* @since 2.0.12
*
* @param string $title original title
* @return string changed title
*/
public function change_endpoint_title( $title ) {
if ( in_the_loop() ) {
foreach ( $this->query_vars as $key => $query_var ) {
if ( $this->is_query( $query_var ) ) {
$title = $this->get_endpoint_title( $key );
}
}
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* @since 2.0.12
*
* @param array $items
* @return array
*/
public function add_menu_items( $menu_items ) {
// Try insert after orders.
if ( isset( $menu_items['orders'] ) ) {
$new_menu_items = array();
foreach ( $menu_items as $key => $menu ) {
$new_menu_items[ $key ] = $menu;
if ( 'orders' === $key ) {
$new_menu_items['account-funds'] = __( 'Account Funds', 'woocommerce-account-funds' );
}
}
$menu_items = $new_menu_items;
} else {
$menu_items['account-funds'] = __( 'Account Funds', 'woocommerce-account-funds' );
}
return $menu_items;
}
/**
* Endpoint HTML content.
*
* @since 2.0.12
*/
public function endpoint_content() {
$topup = '';
$products = '';
if ( 'yes' === get_option( 'account_funds_enable_topup' ) ) {
$topup = $this->get_my_account_topup();
} else {
$products = $this->get_my_account_products();
}
$recent_deposits = $this->get_my_account_orders();
$vars = array(
'funds' => WC_Account_Funds::get_account_funds(),
'topup' => $topup,
'products' => $products,
'recent_deposits' => $recent_deposits,
);
wc_get_template( 'myaccount/account-funds.php', $vars, '', plugin_dir_path( WC_ACCOUNT_FUNDS_FILE ) . 'templates/' );
}
/**
* Fix for endpoints on the homepage
*
* Based on WC_Query->pre_get_posts(), but only applies the fix for endpoints on the homepage from it
* instead of duplicating all the code to handle the main product query.
*
* @since 2.0.12
*
* @param mixed $q query object
*/
public function pre_get_posts( $q ) {
// We only want to affect the main query
if ( ! $q->is_main_query() ) {
return;
}
if ( $q->is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) !== absint( $q->get( 'page_id' ) ) ) {
$_query = wp_parse_args( $q->query );
if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) {
$q->is_page = true;
$q->is_home = false;
$q->is_singular = true;
$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
add_filter( 'redirect_canonical', '__return_false' );
}
}
}
/**
* Handle top-ups
*/
public function topup_handler() {
if ( isset( $_POST['wc_account_funds_topup'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'account-funds-topup' ) ) {
$min = max( 0, get_option( 'account_funds_min_topup' ) );
$max = get_option( 'account_funds_max_topup' );
$topup_amount = wc_clean( $_POST['topup_amount'] );
if ( $topup_amount < $min ) {
wc_add_notice( sprintf( __( 'The minimum amount that can be topped up is %s', 'woocommerce-account-funds' ), wc_price( $min ) ), 'error' );
return;
} elseif ( $max && $topup_amount > $max ) {
wc_add_notice( sprintf( __( 'The maximum amount that can be topped up is %s', 'woocommerce-account-funds' ), wc_price( $max ) ), 'error' );
return;
}
WC()->cart->add_to_cart( wc_get_page_id( 'myaccount' ), true, '', '', array( 'top_up_amount' => $topup_amount ) );
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
wp_redirect( get_permalink( wc_get_page_id( 'cart' ) ) );
}
}
}
/**
* Show funds on account page
*/
public function my_account() {
$funds = WC_Account_Funds::get_account_funds();
echo '<h2>'. __( 'Account Funds', 'woocommerce-account-funds' ) .'</h2>';
echo '<p>'. sprintf( __( 'You currently have <strong>%s</strong> worth of funds in your account.', 'woocommerce-account-funds' ), $funds ) . '</p>';
if ( 'yes' === get_option( 'account_funds_enable_topup' ) ) {
$this->my_account_topup();
} else {
$this->my_account_products();
}
$this->my_account_orders();
}
/**
* Get HTML string for topup form in my account.
*
* @since 2.0.12
*
* @return string HTML string
*/
public function get_my_account_topup() {
ob_start();
$this->my_account_topup();
return ob_get_clean();
}
/**
* Show top up form
*/
public function my_account_topup() {
$min_topup = get_option( 'account_funds_min_topup' );
$max_topup = get_option( 'account_funds_max_topup' );
$items_in_cart = $this->_get_topup_items_in_cart();
$topup_in_cart = array_shift( $items_in_cart );
if ( ! empty( $max_topup ) && ! empty( $topup_in_cart ) ) {
printf(
'<p class="woocommerce-info"><a href="%s" class="button wc-forward">%s</a> %s</p>',
wc_get_page_permalink( 'cart' ),
__( 'View Cart', 'woocommerce-account-funds' ),
sprintf( __( 'You have "%s" in your cart.', 'woocommerce-account-funds' ), $topup_in_cart['data']->get_title() )
);
return;
}
$vars = array(
'min_topup' => $min_topup,
'max_topup' => $max_topup,
);
wc_get_template( 'myaccount/topup-form.php', $vars, '', plugin_dir_path( WC_ACCOUNT_FUNDS_FILE ) . 'templates/' );
}
/**
* Get topup items in cart.
*
* @since 2.0.6
*
* @return array
*/
private function _get_topup_items_in_cart() {
$topup_items = array();
if ( WC()->cart instanceof WC_Cart && ! WC()->cart->is_empty() ) {
$topup_items = array_filter( WC()->cart->get_cart(), array( $this, 'filter_topup_items' ) );
}
return $topup_items;
}
/**
* Cart items filter callback to filter topup product.
*
* @since 2.0.6
*
* @return bool Returns true if item is topup product
*/
public function filter_topup_items( $item ) {
if ( isset( $item['data'] ) && is_callable( array( $item['data'], 'get_type' ) ) ) {
return ( 'topup' === $item['data']->get_type() );
}
return false;
}
/**
* Show top up products
*/
private function my_account_products() {
$product_ids = get_posts( array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'deposit',
)
),
'fields' => 'ids'
) );
if ( $product_ids ) {
echo do_shortcode( '[products ids="' . implode( ',', $product_ids ) . '"]' );
}
}
/**
* Get HTML string of deposit products in my account page.
*
* @since 2.0.12
*
* @return string HTML string
*/
private function get_my_account_products() {
ob_start();
$this->my_account_products();
return ob_get_clean();
}
/**
* Show deposits
*/
private function my_account_orders() {
$deposits = get_posts( array(
'numberposts' => 10,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => array( 'wc-completed', 'wc-processing', 'wc-on-hold' ),
'meta_query' => array(
array(
'key' => '_funds_deposited',
'value' => '1',
)
)
) );
if ( $deposits ) {
$vars = array(
'deposits' => $deposits,
);
wc_get_template( 'myaccount/recent-deposits.php', $vars, '', plugin_dir_path( WC_ACCOUNT_FUNDS_FILE ) . 'templates/' );
}
}
/**
* Get HTML string of recent deposits.
*
* @since 2.0.12
*
* @return string HTML string
*/
private function get_my_account_orders() {
ob_start();
$this->my_account_orders();
return ob_get_clean();
}
}
new WC_Account_Funds_My_Account();
最佳答案
禁用/隐藏充值表单的简单方法是从插件文件夹中复制“账户资金”模板(myaccount/account-funds.php)< em="">到你的主题文件夹——即复制这个文件:
wp-content/plugins/woocommerce-account-funds/templates/myaccount/account-funds.php
到:
wp-content/themes/your-theme/woocommerce/myaccount/account-funds.php
然后找到并更改以下内容:
<?php echo $topup; ?>
到:
<?php
// If the current user is **not** a "customer", show the topup form.
if ( ! current_user_can( 'customer' ) ) {
echo $topup;
}
?>
查看完整代码 here . (对于“WooCommerce 账户资金”版本 2.1.16)
您也可以编辑充值表单模板本身 (myaccount/topup-form.php) like this .
我还将其添加到主题函数文件中:
// If the user is a "customer", bypass the action which handles top-ups.
add_action( 'wp', function(){
if ( isset( $_POST['wc_account_funds_topup'] ) && current_user_can( 'customer' ) ) {
unset( $_POST['wc_account_funds_topup'] );
}
}, 0 );
关于php - 从 WooCommerce 'Customer' 角色隐藏我帐户中的帐户资金充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56702837/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr