2016-08-02 106 views
1

我在wordpress &中使用woocommerce插件並將其放置在/home/builhsqv/public_html/test/wp-contents/plugins/的位置。現在我已經在/home/builhsqv/public_html/test/test.php中創建了一個php文件。在這個文件裏面,我寫了我的代碼來刪除一個單獨的訂單行項目。刪除訂單行wordpress中的項目

<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路徑和調用函數,我可以看到回聲消息。

我想使用woocommerce中的PHP腳本刪除訂單項。

+0

嗨,大家好,運氣好的話? –

回答

-1

需要先了解您的訂單行ID。

我讓他們這樣的:

$ SQL = '選擇'
。 '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
。 '和woi.order_item_id = woim.order_item_id和woim。 meta_key = \'_ product_id \'
。 'by woi.order_item_name order by 3';

我將它們加載到表中。而當我想刪除行我用這個
功能:

功能order_delete_lines($線)
{
的foreach($線爲$線)
wc_delete_order_item($線);
}

亞科夫

0

在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); 
}