掌握WooCommerce动作和筛选器

The use of WooCommerce, along with WordPress, to create eCommerce websites is on the rise. If you are a Web Developer like me, knowing the ins and out of the WooCommerce template system, along with it’s actions and filters is a very nice skill to have. Just think how happy your boss would be if you knew everything there is to know about WooCommerce, or at least enough to be able to implement anything with very little help, just you and the WooCommerce documentation.

使用WooCommerce和WordPress创建电子商务网站的趋势正在上升。 如果您是像我这样的Web开发人员,那么了解WooCommerce模板系统的内幕和外幕,以及它的操作和过滤器是一项非常不错的技能。 只要想一想您的老板会是多么高兴,如果您了解WooCommerce的所有知识,或者至少足以在很少的帮助下实现任何事情,仅您和WooCommerce文档即可。

If all this sounds good to you, continue reading and we’ll explore what can be done with WordPress and WooCommerce working side-by-side. The style of this article (and hopefully many more to come) will include presenting you with a problem and then coming up with a solution using WordPress and WooCommerce actions and filters.

如果这一切对您来说都很好,请继续阅读,我们将探索WordPress和WooCommerce可以并行工作的方法。 本文的风格(并希望将来还会有更多风格)包括向您提出问题,然后使用WordPress和WooCommerce操作和过滤器提出解决方案。

创建和更新帐户的有用操作 (Useful Actions For Account Creation and Updating)

It’s very common to use a CRM along with your eCommerce website. WooCommerce makes it simple for us to detect when a new customer or user registers during the Checkout process or via the ‘My Account’ page.

与您的电子商务网站一起使用CRM是很常见的。 WooCommerce使我们可以很容易地在结帐过程中或通过“我的帐户”页面检测新客户或用户何时注册。

The following example should give you an idea of how to create an account on Salesforce when someone registers through WooCommerce. Note that the Salesforce API code is not shown; I’m just outlining the process.

以下示例将使您了解当有人通过WooCommerce注册时如何在Salesforce上创建帐户的想法。 请注意,未显示Salesforce API代码。 我只是概述过程。

<?php
// Create Customer's Salesforce Account
add_action( 'woocommerce_created_customer', 'create_salesforce_account', 10, 3 );
function create_salesforce_account( $customer_id, $new_customer_data, $password_generated ) {

	// Get the New Customer's data
	$username   = $new_customer_data['user_login'];
	$password   = $new_customer_data['user_pass'];
	$email      = $new_customer_data['user_email'];
	$role       = $new_customer_data['role'];

	// Getting the rest of the info for this customer
	$user = get_user_by( 'id', $customer_id );

	// Continue and send the information to Salesforce now
}

Continuing on from our previous example, it’s only natural that you would want to update the customer’s information on Salesforce when the customer edits either the Billing or Shipping Address. woocommerce_customer_save_address runs immediately after either address has been edited and it gives the User ID and which address was edited. Note that you can also get any of the form fields via $_POST.

继续前面的示例,当客户编辑“开票地址”或“收货地址”时,您自然希望在Salesforce上更新客户信息。 woocommerce_customer_save_address在任何一个地址被编辑后立即运行,并提供用户ID和被编辑的地址。 请注意,您还可以通过$_POST获得任何表单字段。

<?php
// Update Salesforce account with new shipping information
add_action( 'woocommerce_customer_save_address', 'update_salesforce_account', 10, 2 );
function update_salesforce_account( $user_id, $load_address ) {

	// Let's use a Switch statement to know which address we are updating
	switch ( $load_address ) {
		case 'billing':
			// Send billing info to Salesforce
			break;

		case 'shipping':
			// Send shipping info to Salesforce
			break;
	}
}

覆盖前端标题,消息和描述 (Overriding Front-End Titles, Messages And Descriptions)

Section titles, messages and descriptions can easily be changed if you know what filter to use. These filters allow you to customize what’s displayed to the customer down to the last detail. Did you notice I’m saying filters and not actions? That’s because WooCommerce doesn’t use actions for overriding these kinds of values, just like WordPress does.

如果您知道要使用的过滤器,则可以轻松更改节标题,消息和说明。 这些过滤器使您可以自定义显示给客户的信息,直到最后一个细节。 您是否注意到我说的是过滤器,而不是操作? 这是因为WooCommerce不像WordPress那样使用操作来覆盖这些类型的值。

WooCommerce checkout coupon message

woocommerce_checkout_coupon_message displays only if coupons are enabled on your store, and while you are in the checkout page it asks you for a coupon code. For example: Have a coupon? Click here to enter your code.

woocommerce_checkout_coupon_message仅在商店中启用了优惠券时显示,并且当您在结帐页面时,它会询问您优惠券代码。 例如:有优惠券? 单击此处输入您的代码。

WooCommerce checkout login message

woocommerce_checkout_login_message displays on the checkout page when the cart detects that you are not logged in and it gives you the option to sign-in. For example: Returning customer? Click here to login.

当购物车检测到您尚未登录并且您可以选择登录时, woocommerce_checkout_login_message显示在结帐页面上。 例如:回头客? 点击此处登录。

WooCommerce lost password message

woocommerce_lost_password_message displays under the ‘My Account’ page where it instructs you to enter your username or email in order to reset your password. For example: Lost your password? Please enter your username or email address. You will receive a link to create a new password via email.

woocommerce_lost_password_message显示在“我的帐户”页面下,指示您输入用户名或电子邮件以重置密码。 例如:忘记密码了? 请输入您的用户名或电子邮件地址。 您将收到一个通过电子邮件创建新密码的链接。

WooCommerce my account my address title

woocommerce_my_account_my_address_title displays on the ‘My Account’ page and by default it can have one of two titles. Its modified slightly from ‘My Addresses’ to ‘My Address’ when you have set-up to ship only to the billing address so there is only a need for one of them. For example: My Addresses or My Address.

woocommerce_my_account_my_address_title显示在“我的帐户”页面上,默认情况下,它可以具有两个标题之一。 当您设置为仅发送到帐单地址时,将其从“我的地址”略微修改为“我的地址”,因此只需要其中一个即可。 例如:我的地址或我的地址。

WooCommerce my account my address description

woocommerce_my_account_my_address_description displays right below the Address Title, giving you a brief explanation of what the address(es) will be used for. For example: The following addresses will be used on the checkout page by default.

woocommerce_my_account_my_address_description显示在地址标题的正下方,为您简要说明该地址的用途。 例如:默认情况下,以下地址将在结帐页面上使用。

WooCommerce my account my downloads title

woocommerce_my_account_my_downloads_title displays on the ‘My Account’ page and sets the title for the downloads section. For example: Available downloads.

woocommerce_my_account_my_downloads_title显示在“我的帐户”页面上,并设置下载部分的标题。 例如:可用下载。

WooCommerce my account my orders title

woocommerce_my_account_my_orders_title displays on the ‘My Account’ page and sets the title for the Orders section. For example: Recent Orders.

woocommerce_my_account_my_orders_title显示在“我的帐户”页面上,并设置“订单”部分的标题。 例如:最近的订单。

There are quite a few of these and the usage is almost identical with the exception of names, of course. Here are some examples on how to change them.

其中有很多,当然除了名称之外,用法几乎相同。 以下是一些有关如何更改它们的示例。

<?php
add_filter( 'woocommerce_checkout_coupon_message',              'override_checkout_coupon_message', 10, 1 );
function override_checkout_coupon_message( $message ) {
    return __( 'Have a coupon for our store?', 'spyr' ) . ' <a href="#" class="showcoupon">' . __( 'Click here to enter it', 'spyr' ) . '</a>';
}

add_filter( 'woocommerce_checkout_login_message',               'override_checkout_login_message',  10, 1 );
function override_checkout_login_message( $message ) {
    return __('Already have an account with us?', 'spyr' );
}

add_filter( 'woocommerce_lost_password_message',                'override_lost_password_message',   10, 1 );
function override_lost_password_message( $message ) {
    return  __( 'Lost your password? Please enter your username or email address.', 'spyr' );
}

add_filter( 'woocommerce_my_account_my_address_title',          'override_my_address_title',        10, 1 );
function override_my_address_title( $title ) {
    return __( 'Your Address', 'spyr' );
}
add_filter( 'woocommerce_my_account_my_address_description',    'override_my_address_description',  10, 1 );
function override_my_address_description( $description ) {
    return __( 'The following addresses will be used on the checkout.', 'spyr' );
}
add_filter( 'woocommerce_my_account_my_downloads_title',        'override_my_downloads_title',      10, 1 );
function override_my_downloads_title( $title ) {
    return __( 'Your Downloads', 'spyr' );
}
add_filter( 'woocommerce_my_account_my_orders_title',           'override_my_orders_title',         10, 1 );
function override_my_orders_title( $title ) {
    return __( 'Your Most Recent Orders', 'spyr' );
}

That’s it for now. We’ve only begun to explore what’s under the hood of WooCommerce and as we progress through a series of articles, we are going to be building more complex modules that will rely heavily on using WooCommerce’s and WordPress’ hooks system to produce whatever we set our minds to do.

现在就这样。 我们才刚刚开始探索WooCommerce的内幕,随着我们阅读一系列文章,我们将构建更复杂的模块,这些模块将严重依赖于使用WooCommerce和WordPress的hooks系统生成我们设置的内容要做的事。

In the next article, we will work on a real world case scenario where you might want to give a free product with any purchase that meets a certain criteria among other items of interest.

在下一篇文章中,我们将在一个实际案例中进行研究,在这种情况下,您可能希望在购买的任何符合特定条件的商品中提供免费产品,以及其他感兴趣的商品。

翻译自: https://www.sitepoint.com/mastering-woocommerce-actions-and-filters/

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值