WooCommerce:如果购物车中的数量超过股票的数量,请更改交货时间值

发布于 2025-02-11 00:55:51 字数 3568 浏览 1 评论 0原文

如果购物车中的数量超过仓库中的数量,我想更改functions.php中的交付时间的值。不幸的是,我的PHP知识非常有限。

以下代码已用于在产品数据中创建一个字段,如果产品不库存,则可以输入交货时间。如果股票为0,则还将显示新的交货时间。

问题在于这不会改变。如果有人将我放在购物车中,而不是库存。实际上,应显示替代交货时间。

/*================================================
Lieferzeitenanzeige bei Lieferrückstand
================================================*/
add_action( 'woocommerce_process_product_meta', 'wdt_process_delivery_time_fallback', 10, 2 );
function wdt_process_delivery_time_fallback( $id, $post ){
 
    $dtf_id = $_POST['delivery_time_fallback'];
    if( $dtf_id === '' ) {
        delete_post_meta( $id, '_delivery_time_fallback' );
    } else {
        update_post_meta( $id, '_delivery_time_fallback', $dtf_id );
    }
 
}

add_action( 'woocommerce_product_options_stock_status', 'wdt_add_deliver_time_fallback' );
function wdt_add_deliver_time_fallback() {
    // Lieferzeiten aus den Terms generieren
    $delivery_times = get_terms( array(
        'taxonomy' => 'product_delivery_time',
        'hide_empty' => false,
    ) );
    $options[''] = __( 'Keine', 'woocommerce');
    foreach ($delivery_times as $key => $term) {
        $options[$term->term_id] = $term->name;
    }
    // gewählte Lieferzeit aufbereiten
    $dtf_id = get_post_meta( get_the_ID(), '_delivery_time_fallback', true );
    // Element generieren
    woocommerce_wp_select( array(
        'id'          => 'delivery_time_fallback',
        'label'       => __('Lieferzeit bei Lieferrückstand', 'woocommerce'),
        'options'     => $options,
        'value'       => $dtf_id,
        'desc_tip' => true,
        'description' => __( 'Lieferzeit die angezeigt wird, wenn sich der Artikel im Lieferrückstand befindet.' ),
    ) );
        
}

add_filter( 'woocommerce_germanized_delivery_time_backorder_html', 'wdt_adjust_delivery_time_html', 10, 4 );
function wdt_adjust_delivery_time_html( $text, $product ) {

    // this must be a variation if we can find a parent_id
    if ( $product->get_parent_id() ) {
        $id = $product->get_parent_id();
    } else {
        $id = $product->get_id();
    }
    
    // let's try to find the parent delivery time in postmeta
    if ( metadata_exists( 'post', $id, '_delivery_time_fallback' ) ) {

        $delivery_times = get_terms( array(
            'taxonomy' => 'product_delivery_time',
            'hide_empty' => false,
        ) );

        $dtf_id = get_post_meta( $id, '_delivery_time_fallback', true );

        foreach ($delivery_times as $dtf) {
            if ( $dtf->term_id == $dtf_id) {
                $delivery_time = $dtf->name;
                break;
            }
        }

    } else {
        $delivery_time = '1-2 Wochen ab Bestelldatum';
    }
    return 'Lieferzeit: '.$delivery_time;

}

要将购物车与股票进行比较,我已经有以下内容:

