Php Woocommerce高级配送-具有自定义产品属性的自定义匹配功能

Php Woocommerce高级配送-具有自定义产品属性的自定义匹配功能,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,这是我在这里的第一篇帖子,如果我做得不对,我道歉 我在一家酒厂网站上使用Woocommerce Advanced Shipping插件,以编程方式为6瓶或更多瓶的订单提供便士(0.01美元)配送(因为以6的倍数配送是有效的)。然而,该客户提供的产品包括2或3瓶。数量条件不适用于我,因为在计算数量时,三瓶产品视为单个产品 我已经开始尝试编写一个自定义条件函数,如下指南所述: 我觉得我快接近了,但还是不行。这是我的密码: /** Woocommerce Advanced Shipping (WAS

这是我在这里的第一篇帖子,如果我做得不对,我道歉

我在一家酒厂网站上使用Woocommerce Advanced Shipping插件,以编程方式为6瓶或更多瓶的订单提供便士(0.01美元)配送(因为以6的倍数配送是有效的)。然而,该客户提供的产品包括2或3瓶。数量条件不适用于我,因为在计算数量时,三瓶产品视为单个产品

我已经开始尝试编写一个自定义条件函数,如下指南所述:

我觉得我快接近了,但还是不行。这是我的密码:

/**  Woocommerce Advanced Shipping (WAS)  **/

function check_total() {

    // Only run in the Cart or Checkout pages
    if ( is_cart() || is_checkout() ) {

        global $woocommerce, $product;

        $total_bottles = 0;

        //loop through all cart products
        foreach ( $woocommerce->cart->cart_contents as $product ) {

            // Add all bottles, 'bottles' is custom attribute set on the product page
            $total_bottles += $product['bottles'];

        }

    }
    return $total_bottles;
}


/**
 * Add condition to conditions list.
 *
 * @param       array   $conditions     List of existing conditions.
 * @return      aray                    List of modified conditions.
 */

function was_conditions_add_bottles( $conditions ) {

        // 'General', 'User Details', 'Cart' are default groups, you can also use something custom
            $conditions['General']['bottlenum'] = __( 'Bottles', 'woocommerce-advanced-shipping' );
    return $conditions;
}
add_filter( 'was_conditions', 'was_conditions_add_bottles', 10, 1 );


/**
 * Add value field for 'bottles' condition
 *
 * @param       array   $values         List of value field arguments
 * @param       string  $condition      Name of the condition.
 * @return      array   $values         List of modified value field arguments.
 */
function was_values_add_bottles( $values, $condition ) {
    switch ( $condition ) {
            case 'bottlenum':
                    $values['field'] = 'text';
                    $values['placeholder']  = 'ie. 3';
                    // Option 2; Drop down value field
                    //
                    // $values['field'] = 'select';
                    //
                    // foreach ( array( '1', '2' ) as $key => $value ) :
                    //      $values['options'][ $key ] = $value;
                    // endforeach;

            break;
    }
    return $values;
}
add_filter( 'was_values', 'was_values_add_bottles', 10, 2 );


/**
 * Must match quantity of bottles.
 *
 * @param  bool   $match    Current matching status. Default false.
 * @param  string $operator Store owner selected operator.
 * @param  mixed  $value    Store owner given value.
 * @param  array  $package  Shipping package.
 * @return bool             If the current user/environment matches this condition.
 */
function was_match_condition_bottles( $match, $operator, $value, $package ) {

    // Set total quantity of bottles in cart

$total_bottles = check_total();

    // Check if value exists
    if ( $value ) :
            if ( $operator == '==' ) :
                    $match = ( $total_bottles == $value );
            elseif ( $operator == '!=' ) :
                    $match = ( $total_bottles != $value );
            elseif ( $operator == '>=' ) :
                    $match = ( $total_bottles >= $value );
            elseif ( $operator == '<=' ) :
                    $match = ( $total_bottles <= $value );
            endif;
    endif;
    return $match;
}
add_action( 'was_match_condition_bottles', 'was_match_condition_bottles', 10, 4 );
/**Woocommerce高级配送(WAS)**/
功能检查_总计(){
//仅在购物车或结帐页面中运行
if(is_cart()| | is_checkout()){
全球$商业$产品;
$total_瓶=0;
//循环浏览所有购物车产品
foreach($woocommerce->cart->cart\u内容为$product){
//添加所有瓶子,“瓶子”是在产品页面上设置的自定义属性
$total_瓶+=$product['瓶];
}
}
退回$total_瓶;
}
/**
*将条件添加到条件列表中。
*
*@param array$现有条件列表。
*@return aray修改条件列表。
*/
函数was\u conditions\u add\u瓶子($conditions){
//“常规”、“用户详细信息”、“购物车”是默认组,您也可以使用自定义组
$conditions['General']['battlenum']==(瓶、'woocommerce advanced shipping');
返回$条件;
}
添加过滤器(‘was_条件’、‘was_条件’、‘添加_瓶子’、10、1);
/**
*为“瓶子”条件添加值字段
*
*@param数组$values值字段参数列表
*@param string$条件的条件名称。
*@return array$values修改值字段参数列表。
*/
函数was\u values\u add\u瓶子($values,$condition){
开关($条件){
“瓶装水”案例:
$values['field']='text';
$values['placeholder']='ie.3';
//选项2;下拉值字段
//
//$values['field']='select';
//
//foreach(数组('1','2')作为$key=>$value):
//$values['options'][$key]=$value;
//endforeach;
打破
}
返回$value;
}
添加过滤器(‘was_值’、‘was_值’、‘添加_瓶’、10、2);
/**
*必须与瓶子数量相匹配。
*
*@param bool$匹配当前匹配状态。默认为false。
*@param string$operator存储所有者选择了操作员。
*@param mixed$value存储所有者给定值。
*@param数组$package装运包。
*@如果当前用户/环境符合此条件,则返回bool。
*/
函数为\u匹配\u条件\u瓶子($match,$operator,$value,$package){
//设置购物车中的瓶子总数
$total_瓶=check_total();
//检查值是否存在
如果($value):
如果($operator='='='):
$match=($total_瓶==$value);
elseif($operator='!='):
$match=($total_瓶!=$value);
elseif($operator='>='):
$match=($total_瓶>=$value);

埃尔塞夫($operator=='我有点困惑,因为您一开始谈论发货,然后讨论属性。这里的最终目标是什么?属性用于确定发货。自定义属性是每个产品中的瓶数。客户希望为6瓶以上的订单提供低价发货。如何曾经,这个插件关注的是产品的数量,而不是瓶子的数量(因为有些产品附带2或3个瓶子)。我希望使用一个自定义属性来计算装运包装中的瓶子数量,如果是6个或更多,无论产品的数量如何,都有资格获得低价装运。