• Resolved westham00

    (@westham00)


    I have the premium version of WooCommerce Attribute Stock – Shared Stock & Variable Quantities.

    I am using the Stock Multiplier with my products, but I have built some custom shipping rules which are set by total amount of stock added to basket.

    How can I catch the total stock amount added to basket, to allow my shipping rules to work?

    Example: I have a custom shipping rule which adds $10 shipping unless basket quantity = >48.

    My products have a stock multiplier of 6, some 12 and some 18. Therefore what variable can I use to activate my custom shipping rule.

    So in my

    So in my scenario if I added 3 x items which use thr Stock Multiplier of 18, this would not work as currently my code registers only 3.

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mewz

    (@mewz)

    Hi @westham00,

    In the latest version 2.0.1 you’d do something like this:

    $total_qty = $cart_item['quantity'] * mewz_wcas_get_product_multiplier($cart_item['data']);
    Thread Starter westham00

    (@westham00)

    Thank you for the quick response? Did you just add this in the recent update last night? Impressive if so!

    And now i’m finally trying to get the same value and add it to my order. I don’t seem to be able to retrieve it in the same way. Could you guide me where i’ve gone wrong. Thanks.

    /**
    * Add TTootal quantity to the product column in WooCommerce order details.
    .
    */
    function add_total_qty_to_product_column($item_name, $item, $order_id) {
    // Debug....
    error_log('Item Name: ' . $item_name);

    // Get Product object
    $product = $item->get_product();

    // Debug
    error_log('Product Object: ' . print_r($product, true));

    // Calculate total_qty
    $total_qty = $item->get_quantity() * mewz_wcas_get_product_multiplier($product);

    // Debug
    error_log('Total Qty: ' . $total_qty);

    // Append total quantity to item name
    $total_qty_display = sprintf('<br><small><strong>Total Qty:</strong> %d</small>', $total_qty);

    return $item_name . $total_qty_display;
    }
    add_filter('woocommerce_order_item_name', 'add_total_qty_to_product_column', 10, 3);

    // CSS
    function custom_order_item_column_css() {
    echo '<style>
    .woocommerce-order-items .order-item .item-name small {
    display: block;
    font-size: 0.9em;
    color: #555;
    }
    </style>';
    }
    add_action('admin_head', 'custom_order_item_column_css');



    Plugin Author Mewz

    (@mewz)

    Did you just add this in the recent update last night?

    Well no, this function was already available in v2.0.0. I just made the 2nd parameter optional so it’s easier to use.

    Your code works for me. But keep in mind there are many different ways to show custom metadata on order items. For example, your current code will show on frontend order details, but not in the backend when editing an order.

    You have to take a different approach if you want this to show on admin orders, frontend orders, or both. There’s also the decision to be made whether to show it dynamically, or add it as real order item metadata when the order is placed/created.

    If your code isn’t even working on frontend order details, then it could be that you’re using “any” variations which requires a different approach, or perhaps the specific product variation doesn’t exist anymore. In the latter case the product type will be variable instead of variation. Adding order item metadata when the order is placed helps to avoid issues like this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.