Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动删除购物车中的缺货项目,然后通知帐户用户-Woocommerce_Php_Wordpress_Hook Woocommerce - Fatal编程技术网

Php 自动删除购物车中的缺货项目,然后通知帐户用户-Woocommerce

Php 自动删除购物车中的缺货项目,然后通知帐户用户-Woocommerce,php,wordpress,hook-woocommerce,Php,Wordpress,Hook Woocommerce,我希望允许客户将项目添加到他们的Woocommerce购物车中,并将其保留任意时间,以便他们可以在空闲时添加到购物车中。任何缺货的行都需要自动从购物车中删除,并显示一条消息,表明已发生这种情况。类似于“所有缺货项目都已从帐户中删除,因为它们不再可用” 到目前为止我已经试过了 public function is_in_stock() { return apply_filters( 'woocommerce_product_is_in_stock', 'instock' === $this->

我希望允许客户将项目添加到他们的Woocommerce购物车中,并将其保留任意时间,以便他们可以在空闲时添加到购物车中。任何缺货的行都需要自动从购物车中删除,并显示一条消息,表明已发生这种情况。类似于“所有缺货项目都已从帐户中删除,因为它们不再可用”

到目前为止我已经试过了

public function is_in_stock() {
return apply_filters( 'woocommerce_product_is_in_stock', 'instock' === $this->get_stock_status(), $this );
}
function notes_in_cart() {
 global $woocommerce;

if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
    return;
}

if ( isset( $_POST['post_data'] ) ) {
    parse_str( $_POST['post_data'], $post_data );
} else {
    $post_data = $_POST; // fallback for final checkout (non-ajax)
}

if ( WC()->cart->needs_shipping() ){

    // set $out_of_stock_exists to false by default
    $out_of_stock_exists = false;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        if($values['data']->backorders_allowed()){ //check if backorders are allowed on this product
            // get the stock quantity - returns the available amount number
            $stock_info = $values['data']->get_stock_quantity();

            if($stock_info < $values['quantity']){ 
    set $out_of_stock_exists to true and stop foreach execution
                $out_of_stock_exists = true;
                break;
            }
        }

    }

    //if cart has items out of stock
    if ($out_of_stock_exists) {
        ?>
        <tr class="ceckoutStockMeta">
            <th>Item Shipments</th>
            <td>
                <p style="color: red;">*All out of stock items have been removed from your cart as they are no longer available.</p><br>
                <form>

                    <input type="radio" name="stockOp" id="stockOption1" value="ship" />
                    <label for="stockOption1">Ship what is available now</label><br>

                    <input type="radio" name="stockOp" id="stockOption2" value="hold" />
                    <label for="stockOption2">Wait and ship together</label>
                </form>
            </td>
        </tr>

        <?php

    }
}
add_action( 'woocommerce_cart_totals_after_order_total', 'notes_in_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'notes_in_cart' );
公共功能为库存(){
返回apply_过滤器('woocommerce_product_是库存中的,'instock'===$this->get_stock_status(),$this);
}
功能说明\u在\u购物车()中{
全球商业;
如果(!$_POST | |(is_admin()&&!is_ajax())){
返回;
}
如果(isset($_POST['POST_data'])){
parse_str($_POST['POST_data'],$POST_data);
}否则{
$post\u data=$\u post;//最终签出的回退(非ajax)
}
如果(WC()->购物车->需要装运()){
//默认情况下,将股票的$out\U exists设置为false
$out\u of\u stock\u exists=false;
foreach($woocommerce->cart->get\u cart()作为$cart\u item\u key=>$value){
if($values['data']->backorders_allowed()){//检查此产品是否允许延期交货
//获取库存数量-返回可用数量编号
$stock_info=$values['data']->get_stock_quantity();
如果($stock_info<$values['quantity']){
将股票存在的$out\u设置为true,并停止每次执行
$out\u of\u stock\u exists=真;
打破
}
}
}
//如果购物车中有缺货的物品
如果($out\u of\u stock\u存在){
?>
项目装运

*所有缺货商品都已从您的购物车中删除,因为它们不再可用。


现在即可发货
一起等着装船
我已经找出了哪里出了问题,我完全走错了方向。我在下面发布了正确的代码,以防将来其他人需要此功能。它可以同时执行这两个功能,在产品缺货时更新购物车,同时留下新消息

/**
 * This code will automatically remove any out of stock items from a shopping cart.
 * This would be in cases when users add products to their cart and come back to it later.
*
*/
function orb_check_for_out_of_stock_products() {
if ( WC()->cart->is_empty() ) {
    return;
}

$removed_products = [];

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $product_obj = $cart_item['data'];

    if ( ! $product_obj->is_in_stock() ) {
        WC()->cart->remove_cart_item( $cart_item_key );
        $removed_products[] = $product_obj;
    }
}

if (!empty($removed_products)) {
    wc_clear_notices(); // remove any WC notice about sorry about out of stock products to be removed from cart.

    foreach ( $removed_products as $idx => $product_obj ) {
        $product_name = $product_obj->get_title();
        $msg = sprintf( __( "The product '%s' was removed from your cart because it is now out of stock. Sorry for any inconvenience caused.", 'woocommerce' ), $product_name);

        wc_add_notice( $msg, 'error' );
    }
}

}
add_action('woocommerce_before_cart', 'orb_check_for_out_of_stock_products');
我感谢那些提供帮助的人,也非常感谢斯维托斯拉夫·马里诺夫(Svetoslav Marinov)直接与我联系,向我提供了他的意见。正是他,引导我朝着正确的方向前进


请随时使用您可以识别的任何改进来更新此代码。

您好,欢迎使用Stack Overflow。您的问题要求提供建议,以及如何使Stack Overflow对任何其他用户来说都非常模糊。请参考并包括您所做的事情、遇到的问题以及您希望它做的事情。谢谢=)“我的客户希望”如果您不能为您的客户做特定的开发工作(由于缺乏知识或其他原因)你能为你的客户做的最好的事情就是告诉他们,这样他们就可以雇佣一个有能力的开发人员。你只会浪费你和他们的时间。谢谢@Minial我是这个网站的新手,所以以后会尝试提供更多的细节。我已经编辑了我的原始问题以符合要求。希望现在它更合适。:@JustinR。这不是一个理想的格力方式我不是这个网站的新成员。有人告诉我stackoverflow是寻求编程需求帮助的最佳场所,但当我第一次尝试寻求帮助时,我就遭到了猛烈的抨击。也许提出以价格解决我的问题会是一个更好的问候。人们每天都来这里,希望别人免费为他们编写所有代码。一般来说,更多的人从事他们无法从事的开发工作。我的声明适用于任何从事web开发业务的人;这不是针对个人的。正如你最初提出的问题,它不符合社区指导原则。它缺少代码,也没有显示出任何研究成果和这里的大多数人一样,我很乐意在可能的情况下免费回答问题,但我来这里并不是为了完全做别人的工作。