Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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
确定functions.php[wpml]中的语言_Php_Wordpress_Woocommerce_Wpml - Fatal编程技术网

确定functions.php[wpml]中的语言

确定functions.php[wpml]中的语言,php,wordpress,woocommerce,wpml,Php,Wordpress,Woocommerce,Wpml,我在functions.php中有一个函数,当网站被访问时,它会自动按ID将woocommerce产品添加到购物车中 该网站是双语的,功能无法确定翻译后的产品ID 所以,我想知道是否可以在functions.php中添加wpml函数,它决定前端的第一种语言,然后执行函数,类似这样: <?php if(wpml_getLanguage()=='en'); ?> ---do function for product 22--- <?php elseif(wpml_getLangua

我在functions.php中有一个函数,当网站被访问时,它会自动按ID将woocommerce产品添加到购物车中

该网站是双语的,功能无法确定翻译后的产品ID

所以,我想知道是否可以在functions.php中添加wpml函数,它决定前端的第一种语言,然后执行函数,类似这样:

<?php if(wpml_getLanguage()=='en'); ?>
---do function for product 22---
<?php elseif(wpml_getLanguage()=='it'); ?>
---do function for product 45--
<?php endif; ?>

通过使用本机WPML变量
ICL\u LANGUAGE\u code
可以轻松实现这一点

您可以在以下页面上找到有关此主题的更多信息:

这可以放到functions.php中

add_action( 'init', 'add_product_on_language' );

function add_product_on_language(){
    if ( ! is_admin() ) {

        if( ICL_LANGUAGE_CODE == 'en' ){
            $product_id = 22;
        } elseif ( ICL_LANGUAGE_CODE == 'it' ) {
            $product_id = 45;
        }

        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }    
}

我已经做了,但对于将来会看到这个话题的人来说,这并不是完整的答案。谢谢您的关注。@Nick_n1我调整了代码,使其可以拖放到functions.php中
add_action( 'init', 'add_product_on_language' );

function add_product_on_language(){
    if ( ! is_admin() ) {

        if( ICL_LANGUAGE_CODE == 'en' ){
            $product_id = 22;
        } elseif ( ICL_LANGUAGE_CODE == 'it' ) {
            $product_id = 45;
        }

        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }    
}