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_Woocommerce_Product_Notice - Fatal编程技术网

Php 根据WooCommerce产品价格添加免费送货通知

Php 根据WooCommerce产品价格添加免费送货通知,php,wordpress,woocommerce,product,notice,Php,Wordpress,Woocommerce,Product,Notice,考虑到简单和可变产品,我尝试为两种情况编写一个自定义代码: 如果价格低于25欧元,则提供1份免费送货通知 如果价格高于25欧元,则提供1份免费送货通知 这是我的代码尝试: /** * FREE SHIPPING NOTICE IF PRICE BELOW 25 € */ add_action( 'woocommerce_single_product_summary', 'custom_text1', 15 ); function custom_text1() { // We retr

考虑到简单和可变产品,我尝试为两种情况编写一个自定义代码:

如果价格低于25欧元,则提供1份免费送货通知 如果价格高于25欧元,则提供1份免费送货通知 这是我的代码尝试:

/**
 * FREE SHIPPING NOTICE IF PRICE BELOW 25 €
 */
add_action( 'woocommerce_single_product_summary', 'custom_text1', 15 );
function custom_text1() {
    // We retrieve the minimum of the product
    $min_price = $product->get_variation_price( 'min', true );

    // If price is lower than 25, show first label
    if ($min_price < 25) {
        print '<p class="custom_text1">+ livraison gratuite à partir de 25 €</br>(Belgique, France, Pays-Bas)</p>';
    }
}

/**
 * FREE SHIPPING NOTICE IF PRICE ABOVE 25 €
 */
add_action( 'woocommerce_single_product_summary', 'custom_text2', 15 );
function custom_text2() {
    // We retrieve the minimum of the product
    $min_price = $product->get_variation_price( 'min', true );

    // If price is higher than 25, show second label
    if ($min_price >= 25) {
        print '<p class="custom_text2">+ livraison gratuite</br>(Belgique, France, Pays-Bas)</p>';
    }
}
我没有足够的经验,而且不起作用

我做错了什么?有人能帮我更好地实现这一点吗?

需要定义WC_产品对象$Product,您可以将这两个功能合并为一个,用于可变产品和其他产品类型,如:

add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary_text', 15 );
function custom_single_product_summary_text() {
    global $product;
    
    $price_threshold = 25;

    // Avoid 'cadeaux' product category
    if( has_term( array('Cadeaux'), 'product_cat', $product->get_id() ) ) {
        return;
    }
    
    if( $product->is_type('variable') ) {
        $price = $product->get_variation_price( 'min', true ); // Min price
    } else {
        $price = wc_get_price_to_display( $product );
    }


    // If price is lower than 25
    if ( $price < $price_threshold ) {
        $message = __("+ livraison gratuite jusqu'à 25€</br>(Belgique, France, Pays-Bas)", "text_domain");
    } 
    // If price is up to 25
    else {
        $message = __("+ livraison gratuite à partir de 25 € </br>(Belgique, France, Pays-Bas)", "text_domain");
    }
    echo '<p class="custom_text1">' . $message . '</p>';
}
代码进入活动子主题或活动主题的functions.php文件。已测试并正常工作。

需要定义WC_产品对象$Product,您可以将两个功能合并为一个,用于可变产品和其他产品类型,如:

add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary_text', 15 );
function custom_single_product_summary_text() {
    global $product;
    
    $price_threshold = 25;

    // Avoid 'cadeaux' product category
    if( has_term( array('Cadeaux'), 'product_cat', $product->get_id() ) ) {
        return;
    }
    
    if( $product->is_type('variable') ) {
        $price = $product->get_variation_price( 'min', true ); // Min price
    } else {
        $price = wc_get_price_to_display( $product );
    }


    // If price is lower than 25
    if ( $price < $price_threshold ) {
        $message = __("+ livraison gratuite jusqu'à 25€</br>(Belgique, France, Pays-Bas)", "text_domain");
    } 
    // If price is up to 25
    else {
        $message = __("+ livraison gratuite à partir de 25 € </br>(Belgique, France, Pays-Bas)", "text_domain");
    }
    echo '<p class="custom_text1">' . $message . '</p>';
}

代码进入活动子主题或活动主题的functions.php文件。测试和工作。

@gevcen排除“cadeaux”类别有效吗?@gevcen排除“cadeaux”类别有效吗?