【问题标题】:How to remove sale price on specific product in Woocommerce?如何在 Woocommerce 中删除特定产品的销售价格?
【发布时间】:2020-07-27 18:11:52
【问题描述】:

更新。我不需要重新计算订单或购物车,如果订单成功后库存数量为零,我需要删除产品的销售价格。

如果产品缺货,我需要从产品中删除销售价格。我发现了一些代码 sn-ps 但它们都不起作用。找不到正确的解决方案。下面是我的功能,请指教:

function remove_sale_price ( $order_id ) { 
    $order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_id => $item ) {
        $prod_id  = $item->get_product_id();
        $name     = $item->get_name();
        $product  = wc_get_product($prod_id);
        if ($product->is_type( 'variable' )) {
            $prod_id  = $item->get_variation_id();
            $product  = wc_get_product($prod_id);
        }
        $quantity = $product->get_stock_quantity();
        $note     = 'Sale price removed on <b>' . $name . '</b>';
        if ( ($quantity < 1) && (!empty($product->get_sale_price()) ) ) {
            
            /*** HERE I NEED TO REMOVE SALE PRICE FROM PRODUCT ***/

            $order->add_order_note( $note );
       }
    }
}
add_action( 'woocommerce_order_status_processing', 'remove_sale_price');
add_action( 'woocommerce_order_status_completed', 'remove_sale_price');

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    找到解决方案:

    $product->set_sale_price('');
    $product->save();
    

    我不知道我需要保存产品。这是我的全部功能:

    function remove_sale_price ( $order_id ) { 
        $order = new WC_Order( $order_id );
        foreach ( $order->get_items() as $item_id => $item ) {
            $prod_id  = $item->get_product_id();
            $name     = $item->get_name();
            $product  = wc_get_product($prod_id);
            if ($product->is_type( 'variable' )) {
                $prod_idv  = $item->get_variation_id();
                $product   = wc_get_product($prod_idv);
            }
            $quantity = $product->get_stock_quantity();
            $note     = 'Removed sale price on <b>«' . $name . '»</b>.';
            if ( $quantity < 1 ) {
                $order->add_order_note( $note );
                $product->set_sale_price('');
                $product->save();
            }
        }
    }
    add_action( 'woocommerce_order_status_processing', 'remove_sale_price' );
    add_action( 'woocommerce_order_status_completed', 'remove_sale_price' );
    

    功能说明:如果订单状态为“处理中”或“已完成”(这意味着库存减少),功能将检查订单产品的库存数量(在我的情况下,我只需要检查简单和可变的产品)。如果库存小于 1(0、-1 等),则会从产品或变体中删除销售价格。在 Woocommerce 4.3.1 上测试。此功能不影响顺序。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多