[英]Woocommerce: Get some custom cart item data value from a plugin
我正在嘗試從購物車中的產品中獲取特定值。 我需要這個值作為在特定類別中設置價格的乘數,我有大部分工作,除了我在獲取這個值時遇到問題,我是通過一個名為 uni-cpo 的插件設置的。
從 var_dump(裁剪)
["_cpo_nov"] => array(1) {
["uni_nov_cpo_stof_sqm"]=>
array(2) {
["display_name"] => string(8) "stof_sqm"
["value"] => float(4) 2000
}
我需要獲取浮點值 2000,但在導航數組時遇到問題。
add_action( 'woocommerce_before_cart', 'get_sqm', 99);
function get_sqm($length){
global $woocommerce;
$length = 0;
foreach ($woocommerce->cart->cart_contents as $cart_item) {
$item_id = $cart_item['6299'];
if (isset($cart_item['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value)) {
$length = $cart_item['data']->uni_nov_cpo_stof_sqm->uni_nov_cpo_stof_sqm->value;
break;
}
}
echo '<span class="hello">hello' . $length . '</span>';
//Debug, comment to disable
if (current_user_can('administrator') ) {
echo '<pre>';
var_dump( $cart_item );
echo '</pre>';
} //Debug end
return $length;
}
我不,對於我的代碼的某些部分,我走在正確的軌道上,但是在導航數組方面卻很遙遠。 如果有人可以提供幫助,指出正確的方向或應用解決方案,我將永遠感激不盡。 苦苦掙扎了幾個星期,到了這個地步。 學習很痛苦::)
根據您裁剪的var_dump
,要獲得所需的值(2000) ,請嘗試以下重新訪問的代碼:
add_action( 'woocommerce_before_cart', 'get_sqm', 99 );
function get_sqm() {
$targeted_id = 6299;
$length = 0;
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
if ( isset( $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'] ) ) {
$length = $cart_item['_cpo_nov']['uni_nov_cpo_stof_sqm']['value'];
echo '<span class="hello">hello' . $length . '</span>';
break;
}
}
// return $length; // Not needed in an action hook
}
代碼進入活動子主題(或活動主題)的functions.php文件。 它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.