add_action( 'woocommerce_single_product_summary', 'woocommerce_stock_now' ); 
  
  function woocommerce_stock_now() {
        global $product;
        $stocknow = $product->get_stock_quantity();
        ?>
        <script>
              jQuery(document).on('input change','[name=quantity]',function() {
                    var stocknow = '<?php echo $stocknow; ?>';
                    var qtyinput = jQuery(this).val(); 
                    var overdue = parseInt(qtyinput) - parseInt(stocknow);
                    if (parseInt(qtyinput) > parseInt(stocknow)){
        </script>
        <?php 
  }

我还需要添加什么以更改交货时间的价值?

除了WooCommerce之外,还在使用“术语”插件。

感谢您的帮助。

I would like to change the value for delivery time in functions.php if the quantity in the shopping cart exceeds the quantity in the warehouse. My PHP knowledge is unfortunately very limited.

The following code has been used to create a field in the product data where you can enter a delivery time if the product is not in stock. If the stock is 0, the new delivery time will also be displayed.

The problem is that this does not change. If someone puts im more in the cart than is in stock. Then actually, the alternative delivery time should be displayed.

/*================================================
Lieferzeitenanzeige bei Lieferrückstand
================================================*/
add_action( 'woocommerce_process_product_meta', 'wdt_process_delivery_time_fallback', 10, 2 );
function wdt_process_delivery_time_fallback( $id, $post ){
 
    $dtf_id = $_POST['delivery_time_fallback'];
    if( $dtf_id === '' ) {
        delete_post_meta( $id, '_delivery_time_fallback' );
    } else {
        update_post_meta( $id, '_delivery_time_fallback', $dtf_id );
    }
 
}

add_action( 'woocommerce_product_options_stock_status', 'wdt_add_deliver_time_fallback' );
function wdt_add_deliver_time_fallback() {
    // Lieferzeiten aus den Terms generieren
    $delivery_times = get_terms( array(
        'taxonomy' => 'product_delivery_time',
        'hide_empty' => false,
    ) );
    $options[''] = __( 'Keine', 'woocommerce');
    foreach ($delivery_times as $key => $term) {
        $options[$term->term_id] = $term->name;
    }
    // gewählte Lieferzeit aufbereiten
    $dtf_id = get_post_meta( get_the_ID(), '_delivery_time_fallback', true );
    // Element generieren
    woocommerce_wp_select( array(
        'id'          => 'delivery_time_fallback',
        'label'       => __('Lieferzeit bei Lieferrückstand', 'woocommerce'),
        'options'     => $options,
        'value'       => $dtf_id,
        'desc_tip' => true,
        'description' => __( 'Lieferzeit die angezeigt wird, wenn sich der Artikel im Lieferrückstand befindet.' ),
    ) );
        
}

add_filter( 'woocommerce_germanized_delivery_time_backorder_html', 'wdt_adjust_delivery_time_html', 10, 4 );
function wdt_adjust_delivery_time_html( $text, $product ) {

    // this must be a variation if we can find a parent_id
    if ( $product->get_parent_id() ) {
        $id = $product->get_parent_id();
    } else {
        $id = $product->get_id();
    }
    
    // let's try to find the parent delivery time in postmeta
    if ( metadata_exists( 'post', $id, '_delivery_time_fallback' ) ) {

        $delivery_times = get_terms( array(
            'taxonomy' => 'product_delivery_time',
            'hide_empty' => false,
        ) );

        $dtf_id = get_post_meta( $id, '_delivery_time_fallback', true );

        foreach ($delivery_times as $dtf) {
            if ( $dtf->term_id == $dtf_id) {
                $delivery_time = $dtf->name;
                break;
            }
        }

    } else {
        $delivery_time = '1-2 Wochen ab Bestelldatum';
    }
    return 'Lieferzeit: '.$delivery_time;

}

To compare the cart with the stock I already have the following:

add_action( 'woocommerce_single_product_summary', 'woocommerce_stock_now' ); 
  
  function woocommerce_stock_now() {
        global $product;
        $stocknow = $product->get_stock_quantity();
        ?>
        <script>
              jQuery(document).on('input change','[name=quantity]',function() {
                    var stocknow = '<?php echo $stocknow; ?>';
                    var qtyinput = jQuery(this).val(); 
                    var overdue = parseInt(qtyinput) - parseInt(stocknow);
                    if (parseInt(qtyinput) > parseInt(stocknow)){
        </script>
        <?php 
  }

What else do I need to add to change the value of delivery time?

In addition to WooCommerce, the plugin "Germanized" is in use.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文