【问题标题】:Delete order line Items in wordpress在 wordpress 中删除订单行项目
【发布时间】:2016-08-02 11:07:07
【问题描述】:

我在 wordpress & 中使用 woocommerce 插件并将其放在 /home/builhsqv/public_html/test/wp-contents/plugins/ 位置。现在我在/home/builhsqv/public_html/test/test.php 中创建了一个php 文件。在这个文件中,我编写了删除单个订单行项目的代码。

<strong>
//TEST.PHP
<?php   
error_reporting(E_ALL );
require_once( dirname(__FILE__)."/wp-content/plugins/woocommerce/includes/wc-order-functions.php");
echo "Woocommerce";
$itemID = 1234;
echo $result = wc_delete_order_item($itemID);
print_r($result);  die;
?>

//wc-order-functions.php (File of woocommerce with function body)
<?php
function wc_delete_order_item( $item_id ) {
    global $wpdb;
    $item_id = absint( $item_id );
    if ( ! $item_id )
        return false;
    do_action( 'woocommerce_before_delete_order_item', $item_id );
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) );
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $item_id ) );
    do_action( 'woocommerce_delete_order_item', $item_id );
    return true;
}
?>
</strong>

当我在浏览器中运行此文件时,它显示一个空白页面。 但是当我注释 require 路径和调用函数时,我可以看到 echo 消息。

我想在 woocommerce 中使用 PHP 脚本删除订单行项目。

【问题讨论】:

  • 大家好,祝你好运吗?

标签: php wordpress plugins woocommerce


【解决方案1】:

首先需要知道您的订单行 ID。

我是这样理解的:

$sql = 'SELECT woi.order_item_name, woim.meta_value, woim.order_item_id
    FROM wp_woocommerce_order_items woi JOIN wp_woocommerce_order_itemmeta woim
    WHERE order_id = ' . $order_id . '
    AND woi.order_item_id = woim.order_item_id AND woim.meta_key = \'_product_id\'
    GROUP BY woi.order_item_name ORDER BY 3';

我将它们加载到一个表中。当我想删除行时,我使用此功能:

function order_delete_lines($lines) {
    foreach ($lines as $line) {
        wc_delete_order_item($line);
    }
}

【讨论】:

    【解决方案2】:

    您可能会在 WooCommerce 中发现此文件需要插件和 WordPress 本身的其他人才能删除订单项目。

    虽然我不确定你为什么要使用脚本来处理 1 个订单的 1 个订单项,而这可以通过 CMS 或数据库完成,但要使用 WooCommerce 辅助函数复制 index.php,并更改 WP_USE_THEMES像这样到false

    <?php
    /**
     * Front to the WordPress application. This file doesn't do anything, but loads
     * wp-blog-header.php which does and tells WordPress to load the theme.
     *
     * @package WordPress
     */
    
    /**
     * Tells WordPress to load the WordPress theme and output it.
     *
     * @var bool
     */
    define('WP_USE_THEMES', false);
    
    /** Loads the WordPress Environment and Template */
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );
    
    /** Delete order line item 1234 */
    $itemID = 1234;
    $result = wc_delete_order_item($itemID);
    echo "<pre>";
    print_r($result);
    echo "</pre>";
    

    只要您存储在 WordPress 安装的根目录中,这将实现您想要做的事情。

    我偶然发现了这个想要删除与订单 ID 相关的所有项目而不直接查询数据库的问题,在为其他寻找相同结果的人挖掘 WooCommerce 代码后,结果

    $order_id = 123456;
    $order    = new WC_Order($order_id);
    
    foreach ($order->get_items() as $order_item_id => $item){ 
        wc_delete_order_item($order_item_id);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2021-10-17
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多