Woocommerce高级送货 - 自定义匹配功能与自定义产品属性

时间:2015-10-08 21:27:34

标签: php wordpress wordpress-plugin woocommerce

这是我在这里发表的第一篇文章,所以如果我不正确的话,我会道歉。

我在酒厂网站上使用Woocommerce Advanced Shipping插件以编程方式为6瓶或更多瓶子的订单提供便士(0.01美元)运输(因为它有效地以6的倍数发货)。但是,该客户提供的产品包括2瓶或3瓶。数量条件对我不起作用,因为在计算数量时,三瓶产品计为单个产品。

我已经开始尝试编写自定义条件函数,因为此处的指南解释了:http://docs.shopplugins.com/article/41-developer-adding-a-custom-condition

我觉得我越来越近了,但它仍然不起作用。这是我的代码:

/**  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功能。但是,我无法从Woocommerce中找到我需要的文档,以便从购物车中的产品中获取自定义属性。

感谢您提供任何帮助。

0 个答案:

没有答案