在这里,我们将给大家分享关于WooCommerce–使用php代码更改订单状态的知识,让您更了解php订货单显示代码的本质,同时也会涉及到如何更有效地php–WooCommerce–在自定义订单状态更
在这里,我们将给大家分享关于WooCommerce – 使用php代码更改订单状态的知识,让您更了解php订货单显示代码的本质,同时也会涉及到如何更有效地php – WooCommerce – 在自定义订单状态更改时发送自定义电子邮件、php – WooCommerce – 用户在一段时间内完成状态订单、php – WooCommerce – 重命名并使用重命名的订单状态、php – WooCommerce改变订单状态BACS处理的内容。
本文目录一览:- WooCommerce – 使用php代码更改订单状态(php订货单显示代码)
- php – WooCommerce – 在自定义订单状态更改时发送自定义电子邮件
- php – WooCommerce – 用户在一段时间内完成状态订单
- php – WooCommerce – 重命名并使用重命名的订单状态
- php – WooCommerce改变订单状态BACS处理
WooCommerce – 使用php代码更改订单状态(php订货单显示代码)
我试图改变WooCommerce的订单状态,但到目前为止我没有遇到任何运气. $order实例创建成功(我知道因为echo $order-> status;工作正常,$order_id也正确.$order-> status =’pending’;根本不会改变任何东西,我不知道为什么.
$order = new WC_Order($order_id);
$order->status = 'pending';
任何人都可以帮我这个吗?
解决方法:
试试这段代码:
$order = new WC_Order($order_id);
$order->update_status('pending', 'order_note'); // order note is optional, if you want to add a note to order
php – WooCommerce – 在自定义订单状态更改时发送自定义电子邮件
我添加了自定义状态wc-order-confirmed:
// Register new status
function register_order_confirmed_order_status() {
register_post_status( 'wc-order-confirmed', array(
'label' => 'Potvrzení objednávky',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Potvrzení objednávky <span>(%s)</span>', 'Potvrzení objednávky <span>(%s)</span>' )
) );
}
add_action( 'init', 'register_order_confirmed_order_status' );
// Add to list of WC Order statuses
function add_order_confirmed_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-order-confirmed'] = 'Potvrzení objednávky';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_order_confirmed_to_order_statuses' );
我添加了一个自定义电子邮件wc_confirmed_order:
/**
* A custom confirmed Order WooCommerce Email class
*
* @since 0.1
* @extends \WC_Email
*/
class WC_Confirmed_Order_Email extends WC_Email {
/**
* Set email defaults
*
* @since 0.1
*/
public function __construct() {
// set ID, this simply needs to be a unique name
$this->id = 'wc_confirmed_order';
// this is the title in WooCommerce Email settings
$this->title = 'Potvrzení objednávky';
// this is the description in WooCommerce email settings
$this->description = 'Confirmed Order Notification';
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = 'Potvrzení objednávky';
$this->subject = 'Potvrzení objednávky';
// these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
$this->template_html = 'emails/customer-confirmed-order.PHP';
$this->template_plain = 'emails/plain/admin-new-order.PHP';
// Trigger on confirmed orders
add_action( 'woocommerce_order_status_pending_to_order_confirmed', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_processing_to_order_confirmed', array( $this, 'trigger' ) );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
// this sets the recipient to the settings defined below in init_form_fields()
$this->recipient = $this->get_option( 'recipient' );
// if none was entered, just use the WP admin email as a fallback
if ( ! $this->recipient )
$this->recipient = get_option( 'admin_email' );
}
/**
* Determine if the email should actually be sent and setup email merge variables
*
* @since 0.1
* @param int $order_id
*/
public function trigger( $order_id ) {
// bail if no order ID is present
if ( ! $order_id )
return;
if ( $order_id ) {
$this->object = wc_get_order( $order_id );
$this->recipient = $this->object->billing_email;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
$this->replace['order-number'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() )
return;
// woohoo, send the email!
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
/**
* get_content_html function.
*
* @since 0.1
* @return string
*/
public function get_content_html() {
ob_start();
wc_get_template( $this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading()
) );
return ob_get_clean();
}
/**
* get_content_plain function.
*
* @since 0.1
* @return string
*/
public function get_content_plain() {
ob_start();
wc_get_template( $this->template_plain, array(
'order' => $this->object,
'email_heading' => $this->get_heading()
) );
return ob_get_clean();
}
/**
* Initialize Settings Form Fields
*
* @since 2.0
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/disable',
'type' => 'checkBox',
'label' => 'Enable this email notification',
'default' => 'yes'
),
'recipient' => array(
'title' => 'Recipient(s)',
'type' => 'text',
'description' => sprintf( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr( get_option( 'admin_email' ) ) ),
'placeholder' => '',
'default' => ''
),
'subject' => array(
'title' => 'Subject',
'type' => 'text',
'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
'placeholder' => '',
'default' => ''
),
'heading' => array(
'title' => 'Email heading',
'type' => 'text',
'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
'placeholder' => '',
'default' => ''
),
'email_type' => array(
'title' => 'Email type',
'type' => 'select',
'description' => 'Choose which format of email to send.',
'default' => 'html',
'class' => 'email_type',
'options' => array(
'plain' => __( 'Plain text', 'woocommerce' ),
'html' => __( 'HTML', 'woocommerce' ),
'multipart' => __( 'Multipart', 'woocommerce' ),
)
)
);
}
} // end \WC_confirmed_Order_Email class
我可以在电子邮件设置中看到该电子邮件,以及订单状态下拉列表中的状态.现在,只要订单状态更改为wc-order-confirmed,我就需要发送新电子邮件.过渡钩似乎永远不会被解雇.
我也尝试过:
/**
* Register the "woocommerce_order_status_pending_to_quote" hook which is necessary to
* allow automatic email notifications when the order is changed to refunded.
*
* @modified from https://stackoverflow.com/a/26413223/2078474 to remove anonymous function
*/
add_action( 'woocommerce_init', 'so_25353766_register_email' );
function so_25353766_register_email(){
add_action( 'woocommerce_order_status_pending_to_order_confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
add_action( 'woocommerce_order_status_processing_to_order_confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
}
这似乎根本不起作用……请问有什么想法吗?
解决方法:
正如Xcid的回答所示,您需要注册该电子邮件.
在WC 2.2中,我相信你可以通过以下方式做到这一点:
add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
我已经为WooCommerce 2.3添加了一个过滤器,所以当出现这种情况时,自定义电子邮件将能够添加到WooCommerce注册的电子邮件操作列表中:
// As of WooCommerce 2.3
function so_27112461_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-order-confirmed';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );
php – WooCommerce – 用户在一段时间内完成状态订单
我需要在上个月通过Woocommerce中的USER ID获得用户完成的购买.
用户有级别(金牌,银牌):
>金卡会员每月可以购买4件商品;
>银卡会员每月可以购买1件商品.
我需要在将商品添加到购物车之前检查一下.我不想仅为此功能使用插件(无法找到,BTW).
那可能吗?
我怎样才能做到这一点?
谢谢
解决方法:
可以获得当前客户在过去30天内购买的总物品数量.
以下是此功能based on this answer的代码:
function current_customer_month_count( $user_id=null ) {
if ( empty($user_id) ){
$user_id = get_current_user_id();
}
// Date calculations to limit the query
$today_year = date( 'Y' );
$today_month = date( 'm' );
$day = date( 'd' );
if ($today_month == '01') {
$month = '12';
$year = $today_year - 1;
} else{
$month = $today_month - 1;
$month = sprintf("%02d", $month);
$year = $today_year - 1;
}
// ORDERS FOR LAST 30 DAYS (Time calculations)
$Now = strtotime('Now');
// Set the gap time (here 30 days)
$gap_days = 30;
$gap_days_in_seconds = 60*60*24*$gap_days;
$gap_time = $Now - $gap_days_in_seconds;
// The query arguments
$args = array(
// WC orders post type
'post_type' => 'shop_order',
// Only orders with status "completed" (others common status: 'wc-on-hold' or 'wc-processing')
'post_status' => 'wc-completed',
// all posts
'numberposts' => -1,
// for current user id
'Meta_key' => '_customer_user',
'Meta_value' => $user_id,
'date_query' => array(
//orders published on last 30 days
'relation' => 'OR',
array(
'year' => $today_year,
'month' => $today_month,
),
array(
'year' => $year,
'month' => $month,
),
),
);
// Get all customer orders
$customer_orders = get_posts( $args );
$count = 0;
if (!empty($customer_orders)) {
$customer_orders_date = array();
// Going through each current customer orders
foreach ( $customer_orders as $customer_order ){
// Conveting order dates in seconds
$customer_order_date = strtotime($customer_order->post_date);
// Only past 30 days orders
if ( $customer_order_date > $gap_time ) {
$customer_order_date;
$order = new WC_Order( $customer_order->ID );
$order_items = $order->get_items();
// Going through each current customer items in the order
foreach ( $order_items as $order_item ){
$count++;
}
}
}
return $count;
}
}
此代码位于活动子主题(或主题)的function.PHP文件中,或者也可以放在任何插件文件中.
该函数接受可选的user_id参数(如果需要).例如,对于具有56值的user_id,您将使用该函数,这样:
// For user ID: "56".
$number_of_items_in_last_month = current_customer_month_count('56');
该函数将获取$user_id = get_current_user_id();中的当前用户ID,如果您没有设置参数user_id值,这样:
// For current logged user.
$number_of_items_in_last_month = current_customer_month_count();
You can use this function in a conditional if statement to hide or replace the add-to-cart button through somme WooCommerce 07001 or 07002.
You Could get helped in that task, asking a new question including this code, and providing more details about how you want to do it.
此代码经过测试和运行.
参考文献:Checking if customer has already bought something in WooCommerce
php – WooCommerce – 重命名并使用重命名的订单状态
我已经使用此代码将订单状态“已完成”重命名为“已付款”
function wc_renaming_order_status( $order_statuses ) {
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-completed' === $key ) {
$order_statuses['wc-completed'] = _x( 'Paid', 'Order status', 'woocommerce' );
}
}
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
我的问题是我做了一个包含所有订单列表的页面模板:
<?PHP
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<tr>
<td><?PHP echo $order->get_order_number(); ?></td>
<td><?PHP echo $order->billing_first_name; ?>
<?PHP echo $order->billing_last_name; ?></td>
<td><?PHP echo $order->billing_company; ?></td>
<td><?PHP echo $order->status; ?></td>
</tr>
<?PHP endwhile; ?>
并且$order->状态仍然返回’已完成’而不是’已付’.
我怎么解决这个问题?
谢谢
解决方法:
这是正常的,在这种特殊情况下,您可以使用一些额外的代码,创建一个显示自定义重命名状态的函数:
function custom_status($order){
if($order->status == 'completed')
return _x( 'Paid', 'woocommerce' );
else
return $order->status;
}
此代码位于活动子主题(或主题)的function.PHP文件中,或者也可以放在任何插件文件中.
在您的模板页面中,您将以这种方式使用它:
<?PHP
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<tr>
<td><?PHP echo $order->get_order_number(); ?></td>
<td><?PHP echo $order->billing_first_name; ?>
<?PHP echo $order->billing_last_name; ?></td>
<td><?PHP echo $order->billing_company; ?></td>
<td><?PHP echo custom_status($order); ?></td>
</tr>
<?PHP endwhile; ?>
此代码经过测试和运行.
参考:Renaming WooCommerce Order Status
php – WooCommerce改变订单状态BACS处理
在WooCommerce中,任何与BACS(直接银行转账)一起下的订单都设置为“暂停”.
如何自动将其更改为处理?
我不想在functions.PHP里面工作
我有以下代码,但这不起作用:
add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );
function rfvc_update_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( 'on-hold' == $order_status && 'on-hold' == $order->status ) {
return 'processing';
}
return $order_status;
}
任何帮助都会很棒!
解决方法:
Update (added a version for woocommerce 3+ at the end)
似乎woocommerce_payment_complete_order_status动作挂钩不会触发BACS支付方式.
基于this thread,’woocommerce_thankyou’动作挂钩完成工作:
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ('on-hold' == $order->status || 'pending' == $order->status) ) {
$order->update_status('processing');
} else {
return;
}
}
代码位于活动子主题(或活动主题)的function.PHP文件中.测试和工作.
对于woocommerce 3版本:
这里我们使用类似的复合钩woocommerce_thankyou_{$order->get_payment_method()}
:
add_action( 'woocommerce_thankyou_bacs', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
if ( in_array( $order->get_status(), array('on-hold', 'pending') ) ) {
$order->update_status('processing');
} else {
return;
}
}
代码位于活动子主题(或活动主题)的function.PHP文件中.测试和工作.
我们今天的关于WooCommerce – 使用php代码更改订单状态和php订货单显示代码的分享已经告一段落,感谢您的关注,如果您想了解更多关于php – WooCommerce – 在自定义订单状态更改时发送自定义电子邮件、php – WooCommerce – 用户在一段时间内完成状态订单、php – WooCommerce – 重命名并使用重命名的订单状态、php – WooCommerce改变订单状态BACS处理的相关信息,请在本站查询。
本文标签: