【发布时间】:2017-06-08 13:51:08
【问题描述】:
我在functions.php中有一个函数,它会在访问网站时自动将woocommerce产品按ID添加到购物车中。
网站是双语的,功能无法确定翻译后的产品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; ?>
我的代码:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 22;
$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 );
}
}
}
【问题讨论】:
标签: php wordpress woocommerce wpml